Skip to content

Commit f827fb9

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 cdd3e87 commit f827fb9

93 files changed

Lines changed: 2041 additions & 1656 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.

bootloader-tftp/Makefile-16x4u-i2c.GD32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ BOARD=BOARD_GD32F450VI
22

33
DEFINES =CONFIG_STORE_USE_I2C
44

5-
DEFINES+=DEBUG_STACK
5+
DEFINES+=CONFIG_DEBUG_STACK
66

77
DEFINES+=NDEBUG
88

bootloader-tftp/Makefile-16x4u.GD32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ BOARD=BOARD_16X4U_PIXEL
33
DEFINES =CONFIG_STORE_USE_SPI
44
DEFINES+=CONFIG_USE_SOFTUART0
55

6-
DEFINES+=DEBUG_STACK
6+
DEFINES+=CONFIG_DEBUG_STACK
77

88
DEFINES+=NDEBUG
99

bootloader-tftp/Makefile-i2c.GD32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ BOARD=BOARD_GD32F450VI
22

33
DEFINES =CONFIG_STORE_USE_I2C
44

5-
DEFINES+=DEBUG_STACK
5+
DEFINES+=CONFIG_DEBUG_STACK
66

77
DEFINES+=NDEBUG
88

bootloader-tftp/Makefile.GD32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ BOARD=BOARD_GD32F450VI
33
DEFINES =CONFIG_STORE_USE_SPI
44
DEFINES+=CONFIG_USE_SOFTUART0
55

6-
DEFINES+=DEBUG_STACK
6+
DEFINES+=CONFIG_DEBUG_STACK
77

88
DEFINES+=NDEBUG
99

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

0 commit comments

Comments
 (0)