chiark / gitweb /
a09439c797f1d6203658a3ab7435b4610bbd8bd1
[elogind.git] / src / test / test-log.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <stddef.h>
9 #include <unistd.h>
10
11 #include "format-util.h"
12 #include "log.h"
13 /// Additional includes needed by elogind
14 #include "process-util.h"
15 //#include "util.h"
16
17 assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_FTP | LOG_DEBUG))
18           == LOG_REALM_SYSTEMD);
19 assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_LOCAL7 | LOG_DEBUG))
20           == LOG_REALM_UDEV);
21 assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_LOCAL3 | LOG_DEBUG) & LOG_FACMASK)
22           == LOG_LOCAL3);
23 assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_USER | LOG_INFO) & LOG_PRIMASK)
24           == LOG_INFO);
25
26 #define X10(x) x x x x x x x x x x
27 #define X100(x) X10(X10(x))
28 #define X1000(x) X100(X10(x))
29
30 static void test_log_console(void) {
31         log_struct(LOG_INFO,
32                    "MESSAGE=Waldo PID="PID_FMT, getpid_cached(),
33                    "SERVICE=piepapo");
34 }
35
36 static void test_log_journal(void) {
37         log_struct(LOG_INFO,
38                    "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
39                    "SERVICE=foobar");
40
41         log_struct(LOG_INFO,
42                    "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
43                    "FORMAT_STR_TEST=1=%i A=%c 2=%hi 3=%li 4=%lli 1=%p foo=%s 2.5=%g 3.5=%g 4.5=%Lg",
44                    (int) 1, 'A', (short) 2, (long int) 3, (long long int) 4, (void*) 1, "foo", (float) 2.5f, (double) 3.5, (long double) 4.5,
45                    "SUFFIX=GOT IT");
46 }
47
48 static void test_long_lines(void) {
49         log_object_internal(LOG_NOTICE,
50                             EUCLEAN,
51                             X1000("abcd_") ".txt",
52                             1000000,
53                             X1000("fff") "unc",
54                             "OBJECT=",
55                             X1000("obj_") "ect",
56                             "EXTRA=",
57                             X1000("ext_") "tra",
58                             "asdfasdf %s asdfasdfa", "foobar");
59 }
60
61 int main(int argc, char* argv[]) {
62         int target;
63
64         for (target = 0; target <  _LOG_TARGET_MAX; target++) {
65                 log_set_target(target);
66                 log_open();
67
68                 test_log_console();
69                 test_log_journal();
70                 test_long_lines();
71         }
72
73         return 0;
74 }