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