From 2c503c29847ef9a2c3ecac85f85682e356c53c84 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Thu, 7 Dec 2017 20:35:20 +0100 Subject: [PATCH] Add option to suppress log output when testing --- src/dunst.c | 2 +- src/log.c | 14 +++++++++++--- src/log.h | 2 +- test/test.c | 7 +++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/dunst.c b/src/dunst.c index 3998eeb..297784b 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -129,7 +129,7 @@ int dunst_main(int argc, char *argv[]) cmdline_load(argc, argv); - dunst_log_init(); + dunst_log_init(false); if (cmdline_get_bool("-v/-version", false, "Print version") || cmdline_get_bool("--version", false, "Print version")) { diff --git a/src/log.c b/src/log.c index e3b8594..a99b7fe 100644 --- a/src/log.c +++ b/src/log.c @@ -19,13 +19,18 @@ static const char *log_level_to_string(GLogLevelFlags level) /* * Log handling function for GLib's logging wrapper + * + * If the gpointer is valid, do not do anything */ static void dunst_log_handler( const gchar *log_domain, GLogLevelFlags message_level, const gchar *message, - gpointer user_data) + gpointer testing) { + if (testing) + return; + const char *log_level_str = log_level_to_string(message_level & G_LOG_LEVEL_MASK); @@ -38,10 +43,13 @@ static void dunst_log_handler( /* * Initialise log handling. Can be called any time. + * + * If bool is %TRUE, it suppresses all logging output. + * Primarily used for testing */ -void dunst_log_init(void) +void dunst_log_init(bool testing) { - g_log_set_default_handler(dunst_log_handler, NULL); + g_log_set_default_handler(dunst_log_handler, (void*)testing); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/log.h b/src/log.h index bad4c80..55140a7 100644 --- a/src/log.h +++ b/src/log.h @@ -14,7 +14,7 @@ #define LOG_I g_info #define LOG_D g_debug -void dunst_log_init(void); +void dunst_log_init(bool testing); #endif /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/test.c b/test/test.c index 90959a6..935bda9 100644 --- a/test/test.c +++ b/test/test.c @@ -1,5 +1,9 @@ #include "greatest.h" +#include + +#include "src/log.h" + SUITE_EXTERN(suite_utils); SUITE_EXTERN(suite_option_parser); SUITE_EXTERN(suite_notification); @@ -8,6 +12,9 @@ SUITE_EXTERN(suite_markup); GREATEST_MAIN_DEFS(); int main(int argc, char *argv[]) { + // do not print out warning messages, when executing tests + dunst_log_init(true); + GREATEST_MAIN_BEGIN(); RUN_SUITE(suite_utils); RUN_SUITE(suite_option_parser);