]>
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"
9 #include "process-util.h"
10 #include "string-util.h"
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));
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))
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
);
27 assert_se(startswith(__FILE__
, RELATIVE_SOURCE_PATH
"/"));
30 static void test_log_struct(void) {
32 "MESSAGE=Waldo PID="PID_FMT
" (no errno)", getpid_cached(),
35 /* The same as above, just using LOG_MESSAGE(), which is generally recommended */
37 LOG_MESSAGE("Waldo PID="PID_FMT
" (no errno)", getpid_cached()),
40 log_struct_errno(LOG_INFO
, EILSEQ
,
41 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (normal)", getpid_cached()),
44 log_struct_errno(LOG_INFO
, SYNTHETIC_ERRNO(EILSEQ
),
45 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (synthetic)", getpid_cached()),
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,
55 static void test_long_lines(void) {
56 log_object_internal(LOG_NOTICE
,
58 X1000("abcd_") ".txt",
65 "asdfasdf %s asdfasdfa", "foobar");
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
);
74 static void test_log_context(void) {
76 char **strv
= STRV_MAKE("FIRST=abc", "SECOND=qrs");
78 LOG_CONTEXT_PUSH("THIRD=pfs");
79 LOG_CONTEXT_PUSH("FOURTH=def");
80 LOG_CONTEXT_PUSH_STRV(strv
);
81 LOG_CONTEXT_PUSH_STRV(strv
);
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);
88 /* Test that everything still works with modifications to the log context. */
94 LOG_CONTEXT_PUSH("FIFTH=123");
95 LOG_CONTEXT_PUSH_STRV(strv
);
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);
101 /* Test that everything still works in a nested block. */
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);
112 assert_se(log_context_num_contexts() == 0);
113 assert_se(log_context_num_fields() == 0);
116 _cleanup_(log_context_unrefp
) LogContext
*ctx
= NULL
;
118 char **strv
= STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
119 assert_se(ctx
= log_context_new_strv(strv
, /*owned=*/ false));
121 assert_se(log_context_num_contexts() == 1);
122 assert_se(log_context_num_fields() == 2);
124 /* Test that everything still works with a manually configured log context. */
133 assert_se(strv
= strv_new("ABC", "DEF"));
134 LOG_CONTEXT_CONSUME_STRV(strv
);
136 assert_se(log_context_num_contexts() == 1);
137 assert_se(log_context_num_fields() == 2);
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"),
146 _cleanup_free_
struct iovec_wrapper
*iovw
= iovw_new();
148 assert_se(iovw_consume(iovw
, strdup("MNO=pqr"), STRLEN("MNO=pqr") + 1) == 0);
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");
155 assert_se(log_context_num_contexts() == 3);
156 assert_se(log_context_num_fields() == 4);
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);
174 assert_se(log_context_num_contexts() == 0);
175 assert_se(log_context_num_fields() == 0);
178 static void test_log_prefix(void) {
180 LOG_SET_PREFIX("ABC");
187 LOG_SET_PREFIX("QED");
204 int main(int argc
, char* argv
[]) {
207 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN
), "foo") == -EUCLEAN
);
209 for (int target
= 0; target
< _LOG_TARGET_MAX
; target
++) {
210 log_set_target(target
);