Add GLib logging capabilities

This commit is contained in:
Benedikt Heine 2017-12-07 20:15:54 +01:00
parent f7cf5b6f5b
commit cad2ac34f9
3 changed files with 70 additions and 0 deletions

View File

@ -13,6 +13,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "dbus.h" #include "dbus.h"
#include "log.h"
#include "menu.h" #include "menu.h"
#include "notification.h" #include "notification.h"
#include "option_parser.h" #include "option_parser.h"
@ -128,6 +129,8 @@ int dunst_main(int argc, char *argv[])
cmdline_load(argc, argv); cmdline_load(argc, argv);
dunst_log_init();
if (cmdline_get_bool("-v/-version", false, "Print version") if (cmdline_get_bool("-v/-version", false, "Print version")
|| cmdline_get_bool("--version", false, "Print version")) { || cmdline_get_bool("--version", false, "Print version")) {
print_version(); print_version();

47
src/log.c Normal file
View File

@ -0,0 +1,47 @@
/* copyright 2012 - 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#include "log.h"
#include <glib.h>
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: */

20
src/log.h Normal file
View File

@ -0,0 +1,20 @@
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#include <glib.h>
#include <stdbool.h>
#include <stdlib.h>
#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: */