Skip to content

Commit

Permalink
log: Make glib logging go through QEMU
Browse files Browse the repository at this point in the history
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: <20190131164614.19209-3-cfergeau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
  • Loading branch information
cfergeau authored and Markus Armbruster committed Apr 17, 2019
1 parent 99e98d7 commit f5852ef
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bsd-user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qapi/error.h"
#include "qemu.h"
#include "qemu/config-file.h"
#include "qemu/error-report.h"
#include "qemu/path.h"
#include "qemu/help_option.h"
#include "cpu.h"
Expand Down Expand Up @@ -743,6 +744,7 @@ int main(int argc, char **argv)
if (argc <= 1)
usage();

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
qemu_init_cpu_list();
module_call_init(MODULE_INIT_QOM);
Expand Down
3 changes: 2 additions & 1 deletion include/qemu/error-report.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void error_vprintf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
void error_printf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void error_vprintf_unless_qmp(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
void error_printf_unless_qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
void error_set_progname(const char *argv0);

void error_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
void warn_vreport(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
Expand All @@ -49,6 +48,8 @@ bool error_report_once_cond(bool *printed, const char *fmt, ...)
bool warn_report_once_cond(bool *printed, const char *fmt, ...)
GCC_FMT_ATTR(2, 3);

void error_init(const char *argv0);

/*
* Similar to error_report(), except it prints the message just once.
* Return true when it prints, false otherwise.
Expand Down
2 changes: 2 additions & 0 deletions linux-user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "qemu/path.h"
#include "qemu/config-file.h"
#include "qemu/cutils.h"
#include "qemu/error-report.h"
#include "qemu/help_option.h"
#include "cpu.h"
#include "exec/exec-all.h"
Expand Down Expand Up @@ -600,6 +601,7 @@ int main(int argc, char **argv, char **envp)
int ret;
int execfd;

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
qemu_init_cpu_list();
module_call_init(MODULE_INIT_QOM);
Expand Down
2 changes: 1 addition & 1 deletion qemu-img.c
Original file line number Diff line number Diff line change
Expand Up @@ -4923,8 +4923,8 @@ int main(int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
#endif

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
error_set_progname(argv[0]);
qemu_init_exec_dir(argv[0]);

if (qemu_init_main_loop(&local_error)) {
Expand Down
2 changes: 1 addition & 1 deletion qemu-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ int main(int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
#endif

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
error_set_progname(argv[0]);
qemu_init_exec_dir(argv[0]);

qcrypto_init(&error_fatal);
Expand Down
2 changes: 1 addition & 1 deletion qemu-nbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,8 @@ int main(int argc, char **argv)
signal(SIGPIPE, SIG_IGN);
#endif

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
error_set_progname(argv[0]);
qcrypto_init(&error_fatal);

module_call_init(MODULE_INIT_QOM);
Expand Down
1 change: 1 addition & 0 deletions scsi/qemu-pr-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ int main(int argc, char **argv)

signal(SIGPIPE, SIG_IGN);

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);
module_call_init(MODULE_INIT_QOM);
qemu_add_opts(&qemu_trace_opts);
Expand Down
55 changes: 54 additions & 1 deletion util/qemu-error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
}
2 changes: 1 addition & 1 deletion vl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,7 @@ int main(int argc, char **argv, char **envp)
char *dir, **dirs;
BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);

error_init(argv[0]);
module_call_init(MODULE_INIT_TRACE);

qemu_init_cpu_list();
Expand All @@ -3030,7 +3031,6 @@ int main(int argc, char **argv, char **envp)
qemu_mutex_lock_iothread();

atexit(qemu_run_exit_notifiers);
error_set_progname(argv[0]);
qemu_init_exec_dir(argv[0]);

module_call_init(MODULE_INIT_QOM);
Expand Down

0 comments on commit f5852ef

Please sign in to comment.