virgl: move logging to virgl_util.[ch]

Rename the debug callback to virgl logger and add virgl_logv for
logging.  vrend_printf becomes a wrapper of virgl_logv.

v2: use virgl_debug_callback_type (Ryan), add virgl_dummy_logger

Signed-off-by: Chia-I Wu <olvaffe@gmail.com>
Reviewed-by: Ryan Neph <ryanneph@google.com>
Acked-by: Gert Wollny <gert.wollny@collabora.com>
macos/master
Chia-I Wu 3 years ago
parent 8b9ae3bbb6
commit c61eb90392
  1. 62
      src/virgl_util.c
  2. 16
      src/virgl_util.h
  3. 2
      src/virglrenderer.c
  4. 53
      src/vrend_debug.c
  5. 13
      src/vrend_debug.h

@ -34,8 +34,10 @@
#endif #endif
#include <unistd.h> #include <unistd.h>
#include "os/os_misc.h"
#include "util/u_pointer.h" #include "util/u_pointer.h"
#include <assert.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
@ -115,6 +117,66 @@ void flush_eventfd(int fd)
} while ((len == -1 && errno == EINTR) || len == sizeof(value)); } 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 #if ENABLE_TRACING == TRACE_WITH_PERCETTO
PERCETTO_CATEGORY_DEFINE(VIRGL_PERCETTO_CATEGORIES) PERCETTO_CATEGORY_DEFINE(VIRGL_PERCETTO_CATEGORIES)

@ -25,13 +25,16 @@
#ifndef VIRGL_UTIL_H #ifndef VIRGL_UTIL_H
#define VIRGL_UTIL_H #define VIRGL_UTIL_H
#include <stdint.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h>
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif #endif
#include "virglrenderer.h"
#define TRACE_WITH_PERFETTO 1 #define TRACE_WITH_PERFETTO 1
#define TRACE_WITH_STDERR 2 #define TRACE_WITH_STDERR 2
#define TRACE_WITH_PERCETTO 3 #define TRACE_WITH_PERCETTO 3
@ -62,6 +65,17 @@ int create_eventfd(unsigned int initval);
int write_eventfd(int fd, uint64_t val); int write_eventfd(int fd, uint64_t val);
void flush_eventfd(int fd); 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 #ifdef ENABLE_TRACING
void trace_init(void); void trace_init(void);

@ -744,7 +744,7 @@ int virgl_renderer_get_poll_fd(void)
virgl_debug_callback_type virgl_set_debug_callback(virgl_debug_callback_type cb) 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) static int virgl_renderer_export_query(void *execute_args, uint32_t execute_size)

@ -178,56 +178,3 @@ int vrend_debug_can_override(void)
{ {
return vrend_debug_flags & dbg_allow_guest_override; 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;
}

@ -26,6 +26,7 @@
#define vrend_debug_h #define vrend_debug_h
#include "virgl_protocol.h" #include "virgl_protocol.h"
#include "virgl_util.h"
#include <stdarg.h> #include <stdarg.h>
struct vrend_context; 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_debug_add_flag(enum virgl_debug_flags flag);
void vrend_printf(const char *fmt, ...); static inline void vrend_printf(const char *fmt, ...)
{
typedef void (*virgl_debug_callback_type)(const char *fmt, va_list ap); va_list va;
va_start(va, fmt);
virgl_debug_callback_type vrend_set_debug_callback(virgl_debug_callback_type cb); virgl_logv(fmt, va);
va_end(va);
}
#ifndef NDEBUG #ifndef NDEBUG
#define VREND_DEBUG(flag, ctx, ...) \ #define VREND_DEBUG(flag, ctx, ...) \

Loading…
Cancel
Save