Browse Source

log: Make glib logging go through QEMU

This commit adds a error_init() helper which calls
g_log_set_default_handler() so that glib logs (g_log, g_warning, ...)
are handled similarly to other QEMU logs. This means they will get a
timestamp if timestamps are enabled, and they will go through the
HMP monitor if one is configured.

This commit also adds a call to error_init() to the binaries
installed by QEMU. Since error_init() also calls error_set_progname(),
this means that *-linux-user, *-bsd-user and qemu-pr-helper messages
output with error_report, info_report, ... will slightly change: they
will be prefixed by the binary name.

glib debug messages are enabled through G_MESSAGES_DEBUG similarly to
the glib default log handler.

At the moment, this change will mostly impact SPICE logging if your
spice version is >= 0.14.1. With older spice versions, this is not going
to work as expected, but will not have any ill effect, so this call is
not conditional on the SPICE version.

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <2019013116.19209-3-cfergeau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
master
Christophe Fergeau 4 months ago
parent
commit
f5852efa29
9 changed files with 65 additions and 6 deletions
  1. 2
    0
      bsd-user/main.c
  2. 2
    1
      include/qemu/error-report.h
  3. 2
    0
      linux-user/main.c
  4. 1
    1
      qemu-img.c
  5. 1
    1
      qemu-io.c
  6. 1
    1
      qemu-nbd.c
  7. 1
    0
      scsi/qemu-pr-helper.c
  8. 54
    1
      util/qemu-error.c
  9. 1
    1
      vl.c

+ 2
- 0
bsd-user/main.c View File

@@ -24,6 +24,7 @@
24 24
 #include "qapi/error.h"
25 25
 #include "qemu.h"
26 26
 #include "qemu/config-file.h"
27
+#include "qemu/error-report.h"
27 28
 #include "qemu/path.h"
28 29
 #include "qemu/help_option.h"
29 30
 #include "cpu.h"
@@ -743,6 +744,7 @@ int main(int argc, char **argv)
743 744
     if (argc <= 1)
744 745
         usage();
745 746
 
747
+    error_init(argv[0]);
746 748
     module_call_init(MODULE_INIT_TRACE);
747 749
     qemu_init_cpu_list();
748 750
     module_call_init(MODULE_INIT_QOM);

+ 2
- 1
include/qemu/error-report.h View File

@@ -34,7 +34,6 @@ void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
34 34
 void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
