1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "format-util.h"
4 #include "iovec-util.h"
5 #include "iovec-wrapper.h"
7 #include "log-context.h"
8 #include "process-util.h"
9 #include "string-util.h"
13 #define X10(x) x x x x x x x x x x
14 #define X100(x) X10(X10(x))
15 #define X1000(x) X100(X10(x))
17 static int fail_with_EINVAL(void) {
18 assert_return(false, -EINVAL
);
22 static void test_assert_return_is_critical(void) {
23 SAVE_ASSERT_RETURN_IS_CRITICAL
;
25 log_set_assert_return_is_critical(false);
26 assert_se(fail_with_EINVAL() == -EINVAL
);
28 log_set_assert_return_is_critical(true);
29 ASSERT_RETURN_IS_CRITICAL(false, assert_se(fail_with_EINVAL() == -EINVAL
));
30 assert_se(log_get_assert_return_is_critical() == true);
31 ASSERT_RETURN_EXPECTED(assert_se(fail_with_EINVAL() == -EINVAL
));
32 assert_se(log_get_assert_return_is_critical() == true);
33 ASSERT_RETURN_EXPECTED_SE(fail_with_EINVAL() == -EINVAL
);
34 assert_se(log_get_assert_return_is_critical() == true);
37 static void test_file(void) {
38 log_info("__FILE__: %s", __FILE__
);
39 log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH
);
40 log_info("PROJECT_FILE: %s", PROJECT_FILE
);
42 assert_se(startswith(__FILE__
, RELATIVE_SOURCE_PATH
"/"));
45 static void test_log_struct(void) {
47 "MESSAGE=Waldo PID="PID_FMT
" (no errno)", getpid_cached(),
50 /* The same as above, just using LOG_MESSAGE() and LOG_ITEM(), which is generally recommended */
52 LOG_MESSAGE("Waldo PID="PID_FMT
" (no errno)", getpid_cached()),
53 LOG_ITEM("SERVICE=piepapo"));
55 log_struct_errno(LOG_INFO
, EILSEQ
,
56 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (normal)", getpid_cached()),
57 LOG_ITEM("SERVICE=piepapo"));
59 log_struct_errno(LOG_INFO
, SYNTHETIC_ERRNO(EILSEQ
),
60 LOG_MESSAGE("Waldo PID="PID_FMT
": %m (synthetic)", getpid_cached()),
61 LOG_ITEM("SERVICE=piepapo"));
64 LOG_MESSAGE("Foobar PID="PID_FMT
, getpid_cached()),
65 LOG_ITEM("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",
66 (int) 1, 'A', (short) 2, (long) 3, (long long) 4, (void*) 1, "foo", (float) 2.5f
, (double) 3.5, (long double) 4.5),
67 LOG_ITEM("SUFFIX=GOT IT"));
70 static void test_long_lines(void) {
71 log_object_internal(LOG_NOTICE
,
73 X1000("abcd_") ".txt",
80 "asdfasdf %s asdfasdfa", "foobar");
83 static void test_log_syntax(void) {
84 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, EINVAL
, "EINVAL: %s: %m", "hogehoge") == -EINVAL
);
85 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, -ENOENT
, "ENOENT: %s: %m", "hogehoge") == -ENOENT
);
86 assert_se(log_syntax("unit", LOG_ERR
, "filename", 10, SYNTHETIC_ERRNO(ENOTTY
), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY
);
89 static void test_log_context(void) {
91 char **strv
= STRV_MAKE("FIRST=abc", "SECOND=qrs");
93 LOG_CONTEXT_PUSH("THIRD=pfs");
94 LOG_CONTEXT_PUSH("FOURTH=def");
95 LOG_CONTEXT_PUSH_STRV(strv
);
96 LOG_CONTEXT_PUSH_STRV(strv
);
98 /* Test that the log context was set up correctly. The strv we pushed twice should only
99 * result in one log context which is reused. */
100 assert_se(log_context_num_contexts() == 3);
101 assert_se(log_context_num_fields() == 4);
103 /* Test that everything still works with modifications to the log context. */
109 LOG_CONTEXT_PUSH("FIFTH=123");
110 LOG_CONTEXT_PUSH_STRV(strv
);
112 /* Check that our nested fields got added correctly. */
113 assert_se(log_context_num_contexts() == 4);
114 assert_se(log_context_num_fields() == 5);
116 /* Test that everything still works in a nested block. */
122 /* Check that only the fields from the nested block got removed. */
123 assert_se(log_context_num_contexts() == 3);
124 assert_se(log_context_num_fields() == 4);
127 assert_se(log_context_num_contexts() == 0);
128 assert_se(log_context_num_fields() == 0);
131 _cleanup_(log_context_unrefp
) LogContext
*ctx
= NULL
;
133 char **strv
= STRV_MAKE("SIXTH=ijn", "SEVENTH=PRP");
134 assert_se(ctx
= log_context_new_strv(strv
, /*owned=*/ false));
136 assert_se(log_context_num_contexts() == 1);
137 assert_se(log_context_num_fields() == 2);
139 /* Test that everything still works with a manually configured log context. */
148 assert_se(strv
= strv_new("ABC", "DEF"));
149 LOG_CONTEXT_CONSUME_STRV(strv
);
151 assert_se(log_context_num_contexts() == 1);
152 assert_se(log_context_num_fields() == 2);
156 /* Test that everything still works with a mixed strv and iov. */
157 struct iovec iov
[] = {
158 IOVEC_MAKE_STRING("ABC=def"),
159 IOVEC_MAKE_STRING("GHI=jkl"),
161 _cleanup_free_
struct iovec_wrapper
*iovw
= iovw_new();
163 assert_se(iovw_consume(iovw
, strdup("MNO=pqr"), STRLEN("MNO=pqr") + 1) == 0);
165 LOG_CONTEXT_PUSH_IOV(iov
, ELEMENTSOF(iov
));
166 LOG_CONTEXT_PUSH_IOV(iov
, ELEMENTSOF(iov
));
167 LOG_CONTEXT_CONSUME_IOV(iovw
->iovec
, iovw
->count
);
168 LOG_CONTEXT_PUSH("STU=vwx");
170 assert_se(log_context_num_contexts() == 3);
171 assert_se(log_context_num_fields() == 4);
179 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
180 LOG_CONTEXT_PUSH_KEY_VALUE("ABC=", "QED");
181 assert_se(log_context_num_contexts() == 1);
182 assert_se(log_context_num_fields() == 1);
189 assert_se(log_context_num_contexts() == 0);
190 assert_se(log_context_num_fields() == 0);
193 static void test_log_prefix(void) {
195 LOG_SET_PREFIX("ABC");
202 LOG_SET_PREFIX("QED");
219 int main(int argc
, char* argv
[]) {
220 test_setup_logging(LOG_DEBUG
);
222 ASSERT_TRUE(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL
)));
223 ASSERT_TRUE(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(-EINVAL
)));
224 assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL
));
225 assert_cc(!IS_SYNTHETIC_ERRNO(-EINVAL
));
226 ASSERT_TRUE(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
227 assert_cc(!IS_SYNTHETIC_ERRNO(0));
228 ASSERT_EQ(ERRNO_VALUE(EINVAL
), EINVAL
);
229 ASSERT_EQ(ERRNO_VALUE(SYNTHETIC_ERRNO(-EINVAL
)), EINVAL
);
231 test_assert_return_is_critical();
234 assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN
), "foo") == -EUCLEAN
);
236 for (int target
= 0; target
< _LOG_TARGET_MAX
; target
++) {
237 log_set_target(target
);