summaryrefslogtreecommitdiff
path: root/glib/tests/logging.c
blob: 02ddae945d9d291317bd1615ad2fbb694aae2a78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include <stdlib.h>
#include <string.h>
#define G_LOG_USE_STRUCTURED 1
#include <glib.h>

/* Test g_warn macros */
static void
test_warnings (void)
{
  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
                         "*test_warnings*should not be reached*");
  g_warn_if_reached ();
  g_test_assert_expected_messages ();

  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
                         "*test_warnings*runtime check failed*");
  g_warn_if_fail (FALSE);
  g_test_assert_expected_messages ();
}

static guint log_count = 0;

static void
log_handler (const gchar    *log_domain,
             GLogLevelFlags  log_level,
             const gchar    *message,
             gpointer        user_data)
{
  g_assert_cmpstr (log_domain, ==, "bu");
  g_assert_cmpint (log_level, ==, G_LOG_LEVEL_INFO);

  log_count++;
}

/* test that custom log handlers only get called for
 * their domain and level
 */
static void
test_set_handler (void)
{
  guint id;

  id = g_log_set_handler ("bu", G_LOG_LEVEL_INFO, log_handler, NULL);

  g_log ("bu", G_LOG_LEVEL_DEBUG, "message");
  g_log ("ba", G_LOG_LEVEL_DEBUG, "message");
  g_log ("bu", G_LOG_LEVEL_INFO, "message");
  g_log ("ba", G_LOG_LEVEL_INFO, "message");

  g_assert_cmpint (log_count, ==, 1);

  g_log_remove_handler ("bu", id);
}

static void
test_default_handler_error (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_error ("message1");
  exit (0);
}

static void
test_default_handler_critical (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_critical ("message2");
  exit (0);
}

static void
test_default_handler_warning (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_warning ("message3");
  exit (0);
}

static void
test_default_handler_message (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_message ("message4");
  exit (0);
}

static void
test_default_handler_info (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "message5");
  exit (0);
}

static void
test_default_handler_bar_info (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);

  g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);

  g_log ("bar", G_LOG_LEVEL_INFO, "message5");
  exit (0);
}

static void
test_default_handler_baz_debug (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);

  g_setenv ("G_MESSAGES_DEBUG", "foo bar baz", TRUE);

  g_log ("baz", G_LOG_LEVEL_DEBUG, "message6");
  exit (0);
}

static void
test_default_handler_debug (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);

  g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

  g_log ("foo", G_LOG_LEVEL_DEBUG, "6");
  g_log ("bar", G_LOG_LEVEL_DEBUG, "6");
  g_log ("baz", G_LOG_LEVEL_DEBUG, "6");

  exit (0);
}

static void
test_default_handler_debug_stderr (void)
{
  g_log_writer_default_set_prefer_stderr (TRUE);
  g_log_set_default_handler (g_log_default_handler, NULL);

  g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

  g_log ("foo", G_LOG_LEVEL_DEBUG, "6");
  g_log ("bar", G_LOG_LEVEL_DEBUG, "6");
  g_log ("baz", G_LOG_LEVEL_DEBUG, "6");

  exit (0);
}

static void
test_default_handler_would_drop (void)
{
  g_unsetenv ("G_MESSAGES_DEBUG");

  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
  g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
  g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
  g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));

  g_setenv ("G_MESSAGES_DEBUG", "bar baz", TRUE);

  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
  g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
  g_assert_true (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
  g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));

  g_setenv ("G_MESSAGES_DEBUG", "foo bar", TRUE);

  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
  g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));

  g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);

  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_ERROR, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_CRITICAL, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_WARNING, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_MESSAGE, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_INFO, "foo"));
  g_assert_false (g_log_writer_default_would_drop (G_LOG_LEVEL_DEBUG, "foo"));
  g_assert_false (g_log_writer_default_would_drop (1<<G_LOG_LEVEL_USER_SHIFT, "foo"));

  exit (0);
}

static void
test_default_handler_0x400 (void)
{
  g_log_set_default_handler (g_log_default_handler, NULL);
  g_log (G_LOG_DOMAIN, 1<<10, "message7");
  exit (0);
}

