| 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 | 24 | UNITTEST { | |
| 18 | 24 | CHECK_STRING_ARRAY(trace_names); | |
| 19 | 24 | } | |
| 20 | |||
| 21 | // Example valid input: "command,status,input" | ||
| 22 | 7 | TraceLoggingFlags trace_flags_from_str(const char *str) | |
| 23 | { | ||
| 24 |
3/4✓ Branch 2 → 3 taken 7 times.
✗ Branch 2 → 6 not taken.
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 6 taken 1 time.
|
7 | if (!str || str[0] == '\0') { |
| 25 | return 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | 6 | bool all = streq(str, "all"); | |
| 29 |
1/2✓ Branch 4 → 5 taken 6 times.
✗ Branch 4 → 6 not taken.
|
6 | return all ? TRACEFLAGS_ALL : STR_TO_BITFLAGS(str, trace_names, true); |
| 30 | } | ||
| 31 | |||
| 32 | 1 | void set_trace_logging_flags(TraceLoggingFlags flags) | |
| 33 | { | ||
| 34 | 1 | BUG_ON(trace_flags); // Should be called at most once | |
| 35 | 1 | BUG_ON(!flags); // ... with non-zero flags | |
| 36 | 1 | BUG_ON(!log_level_enabled(LOG_LEVEL_TRACE)); // ... and only when enabled | |
| 37 | 1 | trace_flags = flags; | |
| 38 | 1 | } | |
| 39 | |||
| 40 | // Return true if *any* 1 bits in `flags` are enabled in `trace_flags` | ||
| 41 | 53 | bool log_trace_enabled(TraceLoggingFlags flags) | |
| 42 | { | ||
| 43 | 53 | return !!(trace_flags & flags); | |
| 44 | } | ||
| 45 | |||
| 46 | 51 | void log_trace(TraceLoggingFlags flags, const char *file, int line, const char *fmt, ...) | |
| 47 | { | ||
| 48 |
2/2✓ Branch 2 → 3 taken 9 times.
✓ Branch 2 → 4 taken 42 times.
|
51 | if (!log_trace_enabled(flags)) { |
| 49 | 9 | return; | |
| 50 | } | ||
| 51 | |||
| 52 | 42 | va_list ap; | |
| 53 | 42 | va_start(ap, fmt); | |
| 54 | 42 | log_msgv(LOG_LEVEL_TRACE, file, line, fmt, ap); | |
| 55 | 42 | va_end(ap); | |
| 56 | } | ||
| 57 | |||
| 58 | #endif // TRACE_LOGGING_ENABLED | ||
| 59 |