diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2011-12-02 20:11:04 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-03-15 14:14:32 +0100 |
commit | 85a542b5c86ea06b01c629995f23dd1e32f2a0e0 (patch) | |
tree | 307c21649a2c8c012737cff541b452196a415da3 | |
parent | e1cba4a53365753aa1ecadd06b369b2a43765037 (diff) |
cli: new `--verbose' to dump debug logging
-rw-r--r-- | cli/mmcli.c | 47 |
1 files changed, 46 insertions, 1 deletions
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 @@ -78,6 +83,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) { g_print ("\n" @@ -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; } - |