From 7828e7fcfbd89f71d097841d2ea6ddd2cfe2473b Mon Sep 17 00:00:00 2001 From: Marius Vlad Date: Thu, 18 Dec 2025 16:37:20 +0200 Subject: [PATCH] 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 --- libweston/compositor.c | 9 ++++--- shared/string-helpers.h | 52 ++++++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/libweston/compositor.c b/libweston/compositor.c index dac228463..2adc2bf9d 100644 --- a/libweston/compositor.c +++ b/libweston/compositor.c @@ -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"); } } diff --git a/shared/string-helpers.h b/shared/string-helpers.h index d566f8c2f..e17348ca4 100644 --- a/shared/string-helpers.h +++ b/shared/string-helpers.h @@ -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;