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 497f7aaefa
commit 7828e7fcfb
2 changed files with 40 additions and 21 deletions

View file

@ -9672,11 +9672,10 @@ 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);
fprintf(fp, "\t\t\t\tPlane failure reasons: %s\n", fr_str);
free(fr_str);
fprintf(fp, "\t\t\t\tPlane failure reasons: ");
bits_to_str_stream(pnode->try_view_on_plane_failure_reasons,
weston_plane_failure_reasons_to_str, fp);
fprintf(fp, "\n");
}
}

View file

@ -130,12 +130,42 @@ str_printf(char **str_out, const char *fmt, ...)
}
/**
* Utility to print combination of enum values as string
* Utility to print combination of enum values as string. Use an opened FILE
* stream to write data.
*
* 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 an opened FILE stream
*/
static inline void
bits_to_str_stream(uint32_t bits, const char *(*map)(uint32_t), FILE *fp)
{
unsigned i;
const char *sep = "";
if (!fp)
return;
for (i = 0; bits; i++) {
uint32_t bitmask = 1u << i;
if (bits & bitmask) {
fprintf(fp, "%s%s", sep, map(bitmask));
sep = ", ";
}
bits &= ~bitmask;
}
}
/**
* Utility to print combination of enum values as string.
* Uses bits_to_str_stream() helper.
*
* 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.
@ -148,23 +178,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;
bits_to_str_stream(bits, map, fp);
if (bits & bitmask) {
fprintf(fp, "%s%s", sep, map(bitmask));
sep = ", ";
}
bits &= ~bitmask;
}
fclose(fp);
return str;