Module: Mesa Branch: main Commit: 8f0a7e848f4dfd05d59f87d8ffe7c89fad7be37b URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=8f0a7e848f4dfd05d59f87d8ffe7c89fad7be37b
Author: Chia-I Wu <[email protected]> Date: Fri Feb 17 17:37:27 2023 -0800 util/log: add support for MESA_LOG_FILE It allows logger_file to log to any file. v2: check "geteuid() == getuid()" Reviewed-by: Emma Anholt <[email protected]> Reviewed-by: Jesse Natalie <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21454> --- src/util/log.c | 29 ++++++++++++++++++++++++++++- src/util/log.h | 4 ++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/util/log.c b/src/util/log.c index d45bb51e61e..a6396bcbafc 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -62,6 +62,7 @@ static const struct debug_control mesa_log_control_options[] = { }; static uint32_t mesa_log_control; +static FILE *mesa_log_file; static void mesa_log_init_once(void) @@ -78,6 +79,21 @@ mesa_log_init_once(void) #endif } + mesa_log_file = stderr; + +#if !DETECT_OS_WINDOWS + if (geteuid() == getuid()) { + const char *log_file = os_get_option("MESA_LOG_FILE"); + if (log_file) { + FILE *fp = fopen(log_file, "w"); + if (fp) { + mesa_log_file = fp; + mesa_log_control |= MESA_LOG_CONTROL_FILE; + } + } + } +#endif + #if DETECT_OS_UNIX if (mesa_log_control & MESA_LOG_CONTROL_SYSLOG) openlog(util_get_process_name(), LOG_NDELAY | LOG_PID, LOG_USER); @@ -184,7 +200,7 @@ logger_file(enum mesa_log_level level, const char *format, va_list va) { - FILE *fp = stderr; + FILE *fp = mesa_log_file; char local_msg[1024]; char *msg = logger_vasnprintf(local_msg, sizeof(local_msg), LOGGER_VASNPRINTF_AFFIX_TAG | @@ -193,6 +209,7 @@ logger_file(enum mesa_log_level level, level, tag, format, va); fprintf(fp, "%s", msg); + fflush(fp); if (msg != local_msg) free(msg); @@ -275,6 +292,16 @@ logger_android(enum mesa_log_level level, #endif /* DETECT_OS_ANDROID */ +/* This is for use with debug functions that take a FILE, such as + * nir_print_shader, although switching to nir_log_shader* is preferred. + */ +FILE * +mesa_log_get_file(void) +{ + mesa_log_init(); + return mesa_log_file; +} + void mesa_log(enum mesa_log_level level, const char *tag, const char *format, ...) { diff --git a/src/util/log.h b/src/util/log.h index d9e965a2bf6..b81716bd212 100644 --- a/src/util/log.h +++ b/src/util/log.h @@ -25,6 +25,7 @@ #define MESA_LOG_H #include <stdarg.h> +#include <stdio.h> #include "util/macros.h" @@ -43,6 +44,9 @@ enum mesa_log_level { MESA_LOG_DEBUG, }; +FILE * +mesa_log_get_file(void); + void PRINTFLIKE(3, 4) mesa_log(enum mesa_log_level, const char *tag, const char *format, ...);
