]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-log.c
Merge pull request #23351 from keszybz/logind-message
[thirdparty/systemd.git] / src / test / test-log.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stddef.h>
4 #include <unistd.h>
5
6 #include "format-util.h"
7 #include "log.h"
8 #include "process-util.h"
9 #include "string-util.h"
10 #include "util.h"
11
12 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL)));
13 assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL));
14 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
15 assert_cc(!IS_SYNTHETIC_ERRNO(0));
16
17 #define X10(x) x x x x x x x x x x
18 #define X100(x) X10(X10(x))
19 #define X1000(x) X100(X10(x))
20
21 static void test_file(void) {
22 log_info("__FILE__: %s", __FILE__);
23 log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH);
24 log_info("PROJECT_FILE: %s", PROJECT_FILE);
25
26 assert_se(startswith(__FILE__, RELATIVE_SOURCE_PATH "/"));
27 }
28
29 static void test_log_struct(void) {
30 log_struct(LOG_INFO,
31 "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(),
32 "SERVICE=piepapo");
33
34 /* The same as above, just using LOG_MESSAGE(), which is generally recommended */
35 log_struct(LOG_INFO,
36 LOG_MESSAGE("Waldo PID="PID_FMT" (no errno)", getpid_cached()),
37 "SERVICE=piepapo");
38
39 log_struct_errno(LOG_INFO, EILSEQ,
40 LOG_MESSAGE("Waldo PID="PID_FMT": %m (normal)", getpid_cached()),
41 "SERVICE=piepapo");
42
43 log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ),
44 LOG_MESSAGE("Waldo PID="PID_FMT": %m (synthetic)", getpid_cached()),
45 "SERVICE=piepapo");
46
47 log_struct(LOG_INFO,
48 LOG_MESSAGE("Foobar PID="PID_FMT, getpid_cached()),
49 "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",
50 (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,
51 "SUFFIX=GOT IT");
52 }
53
54 static void test_long_lines(void) {
55 log_object_internal(LOG_NOTICE,
56 EUCLEAN,
57 X1000("abcd_") ".txt",
58 1000000,
59 X1000("fff") "unc",
60 "OBJECT=",
61 X1000("obj_") "ect",
62 "EXTRA=",
63 X1000("ext_") "tra",
64 "asdfasdf %s asdfasdfa", "foobar");
65 }
66
67 static void test_log_syntax(void) {
68 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, EINVAL, "EINVAL: %s: %m", "hogehoge") == -EINVAL);
69 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, -ENOENT, "ENOENT: %s: %m", "hogehoge") == -ENOENT);
70 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, SYNTHETIC_ERRNO(ENOTTY), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY);
71 }
72
73 int main(int argc, char* argv[]) {
74 test_file();
75
76 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN);
77
78 for (int target = 0; target < _LOG_TARGET_MAX; target++) {
79 log_set_target(target);
80 log_open();
81
82 test_log_struct();
83 test_long_lines();
84 test_log_syntax();
85 }
86
87 return 0;
88 }