From ff43cc76ae9065c73663226d0924e04caf22986e Mon Sep 17 00:00:00 2001 From: Lepton Wu Date: Wed, 18 Dec 2019 18:02:17 -0800 Subject: [PATCH] debug: Add support to log to file. When VIRGL_LOG_FILE gets set, put log into it. This is similar with MESA_LOG_FILE. An improvement over MESA_LOG_FILE is that it supports "%PID%" in file name and will replace it with pid of the current process. Signed-off-by: Lepton Wu Reviewed-by: Gert Wollny --- src/vrend_debug.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/vrend_debug.c b/src/vrend_debug.c index bd9b7cc..fc4ed30 100644 --- a/src/vrend_debug.c +++ b/src/vrend_debug.c @@ -174,7 +174,34 @@ int vrend_debug_can_override(void) static void vrend_default_debug_callback(const char *fmt, va_list va) { - vfprintf(stderr, fmt, va); + static FILE* fp = NULL; + if (NULL == fp) { + const char* log = getenv("VIRGL_LOG_FILE"); + if (log) { + char *log_prefix = strdup(log); + char *log_suffix = strstr(log_prefix, "%PID%"); + if (log_suffix) { + *log_suffix = 0; + log_suffix += 5; + int len = strlen(log) + 32; + char *name = malloc(len); + snprintf(name, len, "%s%d%s", log_prefix, getpid(), log_suffix); + fp = fopen(name, "a"); + free(name); + } else { + fp = fopen(log, "a"); + } + free(log_prefix); + if (NULL == fp) { + fprintf(stderr, "Can't open %s\n", log); + fp = stderr; + } + } else { + fp = stderr; + } + } + vfprintf(fp, fmt, va); + fflush(fp); } static virgl_debug_callback_type debug_callback = vrend_default_debug_callback;