libweston: Add a bits_to_str_stream() equivalent variant

This is useful for cases where we already have an open stream which we
can pass straight in and use it when printing node information.

Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
This commit is contained in:
Marius Vlad 2025-12-18 16:37:20 +02:00
parent 21e904fe50
commit 8b58b0bba1
2 changed files with 42 additions and 13 deletions

View file

@ -9632,8 +9632,8 @@ debug_scene_view_print_paint_node(FILE *fp,
}
if (pnode->try_view_on_plane_failure_reasons) {
char *fr_str = bits_to_str(pnode->try_view_on_plane_failure_reasons,
weston_plane_failure_reasons_to_str);
char *fr_str = bits_to_str_stream(pnode->try_view_on_plane_failure_reasons,
weston_plane_failure_reasons_to_str, fp);
fprintf(fp, "\t\t\t\tPlane failure reasons: %s\n", fr_str);
free(fr_str);

View file

@ -129,6 +129,45 @@ str_printf(char **str_out, const char *fmt, ...)
*str_out = NULL;
}
/**
* Utility to print combination of enum values as string. Uses an already opened
* FILE stream.
*
* Only works for enum whose values are defined as power of two. Given a bitmask
* in which each bit represents an enum value and a function that maps each enum
* value to a string, this function returns a string (comma separated) with all
* the enum values that are present in the bitmask.
*
* \param bits The bitmask of enum values.
* \param map Function that maps enum values to string.
* \param fp FILE stream
* \return A string combining all the enum values from the bitmask, comma
* separated. Callers must free() it.
*/
static inline char *
bits_to_str_stream(uint32_t bits, const char *(*map)(uint32_t), FILE *fp)
{
char *str = NULL;
unsigned i;
const char *sep = "";
if (!fp)
return NULL;
for (i = 0; bits; i++) {
uint32_t bitmask = 1u << i;
if (bits & bitmask) {
fprintf(fp, "%s%s", sep, map(bitmask));
sep = ", ";
}
bits &= ~bitmask;
}
return str;
}
/**
* Utility to print combination of enum values as string
*
@ -148,23 +187,13 @@ bits_to_str(uint32_t bits, const char *(*map)(uint32_t))
FILE *fp;
char *str = NULL;
size_t size = 0;
unsigned i;
const char *sep = "";
fp = open_memstream(&str, &size);
if (!fp)
return NULL;
for (i = 0; bits; i++) {
uint32_t bitmask = 1u << i;
str = bits_to_str_stream(bits, map, fp);
if (bits & bitmask) {
fprintf(fp, "%s%s", sep, map(bitmask));
sep = ", ";
}
bits &= ~bitmask;
}
fclose(fp);
return str;