From fbb3c8273f339b38edb95f3ddf20aec30cd9b52e Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Mon, 3 Nov 2025 16:22:18 +0100 Subject: [PATCH 1/2] util/log: Add mesa_log_stream_to_file() This function wraps a log_stream into a FILE* pointer, enabling the use of standard fprintf() and other FILE-based functions to write to Mesa's logging system. The implementation uses platform-specific custom stream APIs: - fopencookie() on glibc-based Linux systems - funopen() on Android, macOS, and BSD systems Data written to the FILE* is automatically buffered and flushed through the log_stream infrastructure, with proper line splitting and logging level handling. Closing the FILE* automatically destroys the underlying log_stream. Signed-off-by: Christian Gmeiner --- src/util/log.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/util/log.h | 7 +++++ 2 files changed, 81 insertions(+) diff --git a/src/util/log.c b/src/util/log.c index 4022b6b556e..4fbbac599e4 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -554,3 +554,77 @@ _mesa_log_multiline(enum mesa_log_level level, const char *tag, const char *line mesa_log_stream_flush(&tmp, 0); free(tmp.msg); } + +#if DETECT_OS_LINUX && !DETECT_OS_ANDROID +/* glibc-based Linux (has fopencookie) */ +static ssize_t +log_stream_write_cookie(void *cookie, const char *buf, size_t size) +{ + mesa_log_stream_printf(cookie, "%.*s", (int)size, buf); + + return size; +} + +static int +log_stream_close_cookie(void *cookie) +{ + mesa_log_stream_destroy(cookie); + + return 0; +} + +FILE * +mesa_log_stream_to_file(struct log_stream *stream) +{ + if (!stream) + return NULL; + + static const cookie_io_functions_t funcs = { + .read = NULL, + .write = log_stream_write_cookie, + .seek = NULL, + .close = log_stream_close_cookie + }; + + return fopencookie(stream, "w", funcs); +} +#elif DETECT_OS_ANDROID || DETECT_OS_APPLE || DETECT_OS_BSD +/* BSD-style (has funopen) */ +static int +log_stream_write_func(void *cookie, const char *buf, int size) +{ + mesa_log_stream_printf(cookie, "%.*s", size, buf); + + return size; +} + +static int +log_stream_close_func(void *cookie) +{ + mesa_log_stream_destroy(cookie); + + return 0; +} + +FILE * +mesa_log_stream_to_file(struct log_stream *stream) +{ + if (!stream) + return NULL; + + return funopen(stream, + NULL, /* read */ + log_stream_write_func, /* write */ + NULL, /* seek */ + log_stream_close_func); /* close */ +} + +#else +/* Unsupported platform */ +FILE * +mesa_log_stream_to_file(struct log_stream *stream) +{ + (void)stream; + return NULL; +} +#endif diff --git a/src/util/log.h b/src/util/log.h index c1d9f806232..cb0ea4c4962 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -129,6 +129,13 @@ static inline void PRINTFLIKE(1, 2) __mesa_log_use_args(UNUSED const char *format, ...) { } #endif +FILE * +mesa_log_stream_to_file(struct log_stream *stream); + +#define mesa_log_file_streame() mesa_log_stream_to_file(mesa_log_streame()) +#define mesa_log_file_streamw() mesa_log_stream_to_file(mesa_log_streamw()) +#define mesa_log_file_streami() mesa_log_stream_to_file(mesa_log_streami()) + #ifdef __cplusplus } #endif From 0610865eab8857f2780e09be783358bcc510b2dd Mon Sep 17 00:00:00 2001 From: Christian Gmeiner Date: Mon, 3 Nov 2025 16:32:56 +0100 Subject: [PATCH 2/2] anv: Convert DEBUG_SHADERS_LINENO logging to use mesa_log Signed-off-by: Christian Gmeiner --- src/intel/vulkan/anv_blorp.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/intel/vulkan/anv_blorp.c b/src/intel/vulkan/anv_blorp.c index 0b186bd4354..4da75d4cbad 100644 --- a/src/intel/vulkan/anv_blorp.c +++ b/src/intel/vulkan/anv_blorp.c @@ -89,9 +89,14 @@ upload_blorp_shader(struct blorp_batch *batch, uint32_t stage, if (INTEL_DEBUG(DEBUG_SHADERS_LINENO)) { /* shader hash is zero in this context */ if (!intel_shader_dump_filter) { - brw_disassemble_with_lineno(&device->physical->compiler->isa, - stage, -1, 0, kernel, 0, - bin->kernel.offset, stderr); + FILE *out = mesa_log_file_streami(); + + if (out) { + brw_disassemble_with_lineno(&device->physical->compiler->isa, + stage, -1, 0, kernel, 0, + bin->kernel.offset, out); + fclose(out); + } } }