From cad2ac34f91dd2d90ff1cc623680238c475ea856 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Thu, 7 Dec 2017 20:15:54 +0100 Subject: [PATCH] Add GLib logging capabilities --- src/dunst.c | 3 +++ src/log.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/log.h | 20 ++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/src/dunst.c b/src/dunst.c index 38c8957..3998eeb 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -13,6 +13,7 @@ #include #include "dbus.h" +#include "log.h" #include "menu.h" #include "notification.h" #include "option_parser.h" @@ -128,6 +129,8 @@ int dunst_main(int argc, char *argv[]) cmdline_load(argc, argv); + dunst_log_init(); + if (cmdline_get_bool("-v/-version", false, "Print version") || cmdline_get_bool("--version", false, "Print version")) { print_version(); diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..e3b8594 --- /dev/null +++ b/src/log.c @@ -0,0 +1,47 @@ +/* copyright 2012 - 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ + +#include "log.h" + +#include + +static const char *log_level_to_string(GLogLevelFlags level) +{ + switch (level) { + case G_LOG_LEVEL_ERROR: return "ERROR"; + case G_LOG_LEVEL_CRITICAL: return "CRITICAL"; + case G_LOG_LEVEL_WARNING: return "WARNING"; + case G_LOG_LEVEL_MESSAGE: return "MESSAGE"; + case G_LOG_LEVEL_INFO: return "INFO"; + case G_LOG_LEVEL_DEBUG: return "DEBUG"; + default: return "UNKNOWN"; + } +} + +/* + * Log handling function for GLib's logging wrapper + */ +static void dunst_log_handler( + const gchar *log_domain, + GLogLevelFlags message_level, + const gchar *message, + gpointer user_data) +{ + const char *log_level_str = + log_level_to_string(message_level & G_LOG_LEVEL_MASK); + + /* Use stderr for warnings and higher */ + if (message_level <= G_LOG_LEVEL_WARNING) + g_printerr("%s: %s\n", log_level_str, message); + else + g_print("%s: %s\n", log_level_str, message); +} + +/* + * Initialise log handling. Can be called any time. + */ +void dunst_log_init(void) +{ + g_log_set_default_handler(dunst_log_handler, NULL); +} + +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..bad4c80 --- /dev/null +++ b/src/log.h @@ -0,0 +1,20 @@ +/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ + +#include +#include +#include + +#ifndef DUNST_LOG_H +#define DUNST_LOG_H + +#define LOG_E g_error +#define LOG_C g_critical +#define LOG_W g_warning +#define LOG_M g_message +#define LOG_I g_info +#define LOG_D g_debug + +void dunst_log_init(void); + +#endif +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */