Skip to content

Commit 12e8a00

Browse files
committed
Refactor debug config and module logging
Centralizes debug control behind new compile-time config flags and per-module debug headers, replacing widespread `#undef NDEBUG` patterns and migrating many call sites to scoped debug macros (Art-Net, network, display, RDM, showfile, configstore, etc.). The change also renames stack debug defines to `CONFIG_DEBUG_STACK`, updates startup/makefiles accordingly, and includes broad C++ hygiene updates (`[[nodiscard]]`, const-correctness, safer formatting/casts, std wrapper fixes, and small cleanup/refactor touches) to keep behavior consistent while improving maintainability.
1 parent 1527cef commit 12e8a00

191 files changed

Lines changed: 4291 additions & 3229 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

common/include/firmware/debug/debug_config.h

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,30 @@
2626
#ifndef FIRMWARE_DEBUG_DEBUG_CONFIG_H_
2727
#define FIRMWARE_DEBUG_DEBUG_CONFIG_H_
2828

29-
namespace debug {
30-
#ifdef NDEBUG
31-
constexpr bool kIsDebug = false;
29+
namespace debug::config {
30+
#if defined(NDEBUG)
31+
inline constexpr bool kAssertionsEnabled = false;
3232
#else
33-
constexpr bool kIsDebug = true;
33+
inline constexpr bool kAssertionsEnabled = true;
3434
#endif
35-
} // namespace debug
35+
36+
#if defined(CONFIG_DEBUG_TRACE) // DEBUG_ENTRY, DEBUG_EXIT, DEBUG_PRINTF
37+
inline constexpr bool kTraceEnabled = true;
38+
#else
39+
inline constexpr bool kTraceEnabled = false;
40+
#endif
41+
42+
#if defined(CONFIG_DEBUG_DUMP) // Dump(), PrintBits()
43+
inline constexpr bool kDumpEnabled = true;
44+
#else
45+
inline constexpr bool kDumpnabled = false;
46+
#endif
47+
48+
#if defined(CONFIG_DEBUG_STACK) // Stack monitoring
49+
inline constexpr bool kStackMonitoringEnabled = true;
50+
#else
51+
inline constexpr bool kStackMonitoringEnabled = false;
52+
#endif
53+
} // namespace debug::config
3654

3755
#endif // FIRMWARE_DEBUG_DEBUG_CONFIG_H_

common/include/firmware/debug/debug_debug.h

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,47 @@
2626
#ifndef FIRMWARE_DEBUG_DEBUG_H_
2727
#define FIRMWARE_DEBUG_DEBUG_H_
2828

29-
#if !defined(NDEBUG)
3029
#include <cstdio>
3130
#include <source_location>
3231

