Skip to content

Commit 9a1ae67

Browse files
thekurtovich2zero
authored andcommitted
NimBLELog allow custom log level colors.
1 parent 6ee5174 commit 9a1ae67

File tree

2 files changed

+213
-8
lines changed

2 files changed

+213
-8
lines changed

Kconfig

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,113 @@ config NIMBLE_CPP_LOG_LEVEL
2626
default 3 if NIMBLE_CPP_LOG_LEVEL_INFO
2727
default 4 if NIMBLE_CPP_LOG_LEVEL_DEBUG
2828

29+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR
30+
bool "Enable log color override."
31+
default "n"
32+
help
33+
Enabling this option will allow NimBLE log levels to have
34+
specific colors assigned.
35+
36+
menu "NIMBLE Log Override Colors"
37+
depends on NIMBLE_CPP_LOG_OVERRIDE_COLOR
38+
39+
choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR
40+
prompt "NimBLE CPP log override color Error"
41+
default NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_NONE
42+
help
43+
Select NimBLE CPP log override error color.
44+
45+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_NONE
46+
bool "None"
47+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLACK
48+
bool "Black"
49+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_RED
50+
bool "Red"
51+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_GREEN
52+
bool "Green"
53+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_YELLOW
54+
bool "Yellow"
55+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLUE
56+
bool "Blue"
57+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_PURPLE
58+
bool "Purple"
59+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_CYAN
60+
bool "Cyan"
61+
endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR
62+
63+
choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN
64+
prompt "NimBLE CPP log override color Warning"
65+
default NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_NONE
66+
help
67+
Select NimBLE CPP log override warning color.
68+
69+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_NONE
70+
bool "None"
71+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLACK
72+
bool "Black"
73+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_RED
74+
bool "Red"
75+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_GREEN
76+
bool "Green"
77+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_YELLOW
78+
bool "Yellow"
79+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLUE
80+
bool "Blue"
81+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_PURPLE
82+
bool "Purple"
83+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_CYAN
84+
bool "Cyan"
85+
endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN
86+
87+
choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO
88+
prompt "NimBLE CPP log override color Info"
89+
default NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_NONE
90+
help
91+
Select NimBLE CPP log override info color.
92+
93+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_NONE
94+
bool "None"
95+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLACK
96+
bool "Black"
97+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_RED
98+
bool "Red"
99+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_GREEN
100+
bool "Green"
101+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_YELLOW
102+
bool "Yellow"
103+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLUE
104+
bool "Blue"
105+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_PURPLE
106+
bool "Purple"
107+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_CYAN
108+
bool "Cyan"
109+
endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO
110+
111+
choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG
112+
prompt "NimBLE CPP log override color Debug"
113+
default NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_NONE
114+
help
115+
Select NimBLE CPP log override debug color.
116+
117+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_NONE
118+
bool "None"
119+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLACK
120+
bool "Black"
121+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_RED
122+
bool "Red"
123+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_GREEN
124+
bool "Green"
125+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_YELLOW
126+
bool "Yellow"
127+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLUE
128+
bool "Blue"
129+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_PURPLE
130+
bool "Purple"
131+
config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_CYAN
132+
bool "Cyan"
133+
endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG
134+
endmenu
135+
29136
config NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT
30137
bool "Show NimBLE return codes as text in debug log."
31138
default "n"
@@ -77,12 +184,12 @@ config NIMBLE_CPP_DEBUG_ASSERT_ENABLED
77184
This will use approximately 1kB of flash memory.
78185

79186
config NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT
80-
int "FreeRTOS task block bit"
187+
int "FreeRTOS task block bit."
81188
default 31
82189
help
83190
Configure the bit to set in the task notification value when a task is blocked waiting for an event.
84191
This should be set to a bit that is not used by other notifications in the system.
85-
192+
86193
#
87194
# BT config
88195
#
@@ -130,4 +237,4 @@ if IDF_TARGET_ESP32P4
130237
endchoice
131238
endif
132239

133-
endmenu
240+
endmenu

src/NimBLELog.h

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,109 @@
2828
# define CONFIG_NIMBLE_CPP_LOG_LEVEL 0
2929
# endif
3030

