diff --git a/docs/envvars.rst b/docs/envvars.rst index 034fac5fe51..6a4a16cd5ee 100644 --- a/docs/envvars.rst +++ b/docs/envvars.rst @@ -98,6 +98,13 @@ Core Mesa environment variables specifies a file name for logging all errors, warnings, etc., rather than stderr +.. envvar:: MESA_LOG_FILE_AUTO + + if set, creates a file in /tmp folder with mesa___XXXXXX.log + logging all errors, warnings, etc., rather than stderr. The XXXXXX will be replaced + with alpha numeric character so for each run of the app a new log file will be + created guaranteed. + .. envvar:: MESA_LOG_PREFIX specifies what to to include in the log prefix (linux only) - default is ``tag,level`` diff --git a/src/util/log.c b/src/util/log.c index 4022b6b556e..b594a8f8c30 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -144,13 +144,26 @@ mesa_log_init_once(void) #if !DETECT_OS_WINDOWS if (__normal_user()) { - 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; - } + FILE *fp = NULL; + + if (os_get_option("MESA_LOG_FILE_AUTO")) { + char log_file[512]; + int fd; + + snprintf(log_file, sizeof(log_file), "/tmp/mesa_%s_%d_XXXXXX.log", util_get_process_name(), + getpid()); + fd = mkstemps(log_file, 4); + if (fd >= 0) + fp = fdopen(fd, "w"); + } else { + const char *log_file = os_get_option("MESA_LOG_FILE"); + if (log_file) + fp = fopen(log_file, "w"); + } + + if (fp) { + mesa_log_file = fp; + mesa_log_control |= MESA_LOG_CONTROL_FILE; } } #endif