Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 :
3 : #include <stddef.h>
4 : #include <unistd.h>
5 :
6 : #include "format-util.h"
7 : #include "log.h"
8 : #include "process-util.h"
9 : #include "string-util.h"
10 : #include "util.h"
11 :
12 : assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_FTP | LOG_DEBUG))
13 : == LOG_REALM_SYSTEMD);
14 : assert_cc(LOG_REALM_REMOVE_LEVEL(LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_LOCAL7 | LOG_DEBUG))
15 : == LOG_REALM_UDEV);
16 : assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, LOG_LOCAL3 | LOG_DEBUG) & LOG_FACMASK)
17 : == LOG_LOCAL3);
18 : assert_cc((LOG_REALM_PLUS_LEVEL(LOG_REALM_UDEV, LOG_USER | LOG_INFO) & LOG_PRIMASK)
19 : == LOG_INFO);
20 :
21 : assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(EINVAL)));
22 : assert_cc(!IS_SYNTHETIC_ERRNO(EINVAL));
23 : assert_cc(IS_SYNTHETIC_ERRNO(SYNTHETIC_ERRNO(0)));
24 : assert_cc(!IS_SYNTHETIC_ERRNO(0));
25 :
26 : #define X10(x) x x x x x x x x x x
27 : #define X100(x) X10(X10(x))
28 : #define X1000(x) X100(X10(x))
29 :
30 1 : static void test_file(void) {
31 1 : log_info("__FILE__: %s", __FILE__);
32 1 : log_info("RELATIVE_SOURCE_PATH: %s", RELATIVE_SOURCE_PATH);
33 1 : log_info("PROJECT_FILE: %s", PROJECT_FILE);
34 :
35 1 : assert(startswith(__FILE__, RELATIVE_SOURCE_PATH "/"));
36 1 : }
37 :
38 9 : static void test_log_struct(void) {
39 9 : log_struct(LOG_INFO,
40 : "MESSAGE=Waldo PID="PID_FMT" (no errno)", getpid_cached(),
41 : "SERVICE=piepapo");
42 :
43 9 : log_struct_errno(LOG_INFO, EILSEQ,
44 : "MESSAGE=Waldo PID="PID_FMT": %m (normal)", getpid_cached(),
45 : "SERVICE=piepapo");
46 :
47 9 : log_struct_errno(LOG_INFO, SYNTHETIC_ERRNO(EILSEQ),
48 : "MESSAGE=Waldo PID="PID_FMT": %m (synthetic)", getpid_cached(),
49 : "SERVICE=piepapo");
50 :
51 9 : log_struct(LOG_INFO,
52 : "MESSAGE=Foobar PID="PID_FMT, getpid_cached(),
53 : "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",
54 : (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 : "SUFFIX=GOT IT");
56 9 : }
57 :
58 9 : static void test_long_lines(void) {
59 9 : log_object_internal(LOG_NOTICE,
60 : EUCLEAN,
61 : X1000("abcd_") ".txt",
62 : 1000000,
63 : X1000("fff") "unc",
64 : "OBJECT=",
65 : X1000("obj_") "ect",
66 : "EXTRA=",
67 : X1000("ext_") "tra",
68 : "asdfasdf %s asdfasdfa", "foobar");
69 9 : }
70 :
71 9 : static void test_log_syntax(void) {
72 9 : assert_se(log_syntax("unit", LOG_ERR, "filename", 10, EINVAL, "EINVAL: %s: %m", "hogehoge") == -EINVAL);
73 9 : assert_se(log_syntax("unit", LOG_ERR, "filename", 10, -ENOENT, "ENOENT: %s: %m", "hogehoge") == -ENOENT);
74 9 : assert_se(log_syntax("unit", LOG_ERR, "filename", 10, SYNTHETIC_ERRNO(ENOTTY), "ENOTTY: %s: %m", "hogehoge") == -ENOTTY);
75 9 : }
76 :
77 1 : int main(int argc, char* argv[]) {
78 : int target;
79 :
80 1 : test_file();
81 :
82 10 : for (target = 0; target < _LOG_TARGET_MAX; target++) {
83 9 : log_set_target(target);
84 9 : log_open();
85 :
86 9 : test_log_struct();
87 9 : test_long_lines();
88 9 : test_log_syntax();
89 : }
90 :
91 1 : assert_se(log_info_errno(SYNTHETIC_ERRNO(EUCLEAN), "foo") == -EUCLEAN);
92 :
93 1 : return 0;
94 : }
|