Skip to content

Commit

Permalink
NimBLELog allow custom log level colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
thekurtovic committed Dec 16, 2024
1 parent 675d6bb commit 8f48855
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 7 deletions.
111 changes: 109 additions & 2 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,113 @@ config NIMBLE_CPP_LOG_LEVEL
default 3 if NIMBLE_CPP_LOG_LEVEL_INFO
default 4 if NIMBLE_CPP_LOG_LEVEL_DEBUG

config NIMBLE_LOG_OVERRIDE_COLOR
bool "Enable log color override."
default "n"
help
Enabling this option will allow NimBLE log levels to have
specific colors assigned.

menu "NIMBLE Log Override Colors"
depends on NIMBLE_LOG_OVERRIDE_COLOR

choice NIMBLE_LOG_OVERRIDE_COLOR_ERR
prompt "NimBLE CPP log override color Error"
default NIMBLE_LOG_OVERRIDE_COLOR_ERR_NONE
help
Select NimBLE CPP log override error color.

config NIMBLE_LOG_OVERRIDE_COLOR_ERR_NONE
bool "None"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_BLACK
bool "Black"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_RED
bool "Red"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_GREEN
bool "Green"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_YELLOW
bool "Yellow"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_BLUE
bool "Blue"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_PURPLE
bool "Purple"
config NIMBLE_LOG_OVERRIDE_COLOR_ERR_CYAN
bool "Cyan"
endchoice #NIMBLE_LOG_OVERRIDE_COLOR_ERR

choice NIMBLE_LOG_OVERRIDE_COLOR_WARN
prompt "NimBLE CPP log override color Warning"
default NIMBLE_LOG_OVERRIDE_COLOR_WARN_NONE
help
Select NimBLE CPP log override warning color.

config NIMBLE_LOG_OVERRIDE_COLOR_WARN_NONE
bool "None"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_BLACK
bool "Black"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_RED
bool "Red"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_GREEN
bool "Green"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_YELLOW
bool "Yellow"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_BLUE
bool "Blue"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_PURPLE
bool "Purple"
config NIMBLE_LOG_OVERRIDE_COLOR_WARN_CYAN
bool "Cyan"
endchoice #NIMBLE_LOG_OVERRIDE_COLOR_WARN

choice NIMBLE_LOG_OVERRIDE_COLOR_INFO
prompt "NimBLE CPP log override color Info"
default NIMBLE_LOG_OVERRIDE_COLOR_INFO_NONE
help
Select NimBLE CPP log override info color.

config NIMBLE_LOG_OVERRIDE_COLOR_INFO_NONE
bool "None"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_BLACK
bool "Black"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_RED
bool "Red"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_GREEN
bool "Green"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_YELLOW
bool "Yellow"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_BLUE
bool "Blue"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_PURPLE
bool "Purple"
config NIMBLE_LOG_OVERRIDE_COLOR_INFO_CYAN
bool "Cyan"
endchoice #NIMBLE_LOG_OVERRIDE_COLOR_INFO

choice NIMBLE_LOG_OVERRIDE_COLOR_DEBUG
prompt "NimBLE CPP log override color Debug"
default NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_NONE
help
Select NimBLE CPP log override debug color.

config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_NONE
bool "None"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_BLACK
bool "Black"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_RED
bool "Red"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_GREEN
bool "Green"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_YELLOW
bool "Yellow"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_BLUE
bool "Blue"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_PURPLE
bool "Purple"
config NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_CYAN
bool "Cyan"
endchoice #NIMBLE_LOG_OVERRIDE_COLOR_DEBUG
endmenu

config NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT
bool "Show NimBLE return codes as text in debug log."
default "n"
Expand Down Expand Up @@ -77,12 +184,12 @@ config NIMBLE_CPP_DEBUG_ASSERT_ENABLED
This will use approximately 1kB of flash memory.

config NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT
int "FreeRTOS task block bit"
int "FreeRTOS task block bit."
default 31
help
Configure the bit to set in the task notification value when a task is blocked waiting for an event.
This should be set to a bit that is not used by other notifications in the system.

#
# BT config
#
Expand Down
108 changes: 103 additions & 5 deletions src/NimBLELog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,109 @@
# define CONFIG_NIMBLE_CPP_LOG_LEVEL 0
# endif

# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \
# if defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR)
# if CONFIG_LOG_COLORS
# if defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_BLACK)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLACK)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_RED)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_RED)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_GREEN)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_GREEN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_YELLOW)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BROWN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_BLUE)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLUE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_PURPLE)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_PURPLE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_DEBUG_CYAN)
# define NIMBLE_LOG_COLOR_D LOG_COLOR(LOG_COLOR_CYAN)
# else
# define NIMBLE_LOG_COLOR_D
# endif

# if defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_BLACK)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BLACK)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_RED)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_RED)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_GREEN)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_YELLOW)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BROWN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_BLUE)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BLUE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_PURPLE)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_PURPLE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_INFO_CYAN)
# define NIMBLE_LOG_COLOR_I LOG_COLOR(LOG_COLOR_CYAN)
# else
# define NIMBLE_LOG_COLOR_I
# endif

# if defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_BLACK)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BLACK)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_RED)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_RED)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_GREEN)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_GREEN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_YELLOW)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_BLUE)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BLUE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_PURPLE)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_PURPLE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_WARN_CYAN)
# define NIMBLE_LOG_COLOR_W LOG_COLOR(LOG_COLOR_CYAN)
# else
# define NIMBLE_LOG_COLOR_W
# endif

# if defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_BLACK)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BLACK)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_RED)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_GREEN)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_GREEN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_YELLOW)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BROWN)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_BLUE)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BLUE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_PURPLE)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_PURPLE)
# elif defined(CONFIG_NIMBLE_LOG_OVERRIDE_COLOR_ERR_CYAN)
# define NIMBLE_LOG_COLOR_E LOG_COLOR(LOG_COLOR_CYAN)
# else
# define NIMBLE_LOG_COLOR_E
# endif
# else //CONFIG_LOG_COLORS
# define NIMBLE_LOG_COLOR_D
# define NIMBLE_LOG_COLOR_I
# define NIMBLE_LOG_COLOR_W
# define NIMBLE_LOG_COLOR_E
# endif //CONFIG_LOG_COLORS

# define NIMBLE_LOG_FORMAT(letter, format) NIMBLE_LOG_COLOR_##letter #letter " (%lu) %s: " format LOG_RESET_COLOR "\n"

# define NIMBLE_LOG_LEVEL_LOCAL(level, tag, format, ...) \
do { \
if (level==ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, NIMBLE_LOG_FORMAT(E, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
else if (level==ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, NIMBLE_LOG_FORMAT(W, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
else if (level==ESP_LOG_INFO) { esp_log_write(ESP_LOG_INFO, tag, NIMBLE_LOG_FORMAT(I, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
else { esp_log_write(ESP_LOG_DEBUG, tag, NIMBLE_LOG_FORMAT(D, format), esp_log_timestamp(), tag __VA_OPT__(,) __VA_ARGS__); } \
} while(0)

# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \
do { \
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) NIMBLE_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
} while (0)

# else
# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \
do { \
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) ESP_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) ESP_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
} while (0)

# endif /* CONFIG_NIMBLE_LOG_OVERRIDE_COLOR */

# define NIMBLE_LOGD(tag, format, ...) NIMBLE_CPP_LOG_PRINT(ESP_LOG_DEBUG, tag, format, ##__VA_ARGS__)
# define NIMBLE_LOGI(tag, format, ...) NIMBLE_CPP_LOG_PRINT(ESP_LOG_INFO, tag, format, ##__VA_ARGS__)
# define NIMBLE_LOGW(tag, format, ...) NIMBLE_CPP_LOG_PRINT(ESP_LOG_WARN, tag, format, ##__VA_ARGS__)
Expand Down Expand Up @@ -73,6 +171,6 @@
# define NIMBLE_LOGE(tag, format, ...) (void)tag
# endif

# endif /* CONFIG_NIMBLE_CPP_IDF */
#endif /* CONFIG_BT_ENABLED */
#endif /* NIMBLE_CPP_LOG_H_ */
# endif /* CONFIG_NIMBLE_CPP_IDF */
#endif /* CONFIG_BT_ENABLED */
#endif /* NIMBLE_CPP_LOG_H_ */

0 comments on commit 8f48855

Please sign in to comment.