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