Skip to content

Commit 61f2e9f

Browse files
committed
Centralize debug gating in shared config
Add `debug_config.h` with a single `debug::kIsDebug` constexpr derived from `NDEBUG`, and use it to guard `Dump` and `PrintBits` via `if constexpr` instead of per-file `#ifdef` blocks. This keeps the APIs always available while still compiling out debug output in release builds. Also updates `debug_debug.h` with the full license header and minor formatting cleanup.
1 parent ab31329 commit 61f2e9f

4 files changed

Lines changed: 124 additions & 68 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @file debug_config.h
3+
*
4+
*/
5+
/* Copyright (C) 2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
#ifndef FIRMWARE_DEBUG_DEBUG_CONFIG_H_
27+
#define FIRMWARE_DEBUG_DEBUG_CONFIG_H_
28+
29+
namespace debug {
30+
#ifdef NDEBUG
31+
constexpr bool kIsDebug = false;
32+
#else
33+
constexpr bool kIsDebug = true;
34+
#endif
35+
} // namespace debug
36+
37+
#endif // FIRMWARE_DEBUG_DEBUG_CONFIG_H_

common/include/firmware/debug/debug_debug.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22
* @file debug_debug.h
33
*
44
*/
5-
/* Copyright (C) 2018-2026 by Arjan van Vught mailto:info@gd32-dmx.org */
5+
/* Copyright (C) 2018-2026 by Arjan van Vught mailto:info@gd32-dmx.org
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
625

726
#ifndef FIRMWARE_DEBUG_DEBUG_H_
827
#define FIRMWARE_DEBUG_DEBUG_H_
@@ -23,9 +42,9 @@
2342
printf("<- %s(%u):%s\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name()); \
2443
} while (0)
2544

26-
#define DEBUG_PRINTF(fmt, ...) \
27-
do { \
28-
const std::source_location loc = std::source_location::current(); \
45+
#define DEBUG_PRINTF(fmt, ...) \
46+
do { \
47+
const std::source_location loc = std::source_location::current(); \
2948
printf(" %s(%u):%s: " fmt "\n", loc.file_name(), static_cast<unsigned>(loc.line()), loc.function_name() __VA_OPT__(, ) __VA_ARGS__); \
3049
} while (0)
3150

common/include/firmware/debug/debug_dump.h

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,68 @@
3030
#include <cstdio>
3131
#include <ctype.h>
3232

33+
#include "firmware/debug/debug_config.h"
34+
3335
namespace debug {
34-
#ifdef NDEBUG
35-
static inline void Dump([[maybe_unused]] const void* data, [[maybe_unused]] uint32_t size) {}
36-
#else
37-
inline void Dump(const void* data, uint32_t size) {
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;
42-
43-
printf("%p:%u\n", data, static_cast<unsigned>(size));
44-
45-
do {
46-
printf("%04x ", static_cast<unsigned>(chars));
47-
48-
uint32_t chars_this_line = 0;
49-
const auto* line_start_ptr = ptr;
50-
51-
while ((chars_this_line < kCharsPerLine) && (chars < size)) {
52-
if (chars_this_line % kBytesPerGroup == 0) {
53-
printf(" ");
54-
}
36+
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;
5542

56-
printf("%02x ", *ptr);
43+
printf("%p:%u\n", data, static_cast<unsigned>(size));
5744

58-
chars_this_line++;
59-
chars++;
60-
ptr++;
61-
}
45+
do {
46+
printf("%04x ", static_cast<unsigned>(chars));
6247

63-
auto chars_dot_line = chars_this_line;
48+
uint32_t chars_this_line = 0;
49+
const auto* line_start_ptr = ptr;
6450

65-
for (; chars_this_line < kCharsPerLine; chars_this_line++) {
66-
if (chars_this_line % kBytesPerGroup == 0) {
67-
printf(" ");
68-
}
69-
printf(" ");
70-
}
51+
while ((chars_this_line < kCharsPerLine) && (chars < size)) {
52+
if (chars_this_line % kBytesPerGroup == 0) {
53+
printf(" ");
54+
}
7155

72-
chars_this_line = 0;
56+
printf("%02x ", *ptr);
7357

74-
while (chars_this_line < chars_dot_line) {
75-
if (chars_this_line % kBytesPerGroup == 0) {
76-
printf(" ");
58+
chars_this_line++;
59+
chars++;
60+
ptr++;
7761
}
7862

79-
int character = *line_start_ptr;
80-
if (0 != isprint(character)) {
81-
printf("%c", character);
82-
} else {
83-
printf(".");
63+
auto chars_dot_line = chars_this_line;
64+
65+
for (; chars_this_line < kCharsPerLine; chars_this_line++) {
66+
if (chars_this_line % kBytesPerGroup == 0) {
67+
printf(" ");
68+
}
69+
printf(" ");
8470
}
8571

86-
chars_this_line++;
87-
line_start_ptr++;
88-
}
72+
chars_this_line = 0;
73+
74+
while (chars_this_line < chars_dot_line) {
75+
if (chars_this_line % kBytesPerGroup == 0) {
76+
printf(" ");
77+
}
78+
79+
int character = *line_start_ptr;
80+
if (0 != isprint(character)) {
81+
printf("%c", character);
82+
} else {
83+
printf(".");
84+
}
85+
86+
chars_this_line++;
87+
line_start_ptr++;
88+
}
8989

90-
puts("");
90+
puts("");
9191

92-
} while (chars < size);
92+
} while (chars < size);
93+
}
9394
}
94-
#endif
9595
} // namespace debug
9696

9797
#endif /* COMMON_DEBUG_DEBUG_DUMP_H_ */

common/include/firmware/debug/debug_printbits.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@
2727
#define COMMON_DEBUG_DEBUG_PRINTBITS_H_
2828

2929
#include <cstdint>
30+
#include <cstdio>
31+
32+
#include "firmware/debug/debug_config.h"
3033

3134
namespace debug {
32-
#ifdef NDEBUG
33-
inline void PrintBits([[maybe_unused]] uint32_t u) {}
34-
#else
35-
#include <cstdio>
36-
inline void PrintBits(uint32_t u) {
37-
printf("%.8x ", static_cast<unsigned>(u));
38-
uint32_t b = 1U << 31;
39-
40-
for (uint32_t i = 0; i < 32; i++) {
41-
if ((b & u) == b) {
42-
uint32_t bit_number = 31 - i;
43-
printf("%-2u ", static_cast<unsigned>(bit_number));
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;
4446
}
45-
b = b >> 1;
46-
}
4747

48-
puts("");
48+
puts("");
49+
}
4950
}
50-
#endif
5151
} // namespace debug
5252

5353
#endif // COMMON_DEBUG_DEBUG_PRINTBITS_H_

0 commit comments

Comments
 (0)