31-
# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \
31+
# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR)
32+
# if CONFIG_LOG_COLORS
33+
# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLACK)
34+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLACK)
35+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_RED)
36+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_RED)
37+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_GREEN)
38+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_GREEN)
39+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_YELLOW)
40+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BROWN)
41+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLUE)
42+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLUE)
43+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_PURPLE)
44+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_PURPLE)
45+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_CYAN)
46+
# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_CYAN)
47+
# else
48+
# define NIMBLE_CPP_LOG_COLOR_D
49+
# endif
50+
51+
# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLACK)
52+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BLACK)
53+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_RED)
54+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_RED)
55+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_GREEN)
56+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
57+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_YELLOW)
58+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BROWN)
59+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLUE)
60+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BLUE)
61+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_PURPLE)
62+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_PURPLE)
63+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_CYAN)
64+
# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_CYAN)
65+
# else
66+
# define NIMBLE_CPP_LOG_COLOR_I
67+
# endif
68+
69+
# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLACK)
70+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BLACK)
71+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_RED)
72+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_RED)
73+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_GREEN)
74+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_GREEN)
75+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_YELLOW)
76+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
77+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLUE)
78+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BLUE)
79+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_PURPLE)
80+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_PURPLE)
81+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_CYAN)
82+
# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_CYAN)
83+
# else
84+
# define NIMBLE_CPP_LOG_COLOR_W
85+
# endif
86+
87+
# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLACK)
88+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BLACK)
89+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_RED)
90+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
91+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_GREEN)
92+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_GREEN)
93+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_YELLOW)
94+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BROWN)
95+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLUE)
96+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BLUE)
97+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_PURPLE)
98+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_PURPLE)
99+
# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_CYAN)
100+
# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_CYAN)
101+
# else
102+
# define NIMBLE_CPP_LOG_COLOR_E
103+
# endif
104+
# else //CONFIG_LOG_COLORS
105+
# define NIMBLE_CPP_LOG_COLOR_D
106+
# define NIMBLE_CPP_LOG_COLOR_I
107+
# define NIMBLE_CPP_LOG_COLOR_W
108+
# define NIMBLE_CPP_LOG_COLOR_E
109+
# endif //CONFIG_LOG_COLORS
110+
111+
# define NIMBLE_CPP_LOG_FORMAT(letter, format) NIMBLE_CPP_LOG_COLOR_##letter #letter " (%lu) %s: " format LOG_RESET_COLOR "\n"
112+
113+
# define NIMBLE_CPP_LOG_LEVEL_LOCAL(level, tag, format, ...) \
114+
do { \
115+
if (level==ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, NIMBLE_CPP_LOG_FORMAT(E, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
116+
else if (level==ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, NIMBLE_CPP_LOG_FORMAT(W, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
117+
else if (level==ESP_LOG_INFO) { esp_log_write(ESP_LOG_INFO, tag, NIMBLE_CPP_LOG_FORMAT(I, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
118+
else { esp_log_write(ESP_LOG_DEBUG, tag, NIMBLE_CPP_LOG_FORMAT(D, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
119+
} while(0)
120+
121+
# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \
122+
do { \
123+
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) NIMBLE_CPP_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
124+
} while (0)
125+
126+
# else
127+
# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \
32128
do { \
33-
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) ESP_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
129+
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) ESP_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
34130
} while (0)
35131

132+
# endif /* CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR */
133+
36134
# define NIMBLE_LOGD(tag, format, ...) NIMBLE_CPP_LOG_PRINT(ESP_LOG_DEBUG, tag, format, ##__VA_ARGS__)
37135
# define NIMBLE_LOGI(tag, format, ...) NIMBLE_CPP_LOG_PRINT(ESP_LOG_INFO, tag, format, ##__VA_ARGS__)
38136
# define NIMBLE_LOGW(tag, format, ...) NIMBLE_CPP_LOG_PRINT(ESP_LOG_WARN, tag, format, ##__VA_ARGS__)
@@ -73,6 +171,6 @@
73171
# define NIMBLE_LOGE(tag, format, ...) (void)tag
74172
# endif
75173

76-
# endif /* CONFIG_NIMBLE_CPP_IDF */
77-
#endif /* CONFIG_BT_ENABLED */
78-
#endif /* NIMBLE_CPP_LOG_H_ */
174+
# endif /* CONFIG_NIMBLE_CPP_IDF */
175+
#endif /* CONFIG_BT_ENABLED */
176+
#endif /* NIMBLE_CPP_LOG_H_ */

0 commit comments

Comments
 (0)