Skip to content

Commit 9b21492

Browse files
committed
Improve logging code
1 parent ad210bb commit 9b21492

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

config.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ DEFAULT_CPPFLAGS = -Wno-gnu-zero-variadic-macro-arguments -D_DEFAULT_SOURCE -DVE
5555
DEFAULT_CFLAGS = -g -std=gnu11 -pedantic -Wall -Wno-overlength-strings -Os ${EXTRA_CFLAGS}
5656
DEFAULT_LDFLAGS = -lm -lrt
5757

58-
CPPFLAGS_DEBUG := -DDEBUG_BUILD
58+
CPPFLAGS_DEBUG :=
5959
CFLAGS_DEBUG := -O0
6060
LDFLAGS_DEBUG :=
6161

src/dunst.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ int dunst_main(int argc, char *argv[])
251251

252252
cmdline_load(argc, argv);
253253

254-
dunst_log_init(false);
254+
dunst_log_init(DUNST_LOG_AUTO);
255255

256256
if (cmdline_get_bool("-v/-version/--version", false, "Print version")) {
257257
print_version();

src/log.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
// NOTE: Keep updated with the dunst manual
1515
static GLogLevelFlags log_level = G_LOG_LEVEL_MESSAGE;
1616

17+
static enum log_mask log_mask = DUNST_LOG_AUTO;
18+
1719
/* see log.h */
1820
static const char *log_level_to_string(GLogLevelFlags level)
1921
{
2022
switch (level) {
21-
case G_LOG_LEVEL_ERROR: return "ERROR";
22-
case G_LOG_LEVEL_CRITICAL: return "CRITICAL";
23-
case G_LOG_LEVEL_WARNING: return "WARNING";
24-
case G_LOG_LEVEL_MESSAGE: return "MESSAGE";
25-
case G_LOG_LEVEL_INFO: return "INFO";
26-
case G_LOG_LEVEL_DEBUG: return "DEBUG";
27-
default: return "UNKNOWN";
23+
case G_LOG_LEVEL_ERROR: return "ERROR";
24+
case G_LOG_LEVEL_CRITICAL: return "CRITICAL";
25+
case G_LOG_LEVEL_WARNING: return "WARNING";
26+
case G_LOG_LEVEL_MESSAGE: return "MESSAGE";
27+
case G_LOG_LEVEL_INFO: return "INFO";
28+
case G_LOG_LEVEL_DEBUG: return "DEBUG";
29+
default: return "UNKNOWN";
2830
}
2931
}
3032

@@ -66,27 +68,22 @@ void log_set_level(GLogLevelFlags level)
6668
* @param log_domain Used only by GLib
6769
* @param message_level Used only by GLib
6870
* @param message Used only by GLib
69-
* @param testing If not `NULL` (here: `true`), do nothing
71+
* @param ignore
7072
*/
7173
static void dunst_log_handler(
7274
const gchar *log_domain,
7375
GLogLevelFlags message_level,
7476
const gchar *message,
75-
gpointer testing)
77+
gpointer ignore)
7678
{
7779
(void)log_domain;
7880

79-
if (testing)
80-
log_level = G_LOG_LEVEL_ERROR;
81-
8281
GLogLevelFlags message_level_masked = message_level & G_LOG_LEVEL_MASK;
8382

84-
/* if you want to have a debug build, you want to log anything,
85-
* unconditionally, without specifying debug log level again */
86-
#ifndef DEBUG_BUILD
87-
if (log_level < message_level_masked)
83+
if (log_mask == DUNST_LOG_NONE ||
84+
(log_mask == DUNST_LOG_AUTO && log_level < message_level_masked))
8885
return;
89-
#endif
86+
9087
const char *log_level_str =
9188
log_level_to_string(message_level_masked);
9289

@@ -98,9 +95,10 @@ static void dunst_log_handler(
9895
}
9996

10097
/* see log.h */
101-
void dunst_log_init(bool testing)
98+
void dunst_log_init(enum log_mask mask)
10299
{
103-
g_log_set_default_handler(dunst_log_handler, (void*)testing);
100+
log_mask = mask;
101+
g_log_set_default_handler(dunst_log_handler, NULL);
104102
}
105103

106104
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

src/log.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
#define MSG_FOPEN_SUCCESS(path, fp) "Opened '%s' (fd: '%d')", path, fileno(fp)
4747
#define MSG_FOPEN_FAILURE(path) "Cannot open '%s': %s", path, strerror(errno)
4848

49+
enum log_mask {
50+
DUNST_LOG_NONE,
51+
DUNST_LOG_ALL,
52+
DUNST_LOG_AUTO,
53+
};
54+
4955
/**
5056
* Set the current loglevel to `level`
5157
*
@@ -69,10 +75,10 @@ void log_set_level_from_string(const char* level);
6975
/**
7076
* Initialise log handling. Can be called any time.
7177
*
72-
* @param testing If we're in testing mode and should
73-
* suppress all output
78+
* @param mask If we're in testing mode and should
79+
* suppress all output or show all
7480
*/
75-
void dunst_log_init(bool testing);
81+
void dunst_log_init(enum log_mask mask);
7682

7783
#endif
7884
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */

test/test.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ int main(int argc, char *argv[]) {
3737
base = realpath(base ? base : "./test", NULL);
3838

3939
/* By default do not print out warning messages, when executing tests.
40-
* But allow, if DUNST_TEST_LOG=1 is set in environment. */
40+
* But if DUNST_TEST_LOG=1 is set in environment, print everything. */
4141
const char *log = getenv("DUNST_TEST_LOG");
42-
bool printlog = log && atoi(log) ? true : false;
43-
dunst_log_init(!printlog);
44-
42+
enum log_mask printlog = (log && atoi(log)) ? DUNST_LOG_ALL : DUNST_LOG_NONE;
43+
dunst_log_init(printlog);
4544

4645
// initialize settings
4746
char **configs = g_malloc0(2 * sizeof(char *));

0 commit comments

Comments
 (0)