gmessages: Error if g_log_set_writer_func() is called multiple times

Inspired by https://gitlab.gnome.org/GNOME/glib/-/issues/2638.

This requires moving the tests to subprocesses, so that
`g_log_set_writer_func()` is only called once per process.

It also adds a test for the new error.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-04-25 12:23:45 +01:00
parent cc99d6ee97
commit dfb3517d37
2 changed files with 216 additions and 109 deletions

View File

@ -2059,9 +2059,18 @@ g_log_set_writer_func (GLogWriterFunc func,
g_return_if_fail (func != NULL);
g_mutex_lock (&g_messages_lock);
if (log_writer_func != g_log_writer_default)
{
g_mutex_unlock (&g_messages_lock);
g_error ("g_log_set_writer_func() called multiple times");
return;
}
log_writer_func = func;
log_writer_user_data = user_data;
log_writer_user_data_free = user_data_free;
g_mutex_unlock (&g_messages_lock);
}

View File

@ -445,6 +445,10 @@ expect_log_writer (GLogLevelFlags log_level,
static void
test_structured_logging_no_state (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
gpointer some_pointer = GUINT_TO_POINTER (0x100);
guint some_integer = 123;
@ -458,11 +462,21 @@ test_structured_logging_no_state (void)
some_pointer, some_integer);
g_assert_cmpint (log_count, ==, 1);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_some_state (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
gpointer state_object = NULL; /* this must not be dereferenced */
const GLogField fields[] = {
{ "MESSAGE", "This is a debug message.", -1 },
@ -477,11 +491,21 @@ test_structured_logging_some_state (void)
g_log_structured_array (G_LOG_LEVEL_DEBUG, fields, G_N_ELEMENTS (fields));
g_assert_cmpint (log_count, ==, 1);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_robustness (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
log_count = 0;
g_log_set_writer_func (null_log_writer, NULL, NULL);
@ -489,12 +513,22 @@ test_structured_logging_robustness (void)
g_log (NULL, G_LOG_LEVEL_MESSAGE, "Test");
g_log_structured (NULL, G_LOG_LEVEL_MESSAGE, "MESSAGE", "Test");
g_assert_cmpint (log_count, ==, 1);
g_assert_cmpint (log_count, ==, 2);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_roundtrip1 (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
gpointer some_pointer = GUINT_TO_POINTER (0x100);
gint some_integer = 123;
gchar message[200];
@ -531,11 +565,21 @@ test_structured_logging_roundtrip1 (void)
g_test_fail_printf ("Unexpected message: %s", str);
g_free (str);
}
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_roundtrip2 (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
const gchar *some_string = "abc";
const GLogField fields[] = {
{ "GLIB_DOMAIN", "some-domain", -1 },
@ -556,11 +600,21 @@ test_structured_logging_roundtrip2 (void)
some_string);
g_assert (expected_messages == NULL);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_roundtrip3 (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
const GLogField fields[] = {
{ "GLIB_DOMAIN", "some-domain", -1 },
{ "PRIORITY", "4", -1 },
@ -575,6 +629,12 @@ test_structured_logging_roundtrip3 (void)
"MESSAGE", "Test test test.");
g_assert (expected_messages == NULL);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static GVariant *
@ -597,6 +657,10 @@ create_variant_fields (void)
static void
test_structured_logging_variant1 (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
GVariant *v = create_variant_fields ();
log_count = 0;
@ -605,11 +669,21 @@ test_structured_logging_variant1 (void)
g_log_variant ("some-domain", G_LOG_LEVEL_MESSAGE, v);
g_variant_unref (v);
g_assert_cmpint (log_count, ==, 1);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_variant2 (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func(), which
* can only be called once per process. */
if (g_test_subprocess ())
{
const GLogField fields[] = {
{ "GLIB_DOMAIN", "some-domain", -1 },
{ "PRIORITY", "5", -1 },
@ -627,6 +701,29 @@ test_structured_logging_variant2 (void)
g_log_variant ("some-domain", G_LOG_LEVEL_MESSAGE, v);
g_variant_unref (v);
g_assert (expected_messages == NULL);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static void
test_structured_logging_set_writer_func_twice (void)
{
/* Test has to run in a subprocess as it calls g_log_set_writer_func() and
* causes an error. */
if (g_test_subprocess ())
{
g_log_set_writer_func (null_log_writer, NULL, NULL);
g_log_set_writer_func (expect_log_writer, NULL, NULL);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_failed ();
}
}
int
@ -663,6 +760,7 @@ main (int argc, char *argv[])
g_test_add_func ("/structured-logging/roundtrip3", test_structured_logging_roundtrip3);
g_test_add_func ("/structured-logging/variant1", test_structured_logging_variant1);
g_test_add_func ("/structured-logging/variant2", test_structured_logging_variant2);
g_test_add_func ("/structured-logging/set-writer-func-twice", test_structured_logging_set_writer_func_twice);
return g_test_run ();
}