33-
#define DEBUG_ENTRY() \
34-
do { \
35-
const std::source_location loc = std::source_location::current(); \
36-
printf("-> %s(%u):%s\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name()); \
37-
} while (0)
32+
#include "firmware/debug/debug_config.h"
3833

39-
#define DEBUG_EXIT() \
40-
do { \
41-
const std::source_location loc = std::source_location::current(); \
42-
printf("<- %s(%u):%s\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name()); \
43-
} while (0)
34+
#define DEBUG_ENTRY() \
35+
do { \
36+
if constexpr (::debug::config::kTraceEnabled) { \
37+
const auto location = std::source_location::current(); \
38+
printf("-> %s(%u):%s\n", \
39+
location.file_name(), \
40+
static_cast<unsigned>(location.line()), \
41+
location.function_name()); \
42+
} \
43+
} while (false)
4444

45-
#define DEBUG_PRINTF(fmt, ...) \
46-
do { \
47-
const std::source_location loc = std::source_location::current(); \
48-
printf(" %s(%u):%s: " fmt "\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name() __VA_OPT__(, ) __VA_ARGS__); \
49-
} while (0)
45+
#define DEBUG_EXIT() \
46+
do { \
47+
if constexpr (::debug::config::kTraceEnabled) { \
48+
const auto location = std::source_location::current(); \
49+
printf("<- %s(%u):%s\n", \
50+
location.file_name(), \
51+
static_cast<unsigned>(location.line()), \
52+
location.function_name()); \
53+
} \
54+
} while (false)
5055

51-
#define DEBUG_PUTS(msg) \
52-
do { \
53-
DEBUG_PRINTF("%s", (msg)); \
54-
} while (0)
56+
#define DEBUG_PRINTF(format, ...) \
57+
do { \
58+
if constexpr (::debug::config::kTraceEnabled) { \
59+
const auto location = std::source_location::current(); \
60+
printf(" %s(%u):%s: " format "\n", \
61+
location.file_name(), \
62+
static_cast<unsigned>(location.line()), \
63+
location.function_name() __VA_OPT__(, ) __VA_ARGS__); \
64+
} \
65+
} while (false)
5566

56-
#else
57-
58-
#define DEBUG_ENTRY() \
59-
do { \
60-
} while (0)
61-
62-
#define DEBUG_EXIT() \
63-
do { \
64-
} while (0)
65-
66-
#define DEBUG_PRINTF(...) \
67-
do { \
68-
} while (0)
69-
70-
#define DEBUG_PUTS(...) \
71-
do { \
72-
} while (0)
73-
74-
#endif
67+
#define DEBUG_PUTS(message) \
68+
do { \
69+
DEBUG_PRINTF("%s", (message)); \
70+
} while (false)
7571

7672
#endif // FIRMWARE_DEBUG_DEBUG_H_

common/include/firmware/debug/debug_dump.h

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,76 @@
3434

3535
namespace debug {
3636
inline void Dump([[maybe_unused]] const void* data, [[maybe_unused]] uint32_t size) {
37-
if constexpr (kIsDebug) {
38-
constexpr uint32_t kCharsPerLine = 16;
39-
constexpr uint32_t kBytesPerGroup = 8; // Visual separator every 8 bytes
40-
const auto* ptr = reinterpret_cast<const uint8_t*>(data);
41-
uint32_t chars = 0;
37+
if constexpr (!config::kDumpnabled) {
38+
return;
39+
}
4240

43-
printf("%p:%u\n", data, static_cast<unsigned>(size));
41+
if (size == 0) {
42+
puts("<empty>");
43+
return;
44+
}
4445

45-
do {
46-
printf("%04x ", static_cast<unsigned>(chars));
46+
if (data == nullptr) {
47+
puts("<null>");
48+
return;
49+
}
50+
constexpr uint32_t kCharsPerLine = 16;
51+
constexpr uint32_t kBytesPerGroup = 8; // Visual separator every 8 bytes
52+
const auto* ptr = reinterpret_cast<const uint8_t*>(data);
53+
uint32_t chars = 0;
4754

48-
uint32_t chars_this_line = 0;
49-
const auto* line_start_ptr = ptr;
55+
printf("%p:%u\n", data, static_cast<unsigned>(size));
5056

51-
while ((chars_this_line < kCharsPerLine) && (chars < size)) {
52-
if (chars_this_line % kBytesPerGroup == 0) {
53-
printf(" ");
54-
}
57+
do {
58+
printf("%04x ", static_cast<unsigned>(chars));
5559

56-
printf("%02x ", *ptr);
60+
uint32_t chars_this_line = 0;
61+
const auto* line_start_ptr = ptr;
5762

58-
chars_this_line++;
59-
chars++;
60-
ptr++;
63+
while ((chars_this_line < kCharsPerLine) && (chars < size)) {
64+
if (chars_this_line % kBytesPerGroup == 0) {
65+
printf(" ");
6166
}
6267

63-
auto chars_dot_line = chars_this_line;
68+
printf("%02x ", *ptr);
69+
70+
chars_this_line++;
71+
chars++;
72+
ptr++;
73+
}
6474

65-
for (; chars_this_line < kCharsPerLine; chars_this_line++) {
66-
if (chars_this_line % kBytesPerGroup == 0) {
67-
printf(" ");
68-
}
69-
printf(" ");
75+
auto chars_dot_line = chars_this_line;
76+
77+
for (; chars_this_line < kCharsPerLine; chars_this_line++) {
78+
if (chars_this_line % kBytesPerGroup == 0) {
79+
printf(" ");
7080
}
81+
printf(" ");
82+
}
7183

72-
chars_this_line = 0;
84+
chars_this_line = 0;
7385

74-
while (chars_this_line < chars_dot_line) {
75-
if (chars_this_line % kBytesPerGroup == 0) {
76-
printf(" ");
77-
}
86+
while (chars_this_line < chars_dot_line) {
87+
if (chars_this_line % kBytesPerGroup == 0) {
88+
printf(" ");
89+
}
7890

79-
int character = *line_start_ptr;
80-
if (0 != isprint(character)) {
81-
printf("%c", character);
82-
} else {
83-
printf(".");
84-
}
91+
const auto kCharacter = static_cast<unsigned char>(*line_start_ptr);
8592

86-
chars_this_line++;
87-
line_start_ptr++;
93+
if (std::isprint(kCharacter) != 0) {
94+
printf("%c", static_cast<int>(kCharacter));
95+
} else {
96+
putchar('.');
8897
}
8998

90-
puts("");
99+
chars_this_line++;
100+
line_start_ptr++;
101+
}
91102

92-
} while (chars < size);
93-
}
103+
puts("");
104+
105+
} while (chars < size);
94106
}
95107
} // namespace debug
96108

97-
#endif /* COMMON_DEBUG_DEBUG_DUMP_H_ */
109+
#endif // COMMON_DEBUG_DEBUG_DUMP_H_

common/include/firmware/debug/debug_printbits.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@
3232
#include "firmware/debug/debug_config.h"
3333

3434
namespace debug {
35-
inline void PrintBits([[maybe_unused]] uint32_t u) {
36-
if constexpr (kIsDebug) {
37-
printf("%.8x ", static_cast<unsigned>(u));
38-
uint32_t bit = 1U << 31;
39-
40-
for (uint32_t i = 0; i < 32; i++) {
41-
if ((bit & u) == bit) {
42-
uint32_t bit_number = 31 - i;
43-
printf("%-2u ", static_cast<unsigned>(bit_number));
44-
}
45-
bit = bit >> 1;
46-
}
35+
inline void PrintBits([[maybe_unused]] uint32_t value) {
36+
if constexpr (!config::kTraceEnabled) {
37+
return;
38+
}
39+
40+
printf("%.8x ", static_cast<unsigned>(value));
4741

48-
puts("");
42+
for (int bit_number = 31; bit_number >= 0; --bit_number) {
43+
const auto kMask = uint32_t{1} << bit_number;
44+
45+
if ((value & kMask) != 0U) {
46+
printf("%d ", bit_number);
47+
}
4948
}
49+
50+
putchar('\n');
5051
}
5152
} // namespace debug
5253

common/include/firmware/debug/debug_stack.h

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,70 +31,93 @@
3131
#include <cassert>
3232

3333
#include "timing.h"
34+
#include "firmware/debug/debug_config.h"
3435

3536
extern unsigned char stack_low;
3637
extern unsigned char _sp; // NOLINT
3738

3839
namespace debug::stack {
39-
inline static constexpr uint32_t kMagicWord = 0xABCDABCD;
40+
constexpr uint32_t kMagicWord = 0xABCDABCD;
41+
namespace implementation {
4042

4143
inline void Print() {
42-
static uint32_t s_used_bytes_previous;
43-
const auto* start = reinterpret_cast<uint32_t*>(&stack_low);
44-
const auto* end = reinterpret_cast<uint32_t*>(&_sp);
45-
assert(end > start);
46-
const auto kSize = static_cast<uint32_t>(end - start);
47-
48-
const auto* ptr = start;
44+
if constexpr (!config::kStackMonitoringEnabled) {
45+
return;
46+
}
4947

50-
while (ptr < end) {
48+
static uint32_t s_used_bytes_previous;
49+
const auto* start_address = reinterpret_cast<uint32_t*>(&stack_low);
50+
const auto* end_address = reinterpret_cast<uint32_t*>(&_sp);
51+
assert(end_address > start_address);
52+
const auto kSizeWords = static_cast<uint32_t>(end_address - start_address);
53+
const auto kSizeBytes = kSizeWords * sizeof(uint32_t);
54+
const auto* ptr = start_address;
55+
56+
while (ptr < end_address) {
5157
if (*ptr != kMagicWord) {
5258
break;
5359
}
5460
ptr++;
5561
}
5662

57-
const auto kUsedBytes = static_cast<uint32_t>(4 * (end - ptr));
58-
const auto kFreeBytes = static_cast<uint32_t>(4 * (ptr - start));
59-
const auto kFreePct = (static_cast<uint32_t>(ptr - start) * 100U) / kSize;
63+
const auto kUsedBytes = static_cast<uint32_t>(end_address - ptr) * sizeof(uint32_t);
64+
const auto kFreeBytes = static_cast<uint32_t>(ptr - start_address) * sizeof(uint32_t);
65+
const auto kFreePct = static_cast<uint32_t>((static_cast<uint64_t>(ptr - start_address) * 100U) / kSizeWords);
6066

6167
if (s_used_bytes_previous != kUsedBytes) {
6268
s_used_bytes_previous = kUsedBytes;
6369

64-
if (kFreePct == 0) {
70+
constexpr uint32_t kCriticalFreePercent = 5;
71+
constexpr uint32_t kWarningFreePercent = 15;
72+
73+
if (kFreePct <= kCriticalFreePercent) {
6574
printf("\x1b[31m");
66-
} else if (kFreePct == 1) {
75+
} else if (kFreePct <= kWarningFreePercent) {
6776
printf("\x1b[33m");
6877
} else {
6978
printf("\x1b[34m");
7079
}
7180

72-
#ifndef NDEBUG
73-
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]",
74-
static_cast<unsigned>(kSize / (1024 / 4)),
75-
reinterpret_cast<const void*>(start),
76-
reinterpret_cast<const void*>(ptr),
77-
reinterpret_cast<const void*>(end),
78-
static_cast<unsigned>(kUsedBytes),
79-
static_cast<unsigned>(kFreeBytes),
80-
static_cast<unsigned>(kFreePct));
81-
#else
82-
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSize / (1024 / 4)),
83-
static_cast<unsigned>(kUsedBytes),
84-
static_cast<unsigned>(kFreeBytes));
85-
#endif
81+
if constexpr (!config::kAssertionsEnabled) {
82+
printf("Stack: Size %uKB, [%p:%p:%p], Used: %u, Free: %u [%u]",
83+
static_cast<unsigned>(kSizeBytes / 1024U),
84+
reinterpret_cast<const void*>(start_address),
85+
reinterpret_cast<const void*>(ptr),
86+
reinterpret_cast<const void*>(end_address),
87+
static_cast<unsigned>(kUsedBytes),
88+
static_cast<unsigned>(kFreeBytes),
89+
static_cast<unsigned>(kFreePct));
90+
} else {
91+
printf("Stack: Size %uKB, Used: %u, Free: %u", static_cast<unsigned>(kSizeBytes / 1024U), static_cast<unsigned>(kUsedBytes), static_cast<unsigned>(kFreeBytes));
92+
}
8693
printf("\x1b[39m\n");
8794
}
8895
}
8996

9097
inline void Run() {
98+
if constexpr (!config::kStackMonitoringEnabled) {
99+
return;
100+
}
101+
91102
static uint32_t s_millis_previous;
92103
const auto kMillis = timing::Millis();
93104
if (kMillis - s_millis_previous >= 1000U) {
94105
s_millis_previous = kMillis;
95106
Print();
96107
}
97108
}
109+
} // namespace implementation
110+
inline void Print() {
111+
if constexpr (debug::config::kStackMonitoringEnabled) {
112+
implementation::Print();
113+
}
114+
}
115+
116+
inline void Run() {
117+
if constexpr (debug::config::kStackMonitoringEnabled) {
118+
implementation::Run();
119+
}
120+
}
98121
} // namespace debug::stack
99122

100123
#endif // FIRMWARE_DEBUG_DEBUG_STACK_H_

0 commit comments

Comments
 (0)