diff --git a/glib/gmessages.c b/glib/gmessages.c index 76077d004..2ac98f3b9 100644 --- a/glib/gmessages.c +++ b/glib/gmessages.c @@ -501,12 +501,14 @@ struct _GLogHandler GLogHandler *next; }; +static void g_default_print_func (const gchar *string); +static void g_default_printerr_func (const gchar *string); /* --- variables --- */ static GMutex g_messages_lock; static GLogDomain *g_log_domains = NULL; -static GPrintFunc glib_print_func = NULL; -static GPrintFunc glib_printerr_func = NULL; +static GPrintFunc glib_print_func = g_default_print_func; +static GPrintFunc glib_printerr_func = g_default_printerr_func; static GPrivate g_log_depth; static GPrivate g_log_structured_depth; static GLogFunc default_log_func = g_log_default_handler; @@ -3295,9 +3297,11 @@ g_log_default_handler (const gchar *log_domain, /** * g_set_print_handler: - * @func: the new print handler + * @func: (nullable): the new print handler or %NULL to + * reset to the default * - * Sets the print handler. + * Sets the print handler to @func, or resets it to the + * default GLib handler if %NULL. * * Any messages passed to g_print() will be output via * the new handler. The default handler simply outputs @@ -3305,7 +3309,14 @@ g_log_default_handler (const gchar *log_domain, * you can redirect the output, to a GTK+ widget or a * log file for example. * - * Returns: the old print handler + * Since 2.76 this functions always returns a valid + * #GPrintFunc, and never returns %NULL. If no custom + * print handler was set, it will return the GLib + * default print handler and that can be re-used to + * decorate its output and/or to write to stderr + * in all platforms. Before GLib 2.76, this was %NULL. + * + * Returns: (not nullable): the old print handler */ GPrintFunc g_set_print_handler (GPrintFunc func) @@ -3314,7 +3325,7 @@ g_set_print_handler (GPrintFunc func) g_mutex_lock (&g_messages_lock); old_print_func = glib_print_func; - glib_print_func = func; + glib_print_func = func ? func : g_default_print_func; g_mutex_unlock (&g_messages_lock); return old_print_func; @@ -3349,6 +3360,18 @@ print_string (FILE *stream, fflush (stream); } +static void +g_default_print_func (const gchar *string) +{ + print_string (stdout, string); +} + +static void +g_default_printerr_func (const gchar *string) +{ + print_string (stderr, string); +} + /** * g_print: * @format: the message format. See the printf() documentation @@ -3383,19 +3406,17 @@ g_print (const gchar *format, local_glib_print_func = glib_print_func; g_mutex_unlock (&g_messages_lock); - if (local_glib_print_func) - local_glib_print_func (string); - else - print_string (stdout, string); - + local_glib_print_func (string); g_free (string); } /** * g_set_printerr_handler: - * @func: the new error message handler + * @func: (nullable): he new error message handler or %NULL + * to reset to the default * - * Sets the handler for printing error messages. + * Sets the handler for printing error messages to @func, + * or resets it to the default GLib handler if %NULL. * * Any messages passed to g_printerr() will be output via * the new handler. The default handler simply outputs the @@ -3403,7 +3424,14 @@ g_print (const gchar *format, * redirect the output, to a GTK+ widget or a log file for * example. * - * Returns: the old error message handler + * Since 2.76 this functions always returns a valid + * #GPrintFunc, and never returns %NULL. If no custom error + * print handler was set, it will return the GLib default + * error print handler and that can be re-used to decorate + * its output and/or to write to stderr in all platforms. + * Before GLib 2.76, this was %NULL. + * + * Returns: (not nullable): the old error message handler */ GPrintFunc g_set_printerr_handler (GPrintFunc func) @@ -3412,7 +3440,7 @@ g_set_printerr_handler (GPrintFunc func) g_mutex_lock (&g_messages_lock); old_printerr_func = glib_printerr_func; - glib_printerr_func = func; + glib_printerr_func = func ? func : g_default_printerr_func; g_mutex_unlock (&g_messages_lock); return old_printerr_func; @@ -3450,11 +3478,7 @@ g_printerr (const gchar *format, local_glib_printerr_func = glib_printerr_func; g_mutex_unlock (&g_messages_lock); - if (local_glib_printerr_func) - local_glib_printerr_func (string); - else - print_string (stderr, string); - + local_glib_printerr_func (string); g_free (string); } diff --git a/glib/tests/logging.c b/glib/tests/logging.c index 964451422..201a8f99f 100644 --- a/glib/tests/logging.c +++ b/glib/tests/logging.c @@ -3,6 +3,12 @@ #define G_LOG_USE_STRUCTURED 1 #include +#ifdef G_OS_WIN32 +#define LINE_END "\r\n" +#else +#define LINE_END "\n" +#endif + /* Test g_warn macros */ static void test_warnings (void) @@ -362,13 +368,25 @@ test_print_handler (void) GPrintFunc old_print_handler; old_print_handler = g_set_print_handler (my_print_handler); - g_assert (old_print_handler == NULL); + g_assert_nonnull (old_print_handler); my_print_count = 0; g_print ("bu ba"); g_assert_cmpint (my_print_count, ==, 1); g_set_print_handler (NULL); + + if (g_test_subprocess ()) + { + old_print_handler ("default handler\n"); + g_print ("bu ba\n"); + return; + } + + g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT); + g_test_trap_assert_stdout ("*default handler" LINE_END "*"); + g_test_trap_assert_stdout ("*bu ba" LINE_END "*"); + g_test_trap_has_passed (); } static void @@ -377,13 +395,25 @@ test_printerr_handler (void) GPrintFunc old_printerr_handler; old_printerr_handler = g_set_printerr_handler (my_print_handler); - g_assert (old_printerr_handler == NULL); + g_assert_nonnull (old_printerr_handler); my_print_count = 0; g_printerr ("bu ba"); g_assert_cmpint (my_print_count, ==, 1); g_set_printerr_handler (NULL); + + if (g_test_subprocess ()) + { + old_printerr_handler ("default handler\n"); + g_printerr ("bu ba\n"); + return; + } + + g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT); + g_test_trap_assert_stderr ("*default handler" LINE_END "*"); + g_test_trap_assert_stderr ("*bu ba" LINE_END "*"); + g_test_trap_has_passed (); } static char *fail_str = "foo";