*/
#include "yagl_log.h"
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <pthread.h>
+#include "qemu-common.h"
+#include <glib/gprintf.h>
-static const char* g_log_level_to_str[yagl_log_level_max + 1] =
-{
- "OFF",
- "ERROR",
- "WARN",
- "INFO",
- "DEBUG",
- "TRACE"
+/* "OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" */
+static const char g_log_level_acronym[yagl_log_level_max + 1] = {
+ '-', 'E', 'W', 'I', 'D', 'T'
};
-static struct
-{
- const char* datatype;
- const char* format;
-} g_datatype_to_format[] =
-{
+static struct {
+ const char *datatype;
+ const char *format;
+} g_datatype_to_format[] = {
{ "EGLboolean", "%u" },
{ "EGLenum", "0x%x" },
{ "EGLint", "%d" },
};
static yagl_log_level g_log_level = yagl_log_level_off;
-static char** g_log_facilities_match = NULL;
-static char** g_log_facilities_no_match = NULL;
-static bool g_log_func_trace = false;
+static char **g_log_facilities_match;
+static char **g_log_facilities_no_match;
+static bool g_log_func_trace;
-static const char* yagl_log_datatype_to_format(const char* type)
+static const char *yagl_log_datatype_to_format(const char *type)
{
unsigned int i;
- if (strchr(type, '*'))
- {
+ if (strchr(type, '*')) {
return "%p";
}
- for (i = 0; i < sizeof(g_datatype_to_format)/sizeof(g_datatype_to_format[0]); ++i)
- {
- if (strcmp(g_datatype_to_format[i].datatype, type) == 0)
- {
+ for (i = 0;
+ i < sizeof(g_datatype_to_format)/sizeof(g_datatype_to_format[0]);
+ ++i) {
+ if (strcmp(g_datatype_to_format[i].datatype, type) == 0) {
return g_datatype_to_format[i].format;
}
}
return NULL;
}
-static void yagl_log_print_current_time(void)
+static size_t yagl_log_print_current_time(char *buf, size_t buf_size)
{
- char buff[128];
#ifdef _WIN32
- struct tm *ptm;
+ struct tm *ptm = NULL;
#else
struct tm tm;
#endif
qemu_timeval tv = { 0, 0 };
time_t ti;
+ size_t ret_size;
qemu_gettimeofday(&tv);
-
ti = tv.tv_sec;
#ifdef _WIN32
ptm = localtime(&ti);
- strftime(buff, sizeof(buff),
- "%H:%M:%S", ptm);
+ ret_size = strftime(buf, buf_size, "%H:%M:%S", ptm);
#else
localtime_r(&ti, &tm);
- strftime(buff, sizeof(buff),
- "%H:%M:%S", &tm);
+ ret_size = strftime(buf, buf_size, "%H:%M:%S", &tm);
#endif
- fprintf(stderr, "%s", buff);
+ ret_size += g_snprintf(buf + ret_size, buf_size - ret_size,
+ ".%03ld", (long)tv.tv_usec/1000);
+ return ret_size;
}
/*
* Simple asterisk pattern matcher.
*/
-static bool yagl_log_match(const char* str, const char* expr)
+static bool yagl_log_match(const char *str, const char *expr)
{
- while (*str && *expr)
- {
+ while (*str && *expr) {
/*
* Swallow '**'.
*/
while ((*expr == '*') &&
- (*(expr + 1) == '*'))
- {
+ (*(expr + 1) == '*')) {
++expr;
}
- if (*expr == '*')
- {
- if (!*(expr + 1))
- {
+ if (*expr == '*') {
+ if (!*(expr + 1)) {
/*
* Last '*' always matches.
*/
* Search for symbol after the asterisk.
*/
- while (*str && (*str != *(expr + 1)))
- {
+ while (*str && (*str != *(expr + 1))) {
++str;
}
- if (!*str)
- {
+ if (!*str) {
/*
* Reached the end, didn't find symbol following asterisk,
* no match.
++str;
expr += 2;
- }
- else
- {
+ } else {
/*
* No asterisk, exact match.
*/
- if (*str != *expr)
- {
+ if (*str != *expr) {
return false;
}
* Remaining '*' always match.
*/
- while (*expr == '*')
- {
+ while (*expr == '*') {
++expr;
}
void yagl_log_init(void)
{
- char* level_str = getenv("YAGL_DEBUG");
+ char *level_str = getenv("YAGL_DEBUG");
int level = level_str ? atoi(level_str) : yagl_log_level_info;
- char* facilities;
- char* func_trace;
+ char *facilities;
+ char *func_trace;
- if (level < 0)
- {
+ if (level < 0) {
g_log_level = yagl_log_level_off;
- }
- else if (level > yagl_log_level_max)
- {
+ } else if (level > yagl_log_level_max) {
g_log_level = (yagl_log_level)yagl_log_level_max;
- }
- else
- {
+ } else {
g_log_level = (yagl_log_level)level;
}
facilities = getenv("YAGL_DEBUG_FACILITIES");
- if (facilities)
- {
- char* tmp_facilities = facilities;
+ if (facilities) {
+ char *tmp_facilities = facilities;
int i = 0, num_match = 0, num_no_match = 0;
- while (1)
- {
- char* tmp = strchr(tmp_facilities, ',');
+ while (1) {
+ char *tmp = strchr(tmp_facilities, ',');
- if (!tmp)
- {
+ if (!tmp) {
break;
}
- if (tmp - tmp_facilities > 0)
- {
+ if (tmp - tmp_facilities > 0) {
++i;
}
tmp_facilities = tmp + 1;
}
- if (strlen(tmp_facilities) > 0)
- {
+ if (strlen(tmp_facilities) > 0) {
++i;
}
- g_log_facilities_match = g_malloc0((i + 1) * sizeof(char*));
- g_log_facilities_no_match = g_malloc0((i + 1) * sizeof(char*));
+ g_log_facilities_match = g_malloc0((i + 1) * sizeof(char *));
+ g_log_facilities_no_match = g_malloc0((i + 1) * sizeof(char *));
tmp_facilities = facilities;
- while (1)
- {
- char* tmp = strchr(tmp_facilities, ',');
+ while (1) {
+ char *tmp = strchr(tmp_facilities, ',');
- if (!tmp)
- {
+ if (!tmp) {
break;
}
- if (tmp - tmp_facilities > 0)
- {
- if (*tmp_facilities == '^')
- {
- if ((tmp - tmp_facilities - 1) > 0)
- {
- g_log_facilities_no_match[num_no_match] = g_strndup(tmp_facilities + 1, tmp - tmp_facilities - 1);
+ if (tmp - tmp_facilities > 0) {
+ if (*tmp_facilities == '^') {
+ if ((tmp - tmp_facilities - 1) > 0) {
+ g_log_facilities_no_match[num_no_match] =
+ g_strndup(tmp_facilities + 1,
+ tmp - tmp_facilities - 1);
++num_no_match;
}
- }
- else
- {
- g_log_facilities_match[num_match] = g_strndup(tmp_facilities, tmp - tmp_facilities);
+ } else {
+ g_log_facilities_match[num_match] =
+ g_strndup(tmp_facilities,
+ tmp - tmp_facilities);
++num_match;
}
}
tmp_facilities = tmp + 1;
}
- if (strlen(tmp_facilities) > 0)
- {
- if (*tmp_facilities == '^')
- {
- if ((strlen(tmp_facilities) - 1) > 0)
- {
- g_log_facilities_no_match[num_no_match] = g_strdup(tmp_facilities + 1);
+ if (strlen(tmp_facilities) > 0) {
+ if (*tmp_facilities == '^') {
+ if ((strlen(tmp_facilities) - 1) > 0) {
+ g_log_facilities_no_match[num_no_match] =
+ g_strdup(tmp_facilities + 1);
++num_no_match;
}
- }
- else
- {
+ } else {
g_log_facilities_match[num_match] = g_strdup(tmp_facilities);
++num_match;
}
g_log_facilities_no_match[num_no_match] = NULL;
g_log_facilities_match[num_match] = NULL;
- if (!num_no_match)
- {
+ if (!num_no_match) {
g_free(g_log_facilities_no_match);
g_log_facilities_no_match = NULL;
}
- if (!num_match)
- {
+ if (!num_match) {
g_free(g_log_facilities_match);
g_log_facilities_match = NULL;
}
{
int i;
g_log_level = yagl_log_level_off;
- if (g_log_facilities_no_match)
- {
- for (i = 0; g_log_facilities_no_match[i]; ++i)
- {
+ if (g_log_facilities_no_match) {
+ for (i = 0; g_log_facilities_no_match[i]; ++i) {
g_free(g_log_facilities_no_match[i]);
g_log_facilities_no_match[i] = NULL;
}
g_free(g_log_facilities_no_match);
g_log_facilities_no_match = NULL;
}
- if (g_log_facilities_match)
- {
- for (i = 0; g_log_facilities_match[i]; ++i)
- {
+ if (g_log_facilities_match) {
+ for (i = 0; g_log_facilities_match[i]; ++i) {
g_free(g_log_facilities_match[i]);
g_log_facilities_match[i] = NULL;
}
void yagl_log_event(yagl_log_level log_level,
yagl_pid process_id,
yagl_tid thread_id,
- const char* facility,
+ const char *facility,
int line,
- const char* format, ...)
+ const char *format, ...)
{
va_list args;
-
- yagl_log_print_current_time();
- fprintf(stderr,
- " %-5s [%u/%u] %s:%d",
- g_log_level_to_str[log_level],
- process_id,
- thread_id,
- facility,
- line);
- if (format)
- {
+ size_t ret_size;
+ char log_buf[2048];
+
+ ret_size = yagl_log_print_current_time(log_buf, sizeof(log_buf));
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size,
+ "|%5d|%1c|%10s|%4d|[%u/%u] %s:%d",
+ qemu_get_thread_id(),
+ g_log_level_acronym[log_level],
+ "yagl",
+ line,
+ process_id,
+ thread_id,
+ facility,
+ line);
+ if (format) {
va_start(args, format);
- fprintf(stderr, " - ");
- vfprintf(stderr, format, args);
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, " - ");
+ ret_size += g_vsnprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, format, args);
va_end(args);
}
- fprintf(stderr, "\n");
+ g_fprintf(stderr, "%s\n", log_buf);
}
void yagl_log_func_enter(yagl_pid process_id,
yagl_tid thread_id,
- const char* func,
+ const char *func,
int line,
- const char* format, ...)
+ const char *format, ...)
{
va_list args;
-
- yagl_log_print_current_time();
- fprintf(stderr,
- " %-5s [%u/%u] {{{ %s(",
- g_log_level_to_str[yagl_log_level_trace],
- process_id,
- thread_id,
- func);
- if (format)
- {
+ size_t ret_size;
+ char log_buf[2048];
+
+ ret_size = yagl_log_print_current_time(log_buf, sizeof(log_buf));
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size,
+ "|%5d|%1c|%10s|%4d|[%u/%u] {{{ %s(",
+ qemu_get_thread_id(),
+ g_log_level_acronym[yagl_log_level_trace],
+ "yagl",
+ line,
+ process_id,
+ thread_id,
+ func);
+ if (format) {
va_start(args, format);
- vfprintf(stderr, format, args);
+ ret_size += g_vsnprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, format, args);
va_end(args);
}
- fprintf(stderr, "):%d\n", line);
+ g_fprintf(stderr, "%s):%d\n", log_buf, line);
}
void yagl_log_func_exit(yagl_pid process_id,
yagl_tid thread_id,
- const char* func,
+ const char *func,
int line,
- const char* format, ...)
+ const char *format, ...)
{
va_list args;
-
- yagl_log_print_current_time();
- fprintf(stderr,
- " %-5s [%u/%u] }}} %s:%d",
- g_log_level_to_str[yagl_log_level_trace],
- process_id,
- thread_id,
- func,
- line);
- if (format)
- {
+ size_t ret_size;
+ char log_buf[2048];
+
+ ret_size = yagl_log_print_current_time(log_buf, sizeof(log_buf));
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size,
+ "|%5d|%1c|%10s|%4d|[%u/%u] }}} %s:%d",
+ qemu_get_thread_id(),
+ g_log_level_acronym[yagl_log_level_trace],
+ "yagl",
+ line,
+ process_id,
+ thread_id,
+ func,
+ line);
+ if (format) {
va_start(args, format);
- fprintf(stderr, " = ");
- vfprintf(stderr, format, args);
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, " = ");
+ ret_size += g_vsnprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, format, args);
va_end(args);
}
- fprintf(stderr, "\n");
+ g_fprintf(stderr, "%s\n", log_buf);
}
void yagl_log_func_enter_split(yagl_pid process_id,
yagl_tid thread_id,
- const char* func,
+ const char *func,
int line,
int num_args, ...)
{
char format[1025] = { '\0' };
va_list args;
-
- yagl_log_print_current_time();
- fprintf(stderr,
- " %-5s [%u/%u] {{{ %s(",
- g_log_level_to_str[yagl_log_level_trace],
- process_id,
- thread_id,
- func);
-
- if (num_args > 0)
- {
+ size_t ret_size;
+ char log_buf[2048];
+
+ ret_size = yagl_log_print_current_time(log_buf, sizeof(log_buf));
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size,
+ "|%5d|%1c|%10s|%4d|[%u/%u] {{{ %s(",
+ qemu_get_thread_id(),
+ g_log_level_acronym[yagl_log_level_trace],
+ "yagl",
+ line,
+ process_id,
+ thread_id,
+ func);
+
+ if (num_args > 0) {
int i;
bool skip = false;
va_start(args, num_args);
- for (i = 0; i < num_args;)
- {
- const char* arg_format = yagl_log_datatype_to_format(va_arg(args, const char*));
- const char* arg_name;
+ for (i = 0; i < num_args;) {
+ const char *arg_format =
+ yagl_log_datatype_to_format(va_arg(args,
+ const char *));
+ const char *arg_name;
- if (!arg_format)
- {
+ if (!arg_format) {
skip = true;
break;
}
- arg_name = va_arg(args, const char*);
+ arg_name = va_arg(args, const char *);
strcat(format, arg_name);
strcat(format, " = ");
++i;
- if (i < num_args)
- {
+ if (i < num_args) {
strcat(format, ", ");
}
}
- if (skip)
- {
- fprintf(stderr, "...");
- }
- else
- {
- vfprintf(stderr, format, args);
+ if (skip) {
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, "...");
+
+ } else {
+ ret_size += g_vsnprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, format, args);
}
va_end(args);
}
- fprintf(stderr, "):%d\n", line);
+ g_fprintf(stderr, "%s):%d\n", log_buf, line);
}
void yagl_log_func_exit_split(yagl_pid process_id,
yagl_tid thread_id,
- const char* func,
+ const char *func,
int line,
- const char* datatype, ...)
+ const char *datatype, ...)
{
va_list args;
+ size_t ret_size;
+ char log_buf[2048];
+
+ ret_size = yagl_log_print_current_time(log_buf, sizeof(log_buf));
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size,
+ "|%5d|%1c|%10s|%4d|[%u/%u] }}} %s:%d",
+ qemu_get_thread_id(),
+ g_log_level_acronym[yagl_log_level_trace],
+ "yagl",
+ line,
+ process_id,
+ thread_id,
+ func,
+ line);
+
+ if (datatype) {
+ const char *format = yagl_log_datatype_to_format(datatype);
+ if (format) {
+ ret_size += g_snprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, " = ");
- yagl_log_print_current_time();
- fprintf(stderr,
- " %-5s [%u/%u] }}} %s:%d",
- g_log_level_to_str[yagl_log_level_trace],
- process_id,
- thread_id,
- func,
- line);
-
- if (datatype)
- {
- const char* format = yagl_log_datatype_to_format(datatype);
- if (format)
- {
- fprintf(stderr, " = ");
va_start(args, datatype);
- vfprintf(stderr, format, args);
+ ret_size += g_vsnprintf(log_buf + ret_size,
+ sizeof(log_buf) - ret_size, format, args);
va_end(args);
}
}
- fprintf(stderr, "\n");
+ g_fprintf(stderr, "%s\n", log_buf);
}
bool yagl_log_is_enabled_for_level(yagl_log_level log_level)
return log_level <= g_log_level;
}
-bool yagl_log_is_enabled_for_facility(const char* facility)
+bool yagl_log_is_enabled_for_facility(const char *facility)
{
int i;
bool res = false;
- if (g_log_facilities_match)
- {
- for (i = 0; g_log_facilities_match[i]; ++i)
- {
- if (yagl_log_match(facility, g_log_facilities_match[i]))
- {
+ if (g_log_facilities_match) {
+ for (i = 0; g_log_facilities_match[i]; ++i) {
+ if (yagl_log_match(facility, g_log_facilities_match[i])) {
res = true;
break;
}
}
- }
- else
- {
+ } else {
res = true;
}
- if (res && g_log_facilities_no_match)
- {
- for (i = 0; g_log_facilities_no_match[i]; ++i)
- {
- if (yagl_log_match(facility, g_log_facilities_no_match[i]))
- {
+ if (res && g_log_facilities_no_match) {
+ for (i = 0; g_log_facilities_no_match[i]; ++i) {
+ if (yagl_log_match(facility, g_log_facilities_no_match[i])) {
res = false;
break;
}