summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/qemu-error.c55
1 files changed, 54 insertions, 1 deletions
diff --git a/util/qemu-error.c b/util/qemu-error.c
index fcbe8a1f74..d08139d9ac 100644
--- a/util/qemu-error.c
+++ b/util/qemu-error.c
@@ -142,7 +142,7 @@ static const char *progname;
/*
* Set the program name for error_print_loc().
*/
-void error_set_progname(const char *argv0)
+static void error_set_progname(const char *argv0)
{
const char *p = strrchr(argv0, '/');
progname = p ? p + 1 : argv0;
@@ -345,3 +345,56 @@ bool warn_report_once_cond(bool *printed, const char *fmt, ...)
va_end(ap);
return true;
}
+
+static char *qemu_glog_domains;
+
+static void qemu_log_func(const gchar *log_domain,
+ GLogLevelFlags log_level,
+ const gchar *message,
+ gpointer user_data)
+{
+ switch (log_level & G_LOG_LEVEL_MASK) {
+ case G_LOG_LEVEL_DEBUG:
+ case G_LOG_LEVEL_INFO:
+ /*
+ * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug
+ * messages
+ */
+ if (qemu_glog_domains == NULL) {
+ break;
+ }
+ if (strcmp(qemu_glog_domains, "all") != 0 &&
+ (log_domain == NULL || !strstr(qemu_glog_domains, log_domain))) {
+ break;
+ }
+ /* Fall through */
+ case G_LOG_LEVEL_MESSAGE:
+ info_report("%s%s%s",
+ log_domain ?: "", log_domain ? ": " : "", message);
+
+ break;
+ case G_LOG_LEVEL_WARNING:
+ warn_report("%s%s%s",
+ log_domain ?: "", log_domain ? ": " : "", message);
+ break;
+ case G_LOG_LEVEL_CRITICAL:
+ case G_LOG_LEVEL_ERROR:
+ error_report("%s%s%s",
+ log_domain ?: "", log_domain ? ": " : "", message);
+ break;
+ }
+}
+
+void error_init(const char *argv0)
+{
+ /* Set the program name for error_print_loc(). */
+ error_set_progname(argv0);
+
+ /*
+ * This sets up glib logging so libraries using it also print their logs
+ * through error_report(), warn_report(), info_report().
+ */
+ g_log_set_default_handler(qemu_log_func, NULL);
+ g_warn_if_fail(qemu_glog_domains == NULL);
+ qemu_glog_domains = g_strdup(g_getenv("G_MESSAGES_DEBUG"));
+}