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"
16 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL
)));
17 assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL
));
18 assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
19 assert_cc(!IS_SYNTHETIC_ERRNO(0));
21 #define X10(x) x x x x x x x x x x
22 #define X100(x) X10(X10(x))
23 #define X1000(x) X100(X10(x))
25 static int fail_with_EINVAL(void) {
26 assert_return(false, -EINVAL
);
30 static void test_assert_return_is_critical(void) {
31 SAVE_ASSERT_RETURN_IS_CRITICAL
;
33 log_set_assert_return_is_critical(false);
34 assert_se(fail_with_EINVAL() == -EINVAL
);
36 log_set_assert_return_is_critical(true);
37 ASSERT_RETURN_IS_CRITICAL(false, assert_se(fail_with_EINVAL() == -EINVAL
));
38 assert_se(log_get_assert_return_is_critical() == true);
39 ASSERT_RETURN_EXPECTED(assert_se(fail_with_EINVAL() == -EINVAL
));
40 assert_se(log_get_assert_return_is_critical() == true);
41 ASSERT_RETURN_EXPECTED_SE(fail_with_EINVAL() == -EINVAL
);
42 assert_se(log_get_assert_return_is_critical() == true);
45 static void test_file(void) {
46 log_info("__FILE__: %s", __FILE__
);
47 log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH
);
48 log_info("PROJECT_FILE: %s", PROJECT_FILE
);
50 assert_se(startswith(__FILE__
, RELATIVE_SOURCE_PATH
"/"));
53 static void test_log_struct(void) {
55 "MESSAGE=Waldo PID="PID_FMT
" (no errno)", getpid_cached(),
58 /* The same as above, just using LOG_MESSAGE(), which is generally recommended */
60 LOG_MESSAGE("Waldo PID="PID_FMT
" (no errno)", getpid_cached()),
63 log_struct_errno(LOG_INFO
, EILSEQ
,
64 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (normal)", getpid_cached()),
67 log_struct_errno(LOG_INFO
, SYNTHETIC_ERRNO(EILSEQ
),
68 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (synthetic)", getpid_cached()),
72 LOG_MESSAGE("Foobar PID="PID_FMT
, getpid_cached()),
73 "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",
74 (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,
78 static void test_long_lines(void) {
79 log_object_internal(LOG_NOTICE
,
81 X1000("abcd_") ".txt",
88 "asdfasdf %s asdfasdfa", "foobar");
91 static void test_log_syntax(void) {
92 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, EINVAL
, "EINVAL: %s: %m", "hogehoge") == -EINVAL
);
93 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, -ENOENT
, "ENOENT: %s: %m", "hogehoge") == -ENOENT
);
94 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, SYNTHETIC_ERRNO(ENOTTY
), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY
);
97 static void test_log_context(void) {
99 char **strv
= STRV_MAKE("FIRST=abc", "SECOND=qrs");
101 LOG_CONTEXT_PUSH("THIRD=pfs");
102 LOG_CONTEXT_PUSH("FOURTH=def");
103 LOG_CONTEXT_PUSH_STRV(strv
);
104 LOG_CONTEXT_PUSH_STRV(strv
);
106 /* Test that the log context was set up correctly. The strv we pushed twice should only
107 * result in one log context which is reused. */
108 assert_se(log_context_num_contexts() == 3);
109 assert_se(log_context_num_fields() == 4);
111 /* Test that everything still works with modifications to the log context. */
117 LOG_CONTEXT_PUSH("FIFTH=123");
118 LOG_CONTEXT_PUSH_STRV(strv
);
120 /* Check that our nested fields got added correctly. */
121 assert_se(log_context_num_contexts() == 4);
122 assert_se(log_context_num_fields() == 5);
124 /* Test that everything still works in a nested block. */
130 /* Check that only the fields from the nested block got removed. */
131 assert_se(log_context_num_contexts() == 3);
132 assert_se(log_context_num_fields() == 4);
135 assert_se(log_context_num_contexts() == 0);
136 assert_se(log_context_num_fields() == 0);
139 _cleanup_(log_context_unrefp
) LogContext
*ctx
= NULL
;
141 char **strv
= STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
142 assert_se(ctx
= log_context_new_strv(strv
, /*owned=*/ false));
144 assert_se(log_context_num_contexts() == 1);
145 assert_se(log_context_num_fields() == 2);
147 /* Test that everything still works with a manually configured log context. */
156 assert_se(strv
= strv_new("ABC", "DEF"));
157 LOG_CONTEXT_CONSUME_STRV(strv
);
159 assert_se(log_context_num_contexts() == 1);
160 assert_se(log_context_num_fields() == 2);
164 /* Test that everything still works with a mixed strv and iov. */
165 struct iovec iov
[] = {
166 IOVEC_MAKE_STRING("ABC=def"),
167 IOVEC_MAKE_STRING("GHI=jkl"),
169 _cleanup_free_
struct iovec_wrapper
*iovw
= iovw_new();
171 assert_se(iovw_consume(iovw
, strdup("MNO=pqr"), STRLEN("MNO=pqr") + 1) == 0);
173 LOG_CONTEXT_PUSH_IOV(iov
, ELEMENTSOF(iov
));
174 LOG_CONTEXT_PUSH_IOV(iov
, ELEMENTSOF(iov
));
175 LOG_CONTEXT_CONSUME_IOV(iovw
->iovec
, iovw
->count
);
176 LOG_CONTEXT_PUSH("STU=vwx");
178 assert_se(log_context_num_contexts() == 3);
179 assert_se(log_context_num_fields() == 4);
187 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
188 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
189 assert_se(log_context_num_contexts() == 1);
190 assert_se(log_context_num_fields() == 1);
197 assert_se(log_context_num_contexts() == 0);
198 assert_se(log_context_num_fields() == 0);
201 static void test_log_prefix(void) {
203 LOG_SET_PREFIX("ABC");
210 LOG_SET_PREFIX("QED");
227 int main(int argc
, char* argv
[]) {
228 test_setup_logging(LOG_DEBUG
);
230 test_assert_return_is_critical();
233 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN
), "foo") == -EUCLEAN
);
235 for (int target
= 0; target
< _LOG_TARGET_MAX
; target
++) {
236 log_set_target(target
);