chiark / gitweb /
basic/log: make sure header is printed correctly, add test
[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   systemd is free software; you can redistribute it and/or modify it
8   under the terms of the GNU Lesser General Public License as published by
9   the Free Software Foundation; either version 2.1 of the License, or
10   (at your option) any later version.
11
12   systemd is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public License
18   along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <stddef.h>
22 #include <unistd.h>
23
24 #include "format-util.h"
25 #include "log.h"
26 /// Additional includes needed by elogind
27 #include "process-util.h"
28 //#include "util.h"
29
30 assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_FTP | LOG_DEBUG))
31           == LOG_REALM_SYSTEMD);
32 assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_LOCAL7 | LOG_DEBUG))
33           == LOG_REALM_UDEV);
34 assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_LOCAL3 | LOG_DEBUG) & LOG_FACMASK)
35           == LOG_LOCAL3);
36 assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_USER | LOG_INFO) & LOG_PRIMASK)
37           == LOG_INFO);
38
39 #define X10(x) x x x x x x x x x x
40 #define X100(x) X10(X10(x))
41 #define X1000(x) X100(X10(x))
42
43 static void test_log_console(void) {
44         log_struct(LOG_INFO,
45                    "MESSAGE=Waldo PID="PID_FMT, getpid_cached(),
46                    "SERVICE=piepapo",
47                    NULL);
48 }
49
50 static void test_log_journal(void) {
51         log_struct(LOG_INFO,
52                    "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
53                    "SERVICE=foobar",
54                    NULL);
55
56         log_struct(LOG_INFO,
57                    "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
58                    "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",
59                    (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,
60                    "SUFFIX=GOT IT",
61                    NULL);
62 }
63
64 static void test_long_lines(void) {
65         log_object_internal(LOG_NOTICE,
66                             EUCLEAN,
67                             X1000("abcd_") ".txt",
68                             1000000,
69                             X1000("fff") "unc",
70                             "OBJECT=",
71                             X1000("obj_") "ect",
72                             "EXTRA=",
73                             X1000("ext_") "tra",
74                             "asdfasdf %s asdfasdfa", "foobar");
75 }
76
77 int main(int argc, char* argv[]) {
78         int target;
79
80         for (target = 0; target <  _LOG_TARGET_MAX; target++) {
81                 log_set_target(target);
82                 log_open();
83
84                 test_log_console();
85                 test_log_journal();
86                 test_long_lines();
87         }
88
89         return 0;
90 }