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

@ -444,6 +444,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;
@ -459,9 +463,19 @@ test_structured_logging_no_state (void)
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[] = {
@ -478,9 +492,19 @@ test_structured_logging_some_state (void)
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,11 +513,21 @@ 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;
@ -532,9 +566,19 @@ test_structured_logging_roundtrip1 (void)
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[] = {
@ -557,9 +601,19 @@ test_structured_logging_roundtrip2 (void)
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 },
@ -576,6 +630,12 @@ test_structured_logging_roundtrip3 (void)
g_assert (expected_messages == NULL);
}
else
{
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_passed ();
}
}
static GVariant *
create_variant_fields (void)
@ -596,6 +656,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 ();
@ -606,9 +670,19 @@ test_structured_logging_variant1 (void)
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 },
@ -628,6 +702,29 @@ test_structured_logging_variant2 (void)
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
main (int argc, char *argv[])
@ -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 ();
}