Merge branch 'add-g-log-get-always-fatal' into 'main'

gmessages: Add 'g_log_get_always_fatal()' for use in custom log writers

Closes #2544

See merge request GNOME/glib!4546
This commit is contained in:
Philip Withnall
2025-05-06 12:15:08 +00:00
4 changed files with 63 additions and 6 deletions

View File

@@ -140,7 +140,7 @@ volatile gboolean glib_on_error_halt = TRUE;
* This function may cause different actions on non-UNIX platforms.
*
* On Windows consider using the `G_DEBUGGER` environment
* variable (see [Running GLib Applications](glib-running.html)) and
* variable (see [Running GLib Applications](running.html)) and
* calling g_on_error_stack_trace() instead.
*/
void
@@ -243,7 +243,7 @@ g_on_error_query (const gchar *prg_name)
* g_on_error_query(). If called directly, it will raise an
* exception, which will crash the program. If the `G_DEBUGGER` environment
* variable is set, a debugger will be invoked to attach and
* handle that exception (see [Running GLib Applications](glib-running.html)).
* handle that exception (see [Running GLib Applications](running.html)).
*/
void
g_on_error_stack_trace (const gchar *prg_name)

View File

@@ -219,7 +219,7 @@
*
* You can make warnings fatal at runtime by setting the `G_DEBUG`
* environment variable (see
* [Running GLib Applications](glib-running.html)):
* [Running GLib Applications](running.html)):
*
* ```
* G_DEBUG=fatal-warnings gdb ./my-program
@@ -254,7 +254,7 @@
*
* You can make critical warnings fatal at runtime by
* setting the `G_DEBUG` environment variable (see
* [Running GLib Applications](glib-running.html)):
* [Running GLib Applications](running.html)):
*
* ```
* G_DEBUG=fatal-warnings gdb ./my-program
@@ -557,6 +557,48 @@ g_log_domain_get_handler_L (GLogDomain *domain,
return default_log_func;
}
/**
* g_log_get_always_fatal:
*
* Gets the current fatal mask.
*
* This is mostly used by custom log writers to make fatal messages
* (`fatal-warnings`, `fatal-criticals`) work as expected, when using the
* `G_DEBUG` environment variable (see [Running GLib Applications](running.html)).
*
* An example usage is shown below:
*
* ```c
* static GLogWriterOutput
* my_custom_log_writer_fn (GLogLevelFlags log_level,
* const GLogField *fields,
* gsize n_fields,
* gpointer user_data)
* {
*
* // abort if the message was fatal
* if (log_level & g_log_get_always_fatal ())
* g_abort ();
*
* // custom log handling code
* ...
* ...
*
* // success
* return G_LOG_WRITER_HANDLED;
* }
* ```
*
* Returns: the current fatal mask
*
* Since: 2.86
*/
GLogLevelFlags
g_log_get_always_fatal (void)
{
return g_log_always_fatal;
}
/**
* g_log_set_always_fatal:
* @fatal_mask: the mask containing bits set for each level of error which is
@@ -570,7 +612,7 @@ g_log_domain_get_handler_L (GLogDomain *domain,
*
* You can also make some message levels fatal at runtime by setting
* the `G_DEBUG` environment variable (see
* [Running GLib Applications](glib-running.html)).
* [Running GLib Applications](running.html)).
*
* Libraries should not call this function, as it affects all messages logged
* by a process, including those from other libraries.

View File

@@ -117,6 +117,9 @@ GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
GLIB_AVAILABLE_IN_ALL
GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
GLIB_AVAILABLE_IN_2_86
GLogLevelFlags g_log_get_always_fatal (void);
/* Structured logging mechanism. */
/**
@@ -587,7 +590,7 @@ GPrintFunc g_set_printerr_handler (GPrintFunc func);
*
* To debug failure of a g_return_if_fail() check, run the code under a debugger
* with `G_DEBUG=fatal-criticals` or `G_DEBUG=fatal-warnings` defined in the
* environment (see [Running GLib Applications](glib-running.html)):
* environment (see [Running GLib Applications](running.html)):
*
* |[
* G_DEBUG=fatal-warnings gdb ./my-program

View File

@@ -643,6 +643,17 @@ test_fatal_log_mask (void)
g_test_trap_assert_stdout_unmatched ("*fatal*");
}
static void
test_always_fatal (void)
{
GLogLevelFlags log_level;
log_level = G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING;
g_log_set_always_fatal (log_level);
g_assert_cmpint (g_log_get_always_fatal (), ==, log_level | G_LOG_LEVEL_ERROR);
}
static gint my_print_count = 0;
static void
my_print_handler (const gchar *text)
@@ -1168,6 +1179,7 @@ main (int argc, char *argv[])
g_test_add_func ("/logging/journald-handler", test_journald_handler);
g_test_add_func ("/logging/warnings", test_warnings);
g_test_add_func ("/logging/fatal-log-mask", test_fatal_log_mask);
g_test_add_func ("/logging/always-fatal", test_always_fatal);
g_test_add_func ("/logging/set-handler", test_set_handler);
g_test_add_func ("/logging/print-handler", test_print_handler);
g_test_add_func ("/logging/printerr-handler", test_printerr_handler);