1 /* copyright 2012 - 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
2
3 /**
4 * @file src/log.c
5 * @brief logging wrapper to use GLib's logging capabilities
6 */
7
8 #include "log.h"
9
10 #include <glib.h>
11
12 #include "utils.h"
13
14 static GLogLevelFlags log_level = G_LOG_LEVEL_WARNING;
15
16 /* see log.h */
log_level_to_string(GLogLevelFlags level)17 static const char *log_level_to_string(GLogLevelFlags level)
18 {
19 switch (level) {
20 case G_LOG_LEVEL_ERROR: return "ERROR";
21 case G_LOG_LEVEL_CRITICAL: return "CRITICAL";
22 case G_LOG_LEVEL_WARNING: return "WARNING";
23 case G_LOG_LEVEL_MESSAGE: return "MESSAGE";
24 case G_LOG_LEVEL_INFO: return "INFO";
25 case G_LOG_LEVEL_DEBUG: return "DEBUG";
26 default: return "UNKNOWN";
27 }
28 }
29
30 /* see log.h */
log_set_level_from_string(const char * level)31 void log_set_level_from_string(const char *level)
32 {
33 ASSERT_OR_RET(level,);
34
35 if (STR_CASEQ(level, "critical"))
36 log_level = G_LOG_LEVEL_CRITICAL;
37 else if (STR_CASEQ(level, "crit"))
38 log_level = G_LOG_LEVEL_CRITICAL;
39 else if (STR_CASEQ(level, "warning"))
40 log_level = G_LOG_LEVEL_WARNING;
41 else if (STR_CASEQ(level, "warn"))
42 log_level = G_LOG_LEVEL_WARNING;
43 else if (STR_CASEQ(level, "message"))
44 log_level = G_LOG_LEVEL_MESSAGE;
45 else if (STR_CASEQ(level, "mesg"))
46 log_level = G_LOG_LEVEL_MESSAGE;
47 else if (STR_CASEQ(level, "info"))
48 log_level = G_LOG_LEVEL_INFO;
49 else if (STR_CASEQ(level, "debug"))
50 log_level = G_LOG_LEVEL_DEBUG;
51 else if (STR_CASEQ(level, "deb"))
52 log_level = G_LOG_LEVEL_DEBUG;
53 else
54 LOG_W("Unknown log level: '%s'", level);
55 }
56
log_set_level(GLogLevelFlags level)57 void log_set_level(GLogLevelFlags level)
58 {
59 log_level = level;
60 }
61
62 /**
63 * Log handling function for GLib's logging wrapper
64 *
65 * @param log_domain Used only by GLib
66 * @param message_level Used only by GLib
67 * @param message Used only by GLib
68 * @param testing If not `NULL` (here: `true`), do nothing
69 */
dunst_log_handler(const gchar * log_domain,GLogLevelFlags message_level,const gchar * message,gpointer testing)70 static void dunst_log_handler(
71 const gchar *log_domain,
72 GLogLevelFlags message_level,
73 const gchar *message,
74 gpointer testing)
75 {
76 if (testing)
77 return;
78
79 /* if you want to have a debug build, you want to log anything,
80 * unconditionally, without specifying debug log level again */
81 #ifndef DEBUG_BUILD
82 if (log_level < message_level)
83 return;
84 #endif
85 const char *log_level_str =
86 log_level_to_string(message_level & G_LOG_LEVEL_MASK);
87
88 /* Use stderr for warnings and higher */
89 if (message_level <= G_LOG_LEVEL_WARNING)
90 g_printerr("%s: %s\n", log_level_str, message);
91 else
92 g_print("%s: %s\n", log_level_str, message);
93 }
94
95 /* see log.h */
dunst_log_init(bool testing)96 void dunst_log_init(bool testing)
97 {
98 g_log_set_default_handler(dunst_log_handler, (void*)testing);
99 }
100
101 /* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */
102