static void
test_default_handler (void)
{
  g_test_trap_subprocess ("/logging/default-handler/subprocess/error", 0, 0);
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*ERROR*message1*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/critical", 0, 0);
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*CRITICAL*message2*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/warning", 0, 0);
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*WARNING*message3*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/message", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stderr ("*Message*message4*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/info", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout_unmatched ("*INFO*message5*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/bar-info", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("*INFO*message5*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/baz-debug", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("*DEBUG*message6*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/debug", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("*DEBUG*6*6*6*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/debug-stderr", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout_unmatched ("DEBUG");
  g_test_trap_assert_stderr ("*DEBUG*6*6*6*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/0x400", 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stdout ("*LOG-0x400*message7*");

  g_test_trap_subprocess ("/logging/default-handler/subprocess/would-drop", 0, 0);
  g_test_trap_assert_passed ();
}

static void
test_fatal_log_mask (void)
{
  if (g_test_subprocess ())
    {
      g_log_set_fatal_mask ("bu", G_LOG_LEVEL_INFO);
      g_log ("bu", G_LOG_LEVEL_INFO, "fatal");
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_failed ();
  /* G_LOG_LEVEL_INFO isn't printed by default */
  g_test_trap_assert_stdout_unmatched ("*fatal*");
}

static gint my_print_count = 0;
static void
my_print_handler (const gchar *text)
{
  my_print_count++;
}

static void
test_print_handler (void)
{
  GPrintFunc old_print_handler;

  old_print_handler = g_set_print_handler (my_print_handler);
  g_assert (old_print_handler == NULL);

  my_print_count = 0;
  g_print ("bu ba");
  g_assert_cmpint (my_print_count, ==, 1);

  g_set_print_handler (NULL);
}

static void
test_printerr_handler (void)
{
  GPrintFunc old_printerr_handler;

  old_printerr_handler = g_set_printerr_handler (my_print_handler);
  g_assert (old_printerr_handler == NULL);

  my_print_count = 0;
  g_printerr ("bu ba");
  g_assert_cmpint (my_print_count, ==, 1);

  g_set_printerr_handler (NULL);
}

static char *fail_str = "foo";
static char *log_str = "bar";

static gboolean
good_failure_handler (const gchar    *log_domain,
                      GLogLevelFlags  log_level,
                      const gchar    *msg,
                      gpointer        user_data)
{
  g_test_message ("The Good Fail Message Handler\n");
  g_assert ((char *)user_data != log_str);
  g_assert ((char *)user_data == fail_str);

  return FALSE;
}

static gboolean
bad_failure_handler (const gchar    *log_domain,
                     GLogLevelFlags  log_level,
                     const gchar    *msg,
                     gpointer        user_data)
{
  g_test_message ("The Bad Fail Message Handler\n");
  g_assert ((char *)user_data == log_str);
  g_assert ((char *)user_data != fail_str);

  return FALSE;
}

static void
test_handler (const gchar    *log_domain,
              GLogLevelFlags  log_level,
              const gchar    *msg,
              gpointer        user_data)
{
  g_test_message ("The Log Message Handler\n");
  g_assert ((char *)user_data != fail_str);
  g_assert ((char *)user_data == log_str);
}

static void
bug653052 (void)
{
  g_test_bug ("653052");

  g_test_log_set_fatal_handler (good_failure_handler, fail_str);
  g_log_set_default_handler (test_handler, log_str);

  g_return_if_fail (0);

  g_test_log_set_fatal_handler (bad_failure_handler, fail_str);
  g_log_set_default_handler (test_handler, log_str);

  g_return_if_fail (0);
}

static void
test_gibberish (void)
{
  if (g_test_subprocess ())
    {
      g_warning ("bla bla \236\237\190");
      return;
    }
  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_failed ();
  g_test_trap_assert_stderr ("*bla bla \\x9e\\x9f\\u000190*");
}

static GLogWriterOutput
null_log_writer (GLogLevelFlags   log_level,
                 const GLogField *fields,
                 gsize            n_fields,
                 gpointer         user_data)
{
  log_count++;
  return G_LOG_WRITER_HANDLED;
}

typedef struct {
  const GLogField *fields;
  gsize n_fields;
} ExpectedMessage;

static gboolean
compare_field (const GLogField *f1, const GLogField *f2)
{
  if (strcmp (f1->key, f2->key) != 0)
    return FALSE;
  if (f1->length != f2->length)
    return FALSE;

  if (f1->length == -1)
    return strcmp (f1->value, f2->value) == 0;
  else
    return memcmp (f1->value, f2->value, f1->length) == 0;
}

static gboolean
compare_fields (const GLogField *f1, gsize n1, const GLogField *f2, gsize n2)
{
  int i, j;

  for (i = 0; i < n1; i++)
    {
      for (j = 0; j < n2; j++)
        {
          if (compare_field (&f1[i], &f2[j]))
            break;
        }
      if (j == n2)
        return FALSE;
    }

  return TRUE;
}

static GSList *expected_messages = NULL;
static const guchar binary_field[] = {1, 2, 3, 4, 5};


static GLogWriterOutput
expect_log_writer (GLogLevelFlags   log_level,
                   const GLogField *fields,
                   gsize            n_fields,
                   gpointer         user_data)
{
  ExpectedMessage *expected = expected_messages->data;

  if (compare_fields (fields, n_fields, expected->fields, expected->n_fields))
    {
      expected_messages = g_slist_delete_link (expected_messages, expected_messages);
    }
  else if ((log_level & G_LOG_LEVEL_DEBUG) != G_LOG_LEVEL_DEBUG)
    {
      char *str;

      str = g_log_writer_format_fields (log_level, fields, n_fields, FALSE);
      g_test_message ("Unexpected message: %s", str);
      g_free (str);
      g_test_fail ();
    }

  return G_LOG_WRITER_HANDLED;
}

static void
test_structured_logging_no_state (void)
{
  gpointer some_pointer = GUINT_TO_POINTER (0x100);
  guint some_integer = 123;

  log_count = 0;
  g_log_set_writer_func (null_log_writer, NULL, NULL);

  g_log_structured ("some-domain", G_LOG_LEVEL_MESSAGE,
                    "MESSAGE_ID", "06d4df59e6c24647bfe69d2c27ef0b4e",
                    "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
                    "MESSAGE", "This is a debug message about pointer %p and integer %u.",
                    some_pointer, some_integer);

  g_assert_cmpint (log_count, ==, 1);
}

static void
test_structured_logging_some_state (void)
{
  gpointer state_object = NULL;  /* this must not be dereferenced */
  const GLogField fields[] = {
    { "MESSAGE", "This is a debug message.", -1 },
    { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
    { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 },
    { "MY_APPLICATION_STATE", state_object, 0 },
  };

  log_count = 0;
  g_log_set_writer_func (null_log_writer, NULL, NULL);

  g_log_structured_array (G_LOG_LEVEL_DEBUG, fields, G_N_ELEMENTS (fields));

  g_assert_cmpint (log_count, ==, 1);
}

static void
test_structured_logging_robustness (void)
{
  log_count = 0;
  g_log_set_writer_func (null_log_writer, NULL, NULL);

  /* NULL log_domain shouldn't crash */
  g_log (NULL, G_LOG_LEVEL_MESSAGE, "Test");
  g_log_structured (NULL, G_LOG_LEVEL_MESSAGE, "MESSAGE", "Test");

  g_assert_cmpint (log_count, ==, 1);
}

static void
test_structured_logging_roundtrip1 (void)
{
  gpointer some_pointer = GUINT_TO_POINTER (0x100);
  gint some_integer = 123;
  gchar message[200];
  GLogField fields[] = {
    { "GLIB_DOMAIN", "some-domain", -1 },
    { "PRIORITY", "5", -1 },
    { "MESSAGE", "String assigned using g_snprintf() below", -1 },
    { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
    { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 }
  };
  ExpectedMessage expected = { fields, 5 };

  /* %p format is implementation defined and depends on the platform */
  g_snprintf (message, sizeof (message),
              "This is a debug message about pointer %p and integer %u.",
              some_pointer, some_integer);
  fields[2].value = message;

  expected_messages = g_slist_append (NULL, &expected);
  g_log_set_writer_func (expect_log_writer, NULL, NULL);

  g_log_structured ("some-domain", G_LOG_LEVEL_MESSAGE,
                    "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893",
                    "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
                    "MESSAGE", "This is a debug message about pointer %p and integer %u.",
                    some_pointer, some_integer);

  if (expected_messages != NULL)
    {
      char *str;
      ExpectedMessage *expected = expected_messages->data;

      str = g_log_writer_format_fields (0, expected->fields, expected->n_fields, FALSE);
      g_test_message ("Unexpected message: %s", str);
      g_free (str);
      g_test_fail ();
    }
}

static void
test_structured_logging_roundtrip2 (void)
{
  const gchar *some_string = "abc";
  const GLogField fields[] = {
    { "GLIB_DOMAIN", "some-domain", -1 },
    { "PRIORITY", "5", -1 },
    { "MESSAGE", "This is a debug message about string 'abc'.", -1 },
    { "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893", -1 },
    { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 }
  };
  ExpectedMessage expected = { fields, 5 };

  expected_messages = g_slist_append (NULL, &expected);
  g_log_set_writer_func (expect_log_writer, NULL, NULL);

  g_log_structured ("some-domain", G_LOG_LEVEL_MESSAGE,
                    "MESSAGE_ID", "fcfb2e1e65c3494386b74878f1abf893",
                    "MY_APPLICATION_CUSTOM_FIELD", "some debug string",
                    "MESSAGE", "This is a debug message about string '%s'.",
                    some_string);

  g_assert (expected_messages == NULL);
}

static void
test_structured_logging_roundtrip3 (void)
{
  const GLogField fields[] = {
    { "GLIB_DOMAIN", "some-domain", -1 },
    { "PRIORITY", "4", -1 },
    { "MESSAGE", "Test test test.", -1 }
  };
  ExpectedMessage expected = { fields, 3 };

  expected_messages = g_slist_append (NULL, &expected);
  g_log_set_writer_func (expect_log_writer, NULL, NULL);

  g_log_structured ("some-domain", G_LOG_LEVEL_WARNING,
                    "MESSAGE", "Test test test.");

  g_assert (expected_messages == NULL);
}

static GVariant *
create_variant_fields (void)
{
  GVariant *binary;
  GVariantBuilder builder;

  binary = g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, binary_field, G_N_ELEMENTS (binary_field), sizeof (binary_field[0]));

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
  g_variant_builder_add (&builder, "{sv}", "MESSAGE_ID", g_variant_new_string ("06d4df59e6c24647bfe69d2c27ef0b4e"));
  g_variant_builder_add (&builder, "{sv}", "MESSAGE", g_variant_new_string ("This is a debug message"));
  g_variant_builder_add (&builder, "{sv}", "MY_APPLICATION_CUSTOM_FIELD", g_variant_new_string ("some debug string"));
  g_variant_builder_add (&builder, "{sv}", "MY_APPLICATION_CUSTOM_FIELD_BINARY", binary);

  return g_variant_builder_end (&builder);
}

static void
test_structured_logging_variant1 (void)
{
  GVariant *v = create_variant_fields ();

  log_count = 0;
  g_log_set_writer_func (null_log_writer, NULL, NULL);

  g_log_variant ("some-domain", G_LOG_LEVEL_MESSAGE, v);
  g_variant_unref (v);
  g_assert_cmpint (log_count, ==, 1);
}

static void
test_structured_logging_variant2 (void)
{
  const GLogField fields[] = {
    { "GLIB_DOMAIN", "some-domain", -1 },
    { "PRIORITY", "5", -1 },
    { "MESSAGE", "This is a debug message", -1 },
    { "MESSAGE_ID", "06d4df59e6c24647bfe69d2c27ef0b4e", -1 },
    { "MY_APPLICATION_CUSTOM_FIELD", "some debug string", -1 },
    { "MY_APPLICATION_CUSTOM_FIELD_BINARY", binary_field, sizeof (binary_field) }
  };
  ExpectedMessage expected = { fields, 6 };
  GVariant *v = create_variant_fields ();

  expected_messages = g_slist_append (NULL, &expected);
  g_log_set_writer_func (expect_log_writer, NULL, NULL);

  g_log_variant ("some-domain", G_LOG_LEVEL_MESSAGE, v);
  g_variant_unref (v);
  g_assert (expected_messages == NULL);
}

int
main (int argc, char *argv[])
{
  g_unsetenv ("G_MESSAGES_DEBUG");

  g_test_init (&argc, &argv, NULL);
  g_test_bug_base ("http://bugzilla.gnome.org/");

  g_test_add_func ("/logging/default-handler", test_default_handler);
  g_test_add_func ("/logging/default-handler/subprocess/error", test_default_handler_error);
  g_test_add_func ("/logging/default-handler/subprocess/critical", test_default_handler_critical);
  g_test_add_func ("/logging/default-handler/subprocess/warning", test_default_handler_warning);
  g_test_add_func ("/logging/default-handler/subprocess/message", test_default_handler_message);
  g_test_add_func ("/logging/default-handler/subprocess/info", test_default_handler_info);
  g_test_add_func ("/logging/default-handler/subprocess/bar-info", test_default_handler_bar_info);
  g_test_add_func ("/logging/default-handler/subprocess/baz-debug", test_default_handler_baz_debug);
  g_test_add_func ("/logging/default-handler/subprocess/debug", test_default_handler_debug);
  g_test_add_func ("/logging/default-handler/subprocess/debug-stderr", test_default_handler_debug_stderr);
  g_test_add_func ("/logging/default-handler/subprocess/0x400", test_default_handler_0x400);
  g_test_add_func ("/logging/default-handler/subprocess/would-drop", test_default_handler_would_drop);
  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/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);
  g_test_add_func ("/logging/653052", bug653052);
  g_test_add_func ("/logging/gibberish", test_gibberish);
  g_test_add_func ("/structured-logging/no-state", test_structured_logging_no_state);
  g_test_add_func ("/structured-logging/some-state", test_structured_logging_some_state);
  g_test_add_func ("/structured-logging/robustness", test_structured_logging_robustness);
  g_test_add_func ("/structured-logging/roundtrip1", test_structured_logging_roundtrip1);
  g_test_add_func ("/structured-logging/roundtrip2", test_structured_logging_roundtrip2);
  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);

  return g_test_run ();
}