util/log: Add MESA_LOG_PREFIX environment variable to control log prefixes
Some checks are pending
macOS-CI / macOS-CI (dri) (push) Waiting to run
macOS-CI / macOS-CI (xlib) (push) Waiting to run

Add a new MESA_LOG_PREFIX environment variable that allows fine-grained
control over log prefixes in file logger output on Linux. The variable
accepts a comma-separated list of options:
- "tag": include the tag prefix (e.g., "MESA:")
- "level": include the level prefix (e.g., "info:")

By default, both tag and level are included. Users can customize the
prefix by setting MESA_LOG_PREFIX to any combination (e.g., "tag",
"level", "tag,level", or empty string for no prefix), making the output
more flexible and readable for different use cases.

Other loggers (syslog, Android logcat, Windows debugger) are unaffected
and continue to include tag and level information as appropriate for
their format.

Signed-off-by: Christian Gmeiner <cgmeiner@igalia.com>
Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38217>
This commit is contained in:
Christian Gmeiner 2025-11-05 20:56:14 +01:00 committed by Marge Bot
parent 672c26535a
commit c346f2b673
2 changed files with 22 additions and 8 deletions

View file

@ -98,6 +98,10 @@ Core Mesa environment variables
specifies a file name for logging all errors, warnings, etc., rather
than stderr
.. envvar:: MESA_LOG_PREFIX
specifies what to to include in the log prefix (linux only) - default is ``tag,level``
.. envvar:: MESA_EXTENSION_OVERRIDE
can be used to enable/disable extensions. A value such as

View file

@ -55,6 +55,12 @@ enum mesa_log_control {
MESA_LOG_CONTROL_WAIT = 1 << 8,
};
enum logger_vasnprintf_affix {
LOGGER_VASNPRINTF_AFFIX_TAG = 1 << 0,
LOGGER_VASNPRINTF_AFFIX_LEVEL = 1 << 1,
LOGGER_VASNPRINTF_AFFIX_NEWLINE = 1 << 2,
};
static const struct debug_control mesa_log_control_options[] = {
/* loggers */
{ "null", MESA_LOG_CONTROL_NULL },
@ -96,6 +102,13 @@ level_from_str(const char *str)
static uint32_t mesa_log_control;
static FILE *mesa_log_file;
static enum mesa_log_level mesa_max_log_level;
static int mesa_log_file_affixes;
static const struct debug_named_value log_prefix_options[] = {
{"tag", LOGGER_VASNPRINTF_AFFIX_TAG, "include log tag"},
{"level", LOGGER_VASNPRINTF_AFFIX_LEVEL, "include log level"},
DEBUG_NAMED_VALUE_END
};
static void
mesa_log_init_once(void)
@ -123,6 +136,10 @@ mesa_log_init_once(void)
if (log_level != NULL)
mesa_max_log_level = level_from_str(log_level);
mesa_log_file_affixes =
debug_get_flags_option("MESA_LOG_PREFIX", log_prefix_options,
LOGGER_VASNPRINTF_AFFIX_TAG | LOGGER_VASNPRINTF_AFFIX_LEVEL);
mesa_log_file = stderr;
#if !DETECT_OS_WINDOWS
@ -193,12 +210,6 @@ mesa_log_if_debug(enum mesa_log_level level, const char *outputString)
mesa_log(level, "Mesa", "%s", outputString);
}
enum logger_vasnprintf_affix {
LOGGER_VASNPRINTF_AFFIX_TAG = 1 << 0,
LOGGER_VASNPRINTF_AFFIX_LEVEL = 1 << 1,
LOGGER_VASNPRINTF_AFFIX_NEWLINE = 1 << 2,
};
/* Try vsnprintf first and fall back to vasprintf if buf is too small. This
* function handles all errors and never fails.
*/
@ -281,8 +292,7 @@ logger_file(enum mesa_log_level level,
FILE *fp = mesa_log_file;
char local_msg[1024];
char *msg = logger_vasnprintf(local_msg, sizeof(local_msg),
LOGGER_VASNPRINTF_AFFIX_TAG |
LOGGER_VASNPRINTF_AFFIX_LEVEL |
mesa_log_file_affixes |
LOGGER_VASNPRINTF_AFFIX_NEWLINE,
level, tag, format, va);