]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-log.c
log: Avoid pushing the same fields more than once on the log context
[thirdparty/systemd.git] / src / test / test-log.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
877d54e9
LP
2
3#include <stddef.h>
4#include <unistd.h>
5
f97b34a6 6#include "format-util.h"
2461943b 7#include "io-util.h"
877d54e9 8#include "log.h"
dccca82b 9#include "process-util.h"
a0b15b41 10#include "string-util.h"
7c7a9138 11#include "strv.h"
877d54e9 12
52d86690
ZJS
13assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL)));
14assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL));
15assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
16assert_cc(!IS_SYNTHETIC_ERRNO(0));
17
f8e6f4aa
ZJS
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))
877d54e9 21
a0b15b41
ZJS
22static 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
f21b863e 27 assert_se(startswith(__FILE__, RELATIVE_SOURCE_PATH "/"));
a0b15b41
ZJS
28}
29
52d86690 30static void test_log_struct(void) {
877d54e9 31 log_struct(LOG_INFO,
52d86690 32 "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(),
a1230ff9 33 "SERVICE=piepapo");
877d54e9 34
92663a5e
ZJS
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()),
52d86690
ZJS
38 "SERVICE=piepapo");
39
92663a5e
ZJS
40 log_struct_errno(LOG_INFO, EILSEQ,
41 LOG_MESSAGE("Waldo PID="PID_FMT": %m (normal)", getpid_cached()),
42 "SERVICE=piepapo");
43
52d86690 44 log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ),
92663a5e
ZJS
45 LOG_MESSAGE("Waldo PID="PID_FMT": %m (synthetic)", getpid_cached()),
46 "SERVICE=piepapo");
877d54e9 47
963ddb91 48 log_struct(LOG_INFO,
92663a5e 49 LOG_MESSAGE("Foobar PID="PID_FMT, getpid_cached()),
963ddb91
LP
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,
a1230ff9 52 "SUFFIX=GOT IT");
f8e6f4aa
ZJS
53}
54
55static 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
59c1546b
YW
68static 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
7c7a9138
DDM
74static 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
88335453
DDM
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);
7c7a9138
DDM
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. */
88335453
DDM
98 assert_se(log_context_num_contexts() == 4);
99 assert_se(log_context_num_fields() == 5);
7c7a9138
DDM
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. */
88335453
DDM
108 assert_se(log_context_num_contexts() == 3);
109 assert_se(log_context_num_fields() == 4);
7c7a9138
DDM
110 }
111
112 assert_se(log_context_num_contexts() == 0);
113 assert_se(log_context_num_fields() == 0);
114
115 {
88335453 116 _cleanup_(log_context_unrefp) LogContext *ctx = NULL;
7c7a9138
DDM
117
118 char **strv = STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
119 assert_se(ctx = log_context_new(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
2461943b
LB
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
88335453 150 LOG_CONTEXT_PUSH_IOV(iov, ELEMENTSOF(iov));
2461943b
LB
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
7c7a9138
DDM
163 assert_se(log_context_num_contexts() == 0);
164 assert_se(log_context_num_fields() == 0);
165}
166
f8e6f4aa 167int main(int argc, char* argv[]) {
a0b15b41
ZJS
168 test_file();
169
169d980b
ZJS
170 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN);
171
172 for (int target = 0; target < _LOG_TARGET_MAX; target++) {
f8e6f4aa
ZJS
173 log_set_target(target);
174 log_open();
175
52d86690 176 test_log_struct();
f8e6f4aa 177 test_long_lines();
59c1546b 178 test_log_syntax();
7c7a9138 179 test_log_context();
f8e6f4aa 180 }
963ddb91 181
877d54e9
LP
182 return 0;
183}