From 85a542b5c86ea06b01c629995f23dd1e32f2a0e0 Mon Sep 17 00:00:00 2001 From: Aleksander Morgado Date: Fri, 2 Dec 2011 20:11:04 +0100 Subject: cli: new `--verbose' to dump debug logging --- cli/mmcli.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'cli/mmcli.c') diff --git a/cli/mmcli.c b/cli/mmcli.c index 79906602..7fee76f2 100644 --- a/cli/mmcli.c +++ b/cli/mmcli.c @@ -41,10 +41,15 @@ static GMainLoop *loop; static GCancellable *cancellable; /* Context */ +static gboolean verbose_flag; static gboolean version_flag; static gboolean async_flag; static GOptionEntry main_entries[] = { + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag, + "Run action with verbose logs", + NULL + }, { "version", 'V', 0, G_OPTION_ARG_NONE, &version_flag, "Print version", NULL @@ -77,6 +82,44 @@ signals_handler (int signum) } } +static void +log_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + const gchar *log_level_str; + time_t now; + gchar time_str[64]; + struct tm *local_time; + + now = time ((time_t *) NULL); + local_time = localtime (&now); + strftime (time_str, 64, "%d %b %Y, %H:%M:%S", local_time); + + switch (log_level) { + case G_LOG_LEVEL_WARNING: + log_level_str = "-Warning **"; + break; + + case G_LOG_LEVEL_CRITICAL: + case G_LOG_FLAG_FATAL: + case G_LOG_LEVEL_ERROR: + log_level_str = "-Error **"; + break; + + case G_LOG_LEVEL_DEBUG: + log_level_str = "[Debug]"; + break; + + default: + log_level_str = ""; + break; + } + + g_print ("[%s] %s %s\n", time_str, log_level_str, message); +} + static void print_version_and_exit (void) { @@ -125,6 +168,9 @@ main (gint argc, gchar **argv) if (version_flag) print_version_and_exit (); + if (verbose_flag) + g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MASK, log_handler, NULL); + /* Setup signals */ signal (SIGINT, signals_handler); signal (SIGHUP, signals_handler); @@ -190,4 +236,3 @@ main (gint argc, gchar **argv) return EXIT_SUCCESS; } - -- cgit v1.2.3-70-g09d2