]>
Commit | Line | Data |
---|---|---|
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" |
877d54e9 | 7 | #include "log.h" |
dccca82b | 8 | #include "process-util.h" |
a0b15b41 | 9 | #include "string-util.h" |
de0671ee | 10 | #include "util.h" |
877d54e9 | 11 | |
52d86690 ZJS |
12 | assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL))); |
13 | assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL)); | |
14 | assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0))); | |
15 | assert_cc(!IS_SYNTHETIC_ERRNO(0)); | |
16 | ||
f8e6f4aa ZJS |
17 | #define X10(x) x x x x x x x x x x |
18 | #define X100(x) X10(X10(x)) | |
19 | #define X1000(x) X100(X10(x)) | |
877d54e9 | 20 | |
a0b15b41 ZJS |
21 | static void test_file(void) { |
22 | log_info("__FILE__: %s", __FILE__); | |
23 | log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH); | |
24 | log_info("PROJECT_FILE: %s", PROJECT_FILE); | |
25 | ||
26 | assert(startswith(__FILE__, RELATIVE_SOURCE_PATH "/")); | |
27 | } | |
28 | ||
52d86690 | 29 | static void test_log_struct(void) { |
877d54e9 | 30 | log_struct(LOG_INFO, |
52d86690 | 31 | "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(), |
a1230ff9 | 32 | "SERVICE=piepapo"); |
877d54e9 | 33 | |
52d86690 ZJS |
34 | log_struct_errno(LOG_INFO, EILSEQ, |
35 | "MESSAGE=Waldo PID="PID_FMT": %m (normal)", getpid_cached(), | |
36 | "SERVICE=piepapo"); | |
37 | ||
38 | log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ), | |
39 | "MESSAGE=Waldo PID="PID_FMT": %m (synthetic)", getpid_cached(), | |
40 | "SERVICE=piepapo"); | |
877d54e9 | 41 | |
963ddb91 | 42 | log_struct(LOG_INFO, |
df0ff127 | 43 | "MESSAGE=Foobar PID="PID_FMT, getpid_cached(), |
963ddb91 LP |
44 | "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", |
45 | (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 | 46 | "SUFFIX=GOT IT"); |
f8e6f4aa ZJS |
47 | } |
48 | ||
49 | static void test_long_lines(void) { | |
50 | log_object_internal(LOG_NOTICE, | |
51 | EUCLEAN, | |
52 | X1000("abcd_") ".txt", | |
53 | 1000000, | |
54 | X1000("fff") "unc", | |
55 | "OBJECT=", | |
56 | X1000("obj_") "ect", | |
57 | "EXTRA=", | |
58 | X1000("ext_") "tra", | |
59 | "asdfasdf %s asdfasdfa", "foobar"); | |
60 | } | |
61 | ||
59c1546b YW |
62 | static void test_log_syntax(void) { |
63 | assert_se(log_syntax("unit", LOG_ERR, "filename", 10, EINVAL, "EINVAL: %s: %m", "hogehoge") == -EINVAL); | |
64 | assert_se(log_syntax("unit", LOG_ERR, "filename", 10, -ENOENT, "ENOENT: %s: %m", "hogehoge") == -ENOENT); | |
65 | assert_se(log_syntax("unit", LOG_ERR, "filename", 10, SYNTHETIC_ERRNO(ENOTTY), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY); | |
66 | } | |
67 | ||
f8e6f4aa | 68 | int main(int argc, char* argv[]) { |
a0b15b41 ZJS |
69 | test_file(); |
70 | ||
169d980b ZJS |
71 | assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN); |
72 | ||
73 | for (int target = 0; target < _LOG_TARGET_MAX; target++) { | |
f8e6f4aa ZJS |
74 | log_set_target(target); |
75 | log_open(); | |
76 | ||
52d86690 | 77 | test_log_struct(); |
f8e6f4aa | 78 | test_long_lines(); |
59c1546b | 79 | test_log_syntax(); |
f8e6f4aa | 80 | } |
963ddb91 | 81 | |
877d54e9 LP |
82 | return 0; |
83 | } |