35 35
 void error_vprintf_unless_qmp(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
36 36
 void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
37
-void error_set_progname(const char *argv0);
38 37
 
39 38
 void error_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
40 39
 void warn_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
@@ -49,6 +48,8 @@ bool error_report_once_cond(bool *printed, const char *fmt, ...)
49 48
 bool warn_report_once_cond(bool *printed, const char *fmt, ...)
50 49
     GCC_FMT_ATTR(2, 3);
51 50
 
51
+void error_init(const char *argv0);
52
+
52 53
 /*
53 54
  * Similar to error_report(), except it prints the message just once.
54 55
  * Return true when it prints, false otherwise.

+ 2
- 0
linux-user/main.c View File

@@ -27,6 +27,7 @@
27 27
 #include "qemu/path.h"
28 28
 #include "qemu/config-file.h"
29 29
 #include "qemu/cutils.h"
30
+#include "qemu/error-report.h"
30 31
 #include "qemu/help_option.h"
31 32
 #include "cpu.h"
32 33
 #include "exec/exec-all.h"
@@ -600,6 +601,7 @@ int main(int argc, char **argv, char **envp)
600 601
     int ret;
601 602
     int execfd;
602 603
 
604
+    error_init(argv[0]);
603 605
     module_call_init(MODULE_INIT_TRACE);
604 606
     qemu_init_cpu_list();
605 607
     module_call_init(MODULE_INIT_QOM);

+ 1
- 1
qemu-img.c View File

@@ -4923,8 +4923,8 @@ int main(int argc, char **argv)
4923 4923
     signal(SIGPIPE, SIG_IGN);
4924 4924
 #endif
4925 4925
 
4926
+    error_init(argv[0]);
4926 4927
     module_call_init(MODULE_INIT_TRACE);
4927
-    error_set_progname(argv[0]);
4928 4928
     qemu_init_exec_dir(argv[0]);
4929 4929
 
4930 4930
     if (qemu_init_main_loop(&local_error)) {

+ 1
- 1
qemu-io.c View File

@@ -522,8 +522,8 @@ int main(int argc, char **argv)
522 522
     signal(SIGPIPE, SIG_IGN);
523 523
 #endif
524 524
 
525
+    error_init(argv[0]);
525 526
     module_call_init(MODULE_INIT_TRACE);
526
-    error_set_progname(argv[0]);
527 527
     qemu_init_exec_dir(argv[0]);
528 528
 
529 529
     qcrypto_init(&error_fatal);

+ 1
- 1
qemu-nbd.c View File

@@ -690,8 +690,8 @@ int main(int argc, char **argv)
690 690
     signal(SIGPIPE, SIG_IGN);
691 691
 #endif
692 692
 
693
+    error_init(argv[0]);
693 694
     module_call_init(MODULE_INIT_TRACE);
694
-    error_set_progname(argv[0]);
695 695
     qcrypto_init(&error_fatal);
696 696
 
697 697
     module_call_init(MODULE_INIT_QOM);

+ 1
- 0
scsi/qemu-pr-helper.c View File

@@ -895,6 +895,7 @@ int main(int argc, char **argv)
895 895
 
896 896
     signal(SIGPIPE, SIG_IGN);
897 897
 
898
+    error_init(argv[0]);
898 899
     module_call_init(MODULE_INIT_TRACE);
899 900
     module_call_init(MODULE_INIT_QOM);
900 901
     qemu_add_opts(&qemu_trace_opts);

+ 54
- 1
util/qemu-error.c View File

@@ -142,7 +142,7 @@ static const char *progname;
142 142
 /*
143 143
  * Set the program name for error_print_loc().
144 144
  */
145
-void error_set_progname(const char *argv0)
145
+static void error_set_progname(const char *argv0)
146 146
 {
147 147
     const char *p = strrchr(argv0, '/');
148 148
     progname = p ? p + 1 : argv0;
@@ -345,3 +345,56 @@ bool warn_report_once_cond(bool *printed, const char *fmt, ...)
345 345
     va_end(ap);
346 346
     return true;
347 347
 }
348
+
349
+static char *qemu_glog_domains;
350
+
351
+static void qemu_log_func(const gchar *log_domain,
352
+                          GLogLevelFlags log_level,
353
+                          const gchar *message,
354
+                          gpointer user_data)
355
+{
356
+    switch (log_level & G_LOG_LEVEL_MASK) {
357
+    case G_LOG_LEVEL_DEBUG:
358
+    case G_LOG_LEVEL_INFO:
359
+        /*
360
+         * Use same G_MESSAGES_DEBUG logic as glib to enable/disable debug
361
+         * messages
362
+         */
363
+        if (qemu_glog_domains == NULL) {
364
+            break;
365
+        }
366
+        if (strcmp(qemu_glog_domains, "all") != 0 &&
367
+          (log_domain == NULL || !strstr(qemu_glog_domains, log_domain))) {
368
+            break;
369
+        }
370
+        /* Fall through */
371
+    case G_LOG_LEVEL_MESSAGE:
372
+        info_report("%s%s%s",
373
+                    log_domain ?: "", log_domain ? ": " : "", message);
374
+
375
+        break;
376
+    case G_LOG_LEVEL_WARNING:
377
+        warn_report("%s%s%s",
378
+                    log_domain ?: "", log_domain ? ": " : "", message);
379
+        break;
380
+    case G_LOG_LEVEL_CRITICAL:
381
+    case G_LOG_LEVEL_ERROR:
382
+        error_report("%s%s%s",
383
+                     log_domain ?: "", log_domain ? ": " : "", message);
384
+        break;
385
+    }
386
+}
387
+
388
+void error_init(const char *argv0)
389
+{
390
+    /* Set the program name for error_print_loc(). */
391
+    error_set_progname(argv0);
392
+
393
+    /*
394
+     * This sets up glib logging so libraries using it also print their logs
395
+     * through error_report(), warn_report(), info_report().
396
+     */
397
+    g_log_set_default_handler(qemu_log_func, NULL);
398
+    g_warn_if_fail(qemu_glog_domains == NULL);
399
+    qemu_glog_domains = g_strdup(g_getenv("G_MESSAGES_DEBUG"));
400
+}

+ 1
- 1
vl.c View File

@@ -3022,6 +3022,7 @@ int main(int argc, char **argv, char **envp)
3022 3022
     char *dir, **dirs;
3023 3023
     BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3024 3024
 
3025
+    error_init(argv[0]);
3025 3026
     module_call_init(MODULE_INIT_TRACE);
3026 3027
 
3027 3028
     qemu_init_cpu_list();
@@ -3030,7 +3031,6 @@ int main(int argc, char **argv, char **envp)
3030 3031
     qemu_mutex_lock_iothread();
3031 3032
 
3032 3033
     atexit(qemu_run_exit_notifiers);
3033
-    error_set_progname(argv[0]);
3034 3034
     qemu_init_exec_dir(argv[0]);
3035 3035
 
3036 3036
     module_call_init(MODULE_INIT_QOM);

Loading…
Cancel
Save