Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "trace.h" | ||
2 | #include "util/array.h" | ||
3 | #include "util/str-util.h" | ||
4 | #include "util/xstring.h" | ||
5 | |||
6 | #if TRACE_LOGGING_ENABLED | ||
7 | |||
8 | // NOLINTNEXTLINE(*-avoid-non-const-global-variables) | ||
9 | static TraceLoggingFlags trace_flags = 0; | ||
10 | |||
11 | static const char trace_names[][8] = { | ||
12 | "command", | ||
13 | "input", | ||
14 | "output", | ||
15 | }; | ||
16 | |||
17 | // Example valid input: "command,status,input" | ||
18 | 168 | TraceLoggingFlags trace_flags_from_str(const char *str) | |
19 | { | ||
20 |
3/4✓ Branch 0 (2→3) taken 168 times.
✗ Branch 1 (2→6) not taken.
✓ Branch 2 (3→4) taken 144 times.
✓ Branch 3 (3→6) taken 24 times.
|
168 | if (!str || str[0] == '\0') { |
21 | return 0; | ||
22 | } | ||
23 | 144 | bool all = streq(str, "all"); | |
24 |
1/2✓ Branch 0 (4→5) taken 144 times.
✗ Branch 1 (4→6) not taken.
|
144 | return all ? TRACEFLAGS_ALL : STR_TO_BITFLAGS(str, trace_names, true); |
25 | } | ||
26 | |||
27 | 24 | UNITTEST { | |
28 | 24 | CHECK_STRING_ARRAY(trace_names); | |
29 | // NOLINTBEGIN(bugprone-assert-side-effect) | ||
30 | 24 | BUG_ON(trace_flags_from_str("output") != TRACEFLAG_OUTPUT); | |
31 | 24 | BUG_ON(trace_flags_from_str(",x,, ,output,,") != TRACEFLAG_OUTPUT); | |
32 | 24 | BUG_ON(trace_flags_from_str("command,input") != (TRACEFLAG_COMMAND | TRACEFLAG_INPUT)); | |
33 | 24 | BUG_ON(trace_flags_from_str("command,inpu") != TRACEFLAG_COMMAND); | |
34 | 24 | BUG_ON(trace_flags_from_str("") != 0); | |
35 | 24 | BUG_ON(trace_flags_from_str(",") != 0); | |
36 | 24 | BUG_ON(trace_flags_from_str("a") != 0); | |
37 | // NOLINTEND(bugprone-assert-side-effect) | ||
38 | 24 | } | |
39 | |||
40 | 1 | void set_trace_logging_flags(TraceLoggingFlags flags) | |
41 | { | ||
42 | 1 | BUG_ON(trace_flags); // Should be called at most once | |
43 | 1 | BUG_ON(!flags); // ... with non-zero flags | |
44 | 1 | BUG_ON(!log_level_enabled(LOG_LEVEL_TRACE)); // ... and only when enabled | |
45 | 1 | trace_flags = flags; | |
46 | 1 | } | |
47 | |||
48 | // Return true if *any* 1 bits in `flags` are enabled in `trace_flags` | ||
49 | 53 | bool log_trace_enabled(TraceLoggingFlags flags) | |
50 | { | ||
51 | 53 | return !!(trace_flags & flags); | |
52 | } | ||
53 | |||
54 | 51 | void log_trace(TraceLoggingFlags flags, const char *file, int line, const char *fmt, ...) | |
55 | { | ||
56 |
2/2✓ Branch 0 (2→3) taken 9 times.
✓ Branch 1 (2→4) taken 42 times.
|
51 | if (!log_trace_enabled(flags)) { |
57 | 9 | return; | |
58 | } | ||
59 | 42 | va_list ap; | |
60 | 42 | va_start(ap, fmt); | |
61 | 42 | log_msgv(LOG_LEVEL_TRACE, file, line, fmt, ap); | |
62 | 42 | va_end(ap); | |
63 | } | ||
64 | |||
65 | #endif // TRACE_LOGGING_ENABLED | ||
66 |