]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-log.c
Merge pull request #26944 from aafeijoo-suse/systemd-network-generator-initrd-fix
[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 "io-util.h"
8 #include "log.h"
9 #include "process-util.h"
10 #include "string-util.h"
11 #include "strv.h"
12
13 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL)));
14 assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL));
15 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
16 assert_cc(!IS_SYNTHETIC_ERRNO(0));
17
18 #define X10(x) x x x x x x x x x x
19 #define X100(x) X10(X10(x))
20 #define X1000(x) X100(X10(x))
21
22 static void test_file(void) {
23 log_info("__FILE__: %s", __FILE__);
24 log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH);
25 log_info("PROJECT_FILE: %s", PROJECT_FILE);
26
27 assert_se(startswith(__FILE__, RELATIVE_SOURCE_PATH "/"));
28 }
29
30 static void test_log_struct(void) {
31 log_struct(LOG_INFO,
32 "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(),
33 "SERVICE=piepapo");
34
35 /* The same as above, just using LOG_MESSAGE(), which is generally recommended */
36 log_struct(LOG_INFO,
37 LOG_MESSAGE("Waldo PID="PID_FMT" (no errno)", getpid_cached()),
38 "SERVICE=piepapo");
39
40 log_struct_errno(LOG_INFO, EILSEQ,
41 LOG_MESSAGE("Waldo PID="PID_FMT": %m (normal)", getpid_cached()),
42 "SERVICE=piepapo");
43
44 log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ),
45 LOG_MESSAGE("Waldo PID="PID_FMT": %m (synthetic)", getpid_cached()),
46 "SERVICE=piepapo");
47
48 log_struct(LOG_INFO,
49 LOG_MESSAGE("Foobar PID="PID_FMT, getpid_cached()),
50 "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",
51 (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,
52 "SUFFIX=GOT IT");
53 }
54
55 static void test_long_lines(void) {
56 log_object_internal(LOG_NOTICE,
57 EUCLEAN,
58 X1000("abcd_") ".txt",
59 1000000,
60 X1000("fff") "unc",
61 "OBJECT=",
62 X1000("obj_") "ect",
63 "EXTRA=",
64 X1000("ext_") "tra",
65 "asdfasdf %s asdfasdfa", "foobar");
66 }
67
68 static void test_log_syntax(void) {
69 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, EINVAL, "EINVAL: %s: %m", "hogehoge") == -EINVAL);
70 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, -ENOENT, "ENOENT: %s: %m", "hogehoge") == -ENOENT);
71 assert_se(log_syntax("unit", LOG_ERR, "filename", 10, SYNTHETIC_ERRNO(ENOTTY), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY);
72 }
73
74 static void test_log_context(void) {
75 {
76 char **strv = STRV_MAKE("FIRST=abc", "SECOND=qrs");
77
78 LOG_CONTEXT_PUSH("THIRD=pfs");
79 LOG_CONTEXT_PUSH("FOURTH=def");
80 LOG_CONTEXT_PUSH_STRV(strv);
81 LOG_CONTEXT_PUSH_STRV(strv);
82
83 /* Test that the log context was set up correctly. The strv we pushed twice should only
84 * result in one log context which is reused. */
85 assert_se(log_context_num_contexts() == 3);
86 assert_se(log_context_num_fields() == 4);
87
88 /* Test that everything still works with modifications to the log context. */
89 test_log_struct();
90 test_long_lines();
91 test_log_syntax();
92
93 {
94 LOG_CONTEXT_PUSH("FIFTH=123");
95 LOG_CONTEXT_PUSH_STRV(strv);
96
97 /* Check that our nested fields got added correctly. */
98 assert_se(log_context_num_contexts() == 4);
99 assert_se(log_context_num_fields() == 5);
100
101 /* Test that everything still works in a nested block. */
102 test_log_struct();
103 test_long_lines();
104 test_log_syntax();
105 }
106
107 /* Check that only the fields from the nested block got removed. */
108 assert_se(log_context_num_contexts() == 3);
109 assert_se(log_context_num_fields() == 4);
110 }
111
112 assert_se(log_context_num_contexts() == 0);
113 assert_se(log_context_num_fields() == 0);
114
115 {
116 _cleanup_(log_context_unrefp) LogContext *ctx = NULL;
117
118 char **strv = STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
119 assert_se(ctx = log_context_new_strv(strv, /*owned=*/ false));
120
121 assert_se(log_context_num_contexts() == 1);
122 assert_se(log_context_num_fields() == 2);
123
124 /* Test that everything still works with a manually configured log context. */
125 test_log_struct();
126 test_long_lines();
127 test_log_syntax();
128 }
129
130 {
131 char **strv = NULL;
132
133 assert_se(strv = strv_new("ABC", "DEF"));
134 LOG_CONTEXT_CONSUME_STRV(strv);
135
136 assert_se(log_context_num_contexts() == 1);
137 assert_se(log_context_num_fields() == 2);
138 }
139
140 {
141 /* Test that everything still works with a mixed strv and iov. */
142 struct iovec iov[] = {
143 IOVEC_MAKE_STRING("ABC=def"),
144 IOVEC_MAKE_STRING("GHI=jkl"),
145 };
146 _cleanup_free_ struct iovec_wrapper *iovw = iovw_new();
147 assert_se(iovw);
148 assert_se(iovw_consume(iovw, strdup("MNO=pqr"), STRLEN("MNO=pqr") + 1) == 0);
149
150 LOG_CONTEXT_PUSH_IOV(iov, ELEMENTSOF(iov));
151 LOG_CONTEXT_PUSH_IOV(iov, ELEMENTSOF(iov));
152 LOG_CONTEXT_CONSUME_IOV(iovw->iovec, iovw->count);
153 LOG_CONTEXT_PUSH("STU=vwx");
154
155 assert_se(log_context_num_contexts() == 3);
156 assert_se(log_context_num_fields() == 4);
157
158 test_log_struct();
159 test_long_lines();
160 test_log_syntax();
161 }
162
163 {
164 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
165 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
166 assert_se(log_context_num_contexts() == 1);
167 assert_se(log_context_num_fields() == 1);
168
169 test_log_struct();
170 test_long_lines();
171 test_log_syntax();
172 }
173
174 assert_se(log_context_num_contexts() == 0);
175 assert_se(log_context_num_fields() == 0);
176 }
177
178 static void test_log_prefix(void) {
179 {
180 LOG_SET_PREFIX("ABC");
181
182 test_log_struct();
183 test_long_lines();
184 test_log_syntax();
185
186 {
187 LOG_SET_PREFIX("QED");
188
189 test_log_struct();
190 test_long_lines();
191 test_log_syntax();
192 }
193
194 test_log_struct();
195 test_long_lines();
196 test_log_syntax();
197 }
198
199 test_log_struct();
200 test_long_lines();
201 test_log_syntax();
202 }
203
204 int main(int argc, char* argv[]) {
205 test_file();
206
207 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN);
208
209 for (int target = 0; target < _LOG_TARGET_MAX; target++) {
210 log_set_target(target);
211 log_open();
212
213 test_log_struct();
214 test_long_lines();
215 test_log_syntax();
216 test_log_context();
217 test_log_prefix();
218 }
219
220 return 0;
221 }