]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-log.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "format-util.h"
8 #include "iovec-util.h"
9 #include "iovec-wrapper.h"
11 #include "process-util.h"
12 #include "string-util.h"
15 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL
)));
16 assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL
));
17 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
18 assert_cc(!IS_SYNTHETIC_ERRNO(0));
20 #define X10(x) x x x x x x x x x x
21 #define X100(x) X10(X10(x))
22 #define X1000(x) X100(X10(x))
24 static void test_file(void) {
25 log_info("__FILE__: %s", __FILE__
);
26 log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH
);
27 log_info("PROJECT_FILE: %s", PROJECT_FILE
);
29 assert_se(startswith(__FILE__
, RELATIVE_SOURCE_PATH
"/"));
32 static void test_log_struct(void) {
34 "MESSAGE=Waldo PID="PID_FMT
" (no errno)", getpid_cached(),
37 /* The same as above, just using LOG_MESSAGE(), which is generally recommended */
39 LOG_MESSAGE("Waldo PID="PID_FMT
" (no errno)", getpid_cached()),
42 log_struct_errno(LOG_INFO
, EILSEQ
,
43 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (normal)", getpid_cached()),
46 log_struct_errno(LOG_INFO
, SYNTHETIC_ERRNO(EILSEQ
),
47 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (synthetic)", getpid_cached()),
51 LOG_MESSAGE("Foobar PID="PID_FMT
, getpid_cached()),
52 "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",
53 (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,
57 static void test_long_lines(void) {
58 log_object_internal(LOG_NOTICE
,
60 X1000("abcd_") ".txt",
67 "asdfasdf %s asdfasdfa", "foobar");
70 static void test_log_syntax(void) {
71 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, EINVAL
, "EINVAL: %s: %m", "hogehoge") == -EINVAL
);
72 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, -ENOENT
, "ENOENT: %s: %m", "hogehoge") == -ENOENT
);
73 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, SYNTHETIC_ERRNO(ENOTTY
), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY
);
76 static void test_log_context(void) {
78 char **strv
= STRV_MAKE("FIRST=abc", "SECOND=qrs");
80 LOG_CONTEXT_PUSH("THIRD=pfs");
81 LOG_CONTEXT_PUSH("FOURTH=def");
82 LOG_CONTEXT_PUSH_STRV(strv
);
83 LOG_CONTEXT_PUSH_STRV(strv
);
85 /* Test that the log context was set up correctly. The strv we pushed twice should only
86 * result in one log context which is reused. */
87 assert_se(log_context_num_contexts() == 3);
88 assert_se(log_context_num_fields() == 4);
90 /* Test that everything still works with modifications to the log context. */
96 LOG_CONTEXT_PUSH("FIFTH=123");
97 LOG_CONTEXT_PUSH_STRV(strv
);
99 /* Check that our nested fields got added correctly. */
100 assert_se(log_context_num_contexts() == 4);
101 assert_se(log_context_num_fields() == 5);
103 /* Test that everything still works in a nested block. */
109 /* Check that only the fields from the nested block got removed. */
110 assert_se(log_context_num_contexts() == 3);
111 assert_se(log_context_num_fields() == 4);
114 assert_se(log_context_num_contexts() == 0);
115 assert_se(log_context_num_fields() == 0);
118 _cleanup_(log_context_unrefp
) LogContext
*ctx
= NULL
;
120 char **strv
= STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
121 assert_se(ctx
= log_context_new_strv(strv
, /*owned=*/ false));
123 assert_se(log_context_num_contexts() == 1);
124 assert_se(log_context_num_fields() == 2);
126 /* Test that everything still works with a manually configured log context. */
135 assert_se(strv
= strv_new("ABC", "DEF"));
136 LOG_CONTEXT_CONSUME_STRV(strv
);
138 assert_se(log_context_num_contexts() == 1);
139 assert_se(log_context_num_fields() == 2);
143 /* Test that everything still works with a mixed strv and iov. */
144 struct iovec iov
[] = {
145 IOVEC_MAKE_STRING("ABC=def"),
146 IOVEC_MAKE_STRING("GHI=jkl"),
148 _cleanup_free_
struct iovec_wrapper
*iovw
= iovw_new();
150 assert_se(iovw_consume(iovw
, strdup("MNO=pqr"), STRLEN("MNO=pqr") + 1) == 0);
152 LOG_CONTEXT_PUSH_IOV(iov
, ELEMENTSOF(iov
));
153 LOG_CONTEXT_PUSH_IOV(iov
, ELEMENTSOF(iov
));
154 LOG_CONTEXT_CONSUME_IOV(iovw
->iovec
, iovw
->count
);
155 LOG_CONTEXT_PUSH("STU=vwx");
157 assert_se(log_context_num_contexts() == 3);
158 assert_se(log_context_num_fields() == 4);
166 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
167 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
168 assert_se(log_context_num_contexts() == 1);
169 assert_se(log_context_num_fields() == 1);
176 assert_se(log_context_num_contexts() == 0);
177 assert_se(log_context_num_fields() == 0);
180 static void test_log_prefix(void) {
182 LOG_SET_PREFIX("ABC");
189 LOG_SET_PREFIX("QED");
206 int main(int argc
, char* argv
[]) {
209 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN
), "foo") == -EUCLEAN
);
211 for (int target
= 0; target
< _LOG_TARGET_MAX
; target
++) {
212 log_set_target(target
);