diff --git a/src/virgl_util.c b/src/virgl_util.c index 0ed3d22..99ff88e 100644 --- a/src/virgl_util.c +++ b/src/virgl_util.c @@ -34,8 +34,10 @@ #endif #include +#include "os/os_misc.h" #include "util/u_pointer.h" +#include #include #include @@ -115,6 +117,66 @@ void flush_eventfd(int fd) } while ((len == -1 && errno == EINTR) || len == sizeof(value)); } +static +void virgl_default_logger(const char *fmt, va_list 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 +void virgl_null_logger(UNUSED const char *fmt, UNUSED va_list va) +{ +} + +static virgl_debug_callback_type virgl_logger = virgl_default_logger; + +virgl_debug_callback_type virgl_log_set_logger(virgl_debug_callback_type logger) +{ + virgl_debug_callback_type old = virgl_logger; + + /* virgl_null_logger is internal */ + if (old == virgl_null_logger) + old = NULL; + if (!logger) + logger = virgl_null_logger; + + virgl_logger = logger; + return old; +} + +void virgl_logv(const char *fmt, va_list va) +{ + assert(virgl_logger); + virgl_logger(fmt, va); +} + #if ENABLE_TRACING == TRACE_WITH_PERCETTO PERCETTO_CATEGORY_DEFINE(VIRGL_PERCETTO_CATEGORIES) diff --git a/src/virgl_util.h b/src/virgl_util.h index cd02fa9..4559f35 100644 --- a/src/virgl_util.h +++ b/src/virgl_util.h @@ -25,13 +25,16 @@ #ifndef VIRGL_UTIL_H #define VIRGL_UTIL_H -#include +#include #include +#include #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include "virglrenderer.h" + #define TRACE_WITH_PERFETTO 1 #define TRACE_WITH_STDERR 2 #define TRACE_WITH_PERCETTO 3 @@ -62,6 +65,17 @@ int create_eventfd(unsigned int initval); int write_eventfd(int fd, uint64_t val); void flush_eventfd(int fd); +virgl_debug_callback_type virgl_log_set_logger(virgl_debug_callback_type logger); +void virgl_logv(const char *fmt, va_list va); + +static inline void virgl_log(const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + virgl_logv(fmt, va); + va_end(va); +} + #ifdef ENABLE_TRACING void trace_init(void); diff --git a/src/virglrenderer.c b/src/virglrenderer.c index 7250c1d..8d69ce7 100644 --- a/src/virglrenderer.c +++ b/src/virglrenderer.c @@ -744,7 +744,7 @@ int virgl_renderer_get_poll_fd(void) virgl_debug_callback_type virgl_set_debug_callback(virgl_debug_callback_type cb) { - return vrend_set_debug_callback(cb); + return virgl_log_set_logger(cb); } static int virgl_renderer_export_query(void *execute_args, uint32_t execute_size) diff --git a/src/vrend_debug.c b/src/vrend_debug.c index 4129643..a0ab7bc 100644 --- a/src/vrend_debug.c +++ b/src/vrend_debug.c @@ -178,56 +178,3 @@ int vrend_debug_can_override(void) { return vrend_debug_flags & dbg_allow_guest_override; } - -static -void vrend_default_debug_callback(const char *fmt, va_list 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; - -void vrend_printf(const char *fmt, ...) -{ - if (debug_callback) { - va_list va; - va_start(va, fmt); - debug_callback(fmt, va); - va_end(va); - } -} - -virgl_debug_callback_type vrend_set_debug_callback(virgl_debug_callback_type cb) -{ - virgl_debug_callback_type retval = debug_callback; - debug_callback = cb; - return retval; -} - diff --git a/src/vrend_debug.h b/src/vrend_debug.h index 481e20f..40ff226 100644 --- a/src/vrend_debug.h +++ b/src/vrend_debug.h @@ -26,6 +26,7 @@ #define vrend_debug_h #include "virgl_protocol.h" +#include "virgl_util.h" #include struct vrend_context; @@ -70,11 +71,13 @@ unsigned vrend_debug(const struct vrend_context *ctx, enum virgl_debug_flags fla void vrend_debug_add_flag(enum virgl_debug_flags flag); -void vrend_printf(const char *fmt, ...); - -typedef void (*virgl_debug_callback_type)(const char *fmt, va_list ap); - -virgl_debug_callback_type vrend_set_debug_callback(virgl_debug_callback_type cb); +static inline void vrend_printf(const char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + virgl_logv(fmt, va); + va_end(va); +} #ifndef NDEBUG #define VREND_DEBUG(flag, ctx, ...) \