bolt/fix-avoid-multiple-g_log_set_writer_func-calls.patch

440 lines
12 KiB
Diff

diff -Nur a/tests/test-logging.c b/tests/test-logging.c
--- a/tests/test-logging.c 2021-07-08 09:26:32.000000000 +0800
+++ b/tests/test-logging.c 2023-02-08 15:22:41.792643172 +0800
@@ -38,6 +38,34 @@
#include <stdlib.h>
#include <stdio.h>
+
+typedef struct _TestContext
+{
+ GLogWriterFunc logger;
+ gpointer logger_data;
+} TestContext;
+
+static GLogWriterOutput
+test_context_logger (GLogLevelFlags log_level,
+ const GLogField *fields,
+ gsize n_fields,
+ gpointer user_data)
+{
+ TestContext *ctx = user_data;
+
+ g_assert_nonnull (ctx);
+ g_assert_nonnull (ctx->logger);
+
+ return ctx->logger (log_level, fields, n_fields, ctx->logger_data);
+}
+
+static void
+test_context_set_logger (TestContext *ctx, GLogWriterFunc logger, gpointer user_data)
+{
+ ctx->logger = logger;
+ ctx->logger_data = user_data;
+}
+
typedef struct _LogData
{
GLogLevelFlags level;
@@ -52,8 +80,12 @@
static void
test_log_setup (TestLog *tt, gconstpointer user_data)
{
+ TestContext *ctx = (TestContext *) user_data;
+
+ /* reset logger */
+ test_context_set_logger (ctx, g_log_writer_standard_streams, NULL);
+
tt->data.fields = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
- g_log_set_writer_func (g_log_writer_standard_streams, NULL, NULL);
}
static void
@@ -149,9 +181,11 @@
static void
test_log_basic (TestLog *tt, gconstpointer user_data)
{
+ TestContext *ctx = (TestContext *) user_data;
+
log_expect (tt, G_LOG_LEVEL_MESSAGE, "bolt-test", "test", NULL);
- g_log_set_writer_func (test_writer, &tt->data, NULL);
+ test_context_set_logger (ctx, test_writer, &tt->data);
bolt_log ("bolt-test", G_LOG_LEVEL_MESSAGE, "test");
g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_ERROR));
@@ -175,13 +209,14 @@
g_autoptr(GError) error = NULL;
const char *domain = "bolt-gerror";
GLogLevelFlags lvl = G_LOG_LEVEL_INFO;
+ TestContext *ctx = user_data;
const char *msg;
msg = "no udev";
g_set_error_literal (&error, BOLT_ERROR, BOLT_ERROR_UDEV, msg);
log_expect (tt, lvl, domain, NULL, "ERROR_MESSAGE", msg, NULL);
- g_log_set_writer_func (test_writer, &tt->data, NULL);
+ test_context_set_logger (ctx, test_writer, &tt->data);
bolt_log (domain, lvl, LOG_ERR (error), NULL);
/* check we handle NULL GErrors without crashing */
@@ -199,6 +234,7 @@
const char *msg;
GLogLevelFlags lvl;
const char *uid_a = "fbc83890-e9bf-45e5-a777-b3728490989c";
+ TestContext *ctx = user_data;
a = g_object_new (BOLT_TYPE_DEVICE,
"uid", uid_a,
@@ -213,7 +249,8 @@
BOLT_LOG_DEVICE_UID, uid_a,
NULL);
- g_log_set_writer_func (test_writer, &tt->data, NULL);
+ test_context_set_logger (ctx, test_writer, &tt->data);
+
bolt_log (domain, lvl, LOG_DEV (a), msg);
}
@@ -222,10 +259,11 @@
{
g_autoptr(GError) error = NULL;
GLogLevelFlags lvl = G_LOG_LEVEL_INFO;
+ TestContext *ctx = user_data;
const char *msg = "da steht ich nun ich armer test";
- g_log_set_writer_func (test_writer, &tt->data, NULL);
+ test_context_set_logger (ctx, test_writer, &tt->data);
log_expect (tt, G_LOG_LEVEL_MESSAGE, G_LOG_DOMAIN, msg, NULL);
bolt_msg (msg);
@@ -257,10 +295,10 @@
}
static GLogWriterOutput
-test_log_logger_stdstream (GLogLevelFlags level,
- const GLogField *fields,
- gsize n_fields,
- gpointer user_data)
+test_logger_stdstream (GLogLevelFlags level,
+ const GLogField *fields,
+ gsize n_fields,
+ gpointer user_data)
{
g_autoptr(BoltLogCtx) ctx = NULL;
@@ -276,10 +314,10 @@
}
static GLogWriterOutput
-test_log_logger_journal (GLogLevelFlags level,
- const GLogField *fields,
- gsize n_fields,
- gpointer user_data)
+test_logger_journal (GLogLevelFlags level,
+ const GLogField *fields,
+ gsize n_fields,
+ gpointer user_data)
{
g_autoptr(BoltLogCtx) ctx = NULL;
char message[2048] = {0, };
@@ -304,23 +342,24 @@
}
static void
-test_log_logger (TestLog *tt, gconstpointer user_data)
+check_log_logger (TestLog *tt, gconstpointer user_data, gboolean journal)
{
g_autoptr(GError) err = NULL;
const char *msg = NULL;
const char *uid1 = "884c6edd-7118-4b21-b186-b02d396ecca0";
const char *uid2 = "884c6ede-7118-4b21-b186-b02d396ecca0";
const char *uid3 = "884c6edf-7118-4b21-b186-b02d396ecca0";
+ TestContext *ctx = (TestContext *) user_data;
if (g_test_subprocess ())
{
g_autoptr(BoltDomain) dom = NULL;
g_autoptr(BoltDevice) dev = NULL;
- if (GPOINTER_TO_INT (user_data) == 1)
- g_log_set_writer_func (test_log_logger_journal, tt, NULL);
+ if (journal)
+ test_context_set_logger (ctx, test_logger_journal, tt);
else
- g_log_set_writer_func (test_log_logger_stdstream, tt, NULL);
+ test_context_set_logger (ctx, test_logger_stdstream, tt);
dom = g_object_new (BOLT_TYPE_DOMAIN,
"id", "domain0",
@@ -379,57 +418,71 @@
}
}
+static void
+test_log_logger_stdstream (TestLog *tt, gconstpointer user_data)
+{
+ check_log_logger (tt, user_data, FALSE);
+}
+
+static void
+test_log_logger_journal (TestLog *tt, gconstpointer user_data)
+{
+ check_log_logger (tt, user_data, TRUE);
+}
int
main (int argc, char **argv)
{
+ TestContext test_ctx;
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
+ g_log_set_writer_func (test_context_logger, &test_ctx, NULL);
+
bolt_dbus_ensure_resources ();
g_test_add ("/logging/basic",
TestLog,
- NULL,
+ &test_ctx,
test_log_setup,
test_log_basic,
test_log_tear_down);
g_test_add ("/logging/gerror",
TestLog,
- NULL,
+ &test_ctx,
test_log_setup,
test_log_gerror,
test_log_tear_down);
g_test_add ("/logging/device",
TestLog,
- NULL,
+ &test_ctx,
test_log_setup,
test_log_device,
test_log_tear_down);
g_test_add ("/logging/macros",
TestLog,
- NULL,
+ &test_ctx,
test_log_setup,
test_log_macros,
test_log_tear_down);
g_test_add ("/logging/logger/stdstream",
TestLog,
- GINT_TO_POINTER (0),
+ &test_ctx,
test_log_setup,
- test_log_logger,
+ test_log_logger_stdstream,
test_log_tear_down);
g_test_add ("/logging/logger/journal",
TestLog,
- GINT_TO_POINTER (1),
+ &test_ctx,
test_log_setup,
- test_log_logger,
+ test_log_logger_journal,
test_log_tear_down);
return g_test_run ();
diff -Nur a/tests/test-store.c b/tests/test-store.c
--- a/tests/test-store.c 2021-07-08 09:26:32.000000000 +0800
+++ b/tests/test-store.c 2023-02-08 15:25:32.867123067 +0800
@@ -47,13 +47,50 @@
{
char *path;
BoltStore *store;
+
} TestStore;
+typedef struct
+{
+ GLogWriterFunc logger;
+} TestContext;
+
+static void
+test_context_set_logger (TestContext *data, GLogWriterFunc logger)
+{
+ data->logger = logger;
+}
+
+static GLogWriterOutput
+test_context_logger (GLogLevelFlags log_level,
+ const GLogField *fields,
+ gsize n_fields,
+ gpointer user_data)
+{
+ const TestContext *data = user_data;
+
+ return data->logger (log_level, fields, n_fields, NULL);
+}
+
+static GLogWriterOutput
+null_logger (GLogLevelFlags log_level,
+ const GLogField *fields,
+ gsize n_fields,
+ gpointer user_data)
+{
+ return G_LOG_WRITER_HANDLED;
+}
static void
test_store_setup (TestStore *tt, gconstpointer user_data)
{
g_autoptr(GError) error = NULL;
+ TestContext *ctx = (TestContext *) user_data;
+
+ /* reset logger */
+ test_context_set_logger (ctx, g_log_writer_default);
+
+ ctx->logger = g_log_writer_default;
tt->path = g_dir_make_tmp ("bolt.auth.XXXXXX",
&error);
@@ -75,7 +112,6 @@
}
g_debug ("store at '%s'", tt->path);
-
}
static void
@@ -444,16 +480,6 @@
}
}
-static GLogWriterOutput
-null_logger (GLogLevelFlags log_level,
- const GLogField *fields,
- gsize n_fields,
- gpointer user_data)
-{
- return G_LOG_WRITER_HANDLED;
-}
-
-
static void
test_store_invalid_data (TestStore *tt, gconstpointer user_data)
{
@@ -463,6 +489,7 @@
g_autoptr(GError) err = NULL;
g_autoptr(BoltDevice) dev = NULL;
static const char *uid = "399d33cb-c9cf-4273-8f92-9445437e0b43";
+ TestContext *ctx = (TestContext *) user_data;
gboolean ok;
int r;
@@ -475,9 +502,9 @@
g_assert_no_error (err);
g_assert_true (ok);
- g_log_set_writer_func (null_logger, NULL, NULL);
+ test_context_set_logger (ctx, null_logger);
dev = bolt_store_get_device (tt->store, uid, &err);
- g_log_set_writer_func (g_log_writer_default, NULL, NULL);
+ test_context_set_logger (ctx, g_log_writer_default);
g_assert_null (dev);
g_assert_error (err, BOLT_ERROR, BOLT_ERROR_FAILED);
@@ -905,75 +932,79 @@
int
main (int argc, char **argv)
{
+ TestContext test_context;
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
+ g_log_set_writer_func (test_context_logger, &test_context, NULL);
+
bolt_dbus_ensure_resources ();
g_test_add ("/daemon/key",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_key,
test_store_tear_down);
g_test_add ("/daemon/store/basic",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_basic,
test_store_tear_down);
g_test_add ("/daemon/store/update",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_update,
test_store_tear_down);
g_test_add ("/daemon/store/config",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_config,
test_store_tear_down);
g_test_add ("/daemon/store/invalid_data",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_invalid_data,
test_store_tear_down);
g_test_add ("/daemon/store/times",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_times,
test_store_tear_down);
g_test_add ("/daemon/store/domain",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_domain,
test_store_tear_down);
g_test_add ("/daemon/store/journal",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_journal,
test_store_tear_down);
g_test_add ("/daemon/store/upgrade",
TestStore,
- NULL,
+ &test_context,
test_store_setup,
test_store_upgrade,
test_store_tear_down);
return g_test_run ();
}
+