From 0e7d3759d94a6a064de0fe1724c486deea71f963 Mon Sep 17 00:00:00 2001 From: thekurtovic <40248206+thekurtovic@users.noreply.github.com> Date: Sat, 14 Dec 2024 18:18:53 -0500 Subject: [PATCH] NimBLELog allow custom log level colors. --- Kconfig | 113 ++++++++++++++++++++++++++++++++++++++++++++++-- src/NimBLELog.h | 108 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 213 insertions(+), 8 deletions(-) diff --git a/Kconfig b/Kconfig index 2a603c6b..88db6e5c 100644 --- a/Kconfig +++ b/Kconfig @@ -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_CPP_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_CPP_LOG_OVERRIDE_COLOR + + choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR + prompt "NimBLE CPP log override color Error" + default NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_NONE + help + Select NimBLE CPP log override error color. + + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_NONE + bool "None" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLACK + bool "Black" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_RED + bool "Red" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_GREEN + bool "Green" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_YELLOW + bool "Yellow" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLUE + bool "Blue" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_PURPLE + bool "Purple" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_CYAN + bool "Cyan" + endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR + + choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN + prompt "NimBLE CPP log override color Warning" + default NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_NONE + help + Select NimBLE CPP log override warning color. + + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_NONE + bool "None" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLACK + bool "Black" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_RED + bool "Red" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_GREEN + bool "Green" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_YELLOW + bool "Yellow" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLUE + bool "Blue" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_PURPLE + bool "Purple" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_CYAN + bool "Cyan" + endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN + + choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO + prompt "NimBLE CPP log override color Info" + default NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_NONE + help + Select NimBLE CPP log override info color. + + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_NONE + bool "None" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLACK + bool "Black" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_RED + bool "Red" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_GREEN + bool "Green" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_YELLOW + bool "Yellow" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLUE + bool "Blue" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_PURPLE + bool "Purple" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_CYAN + bool "Cyan" + endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO + + choice NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG + prompt "NimBLE CPP log override color Debug" + default NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_NONE + help + Select NimBLE CPP log override debug color. + + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_NONE + bool "None" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLACK + bool "Black" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_RED + bool "Red" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_GREEN + bool "Green" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_YELLOW + bool "Yellow" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLUE + bool "Blue" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_PURPLE + bool "Purple" + config NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_CYAN + bool "Cyan" + endchoice #NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG +endmenu + config NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT bool "Show NimBLE return codes as text in debug log." default "n" @@ -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 # @@ -130,4 +237,4 @@ if IDF_TARGET_ESP32P4 endchoice endif -endmenu +endmenu \ No newline at end of file diff --git a/src/NimBLELog.h b/src/NimBLELog.h index d9974d3e..618465ec 100644 --- a/src/NimBLELog.h +++ b/src/NimBLELog.h @@ -28,11 +28,109 @@ # define CONFIG_NIMBLE_CPP_LOG_LEVEL 0 # endif -# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) \ +# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR) +# if CONFIG_LOG_COLORS +# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLACK) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLACK) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_RED) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_RED) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_GREEN) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_GREEN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_YELLOW) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BROWN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_BLUE) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_BLUE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_PURPLE) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_PURPLE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_DEBUG_CYAN) +# define NIMBLE_CPP_LOG_COLOR_D LOG_COLOR(LOG_COLOR_CYAN) +# else +# define NIMBLE_CPP_LOG_COLOR_D +# endif + +# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLACK) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BLACK) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_RED) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_RED) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_GREEN) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_YELLOW) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BROWN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_BLUE) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_BLUE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_PURPLE) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_PURPLE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_INFO_CYAN) +# define NIMBLE_CPP_LOG_COLOR_I LOG_COLOR(LOG_COLOR_CYAN) +# else +# define NIMBLE_CPP_LOG_COLOR_I +# endif + +# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLACK) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BLACK) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_RED) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_RED) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_GREEN) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_GREEN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_YELLOW) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_BLUE) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_BLUE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_PURPLE) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_PURPLE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_WARN_CYAN) +# define NIMBLE_CPP_LOG_COLOR_W LOG_COLOR(LOG_COLOR_CYAN) +# else +# define NIMBLE_CPP_LOG_COLOR_W +# endif + +# if defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLACK) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BLACK) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_RED) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_GREEN) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_GREEN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_YELLOW) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BROWN) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_BLUE) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_BLUE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_PURPLE) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_PURPLE) +# elif defined(CONFIG_NIMBLE_CPP_LOG_OVERRIDE_COLOR_ERR_CYAN) +# define NIMBLE_CPP_LOG_COLOR_E LOG_COLOR(LOG_COLOR_CYAN) +# else +# define NIMBLE_CPP_LOG_COLOR_E +# endif +# else //CONFIG_LOG_COLORS +# define NIMBLE_CPP_LOG_COLOR_D +# define NIMBLE_CPP_LOG_COLOR_I +# define NIMBLE_CPP_LOG_COLOR_W +# define NIMBLE_CPP_LOG_COLOR_E +# endif //CONFIG_LOG_COLORS + +# define NIMBLE_CPP_LOG_FORMAT(letter, format) NIMBLE_CPP_LOG_COLOR_##letter #letter " (%lu) %s: " format LOG_RESET_COLOR "\n" + +# define NIMBLE_CPP_LOG_LEVEL_LOCAL(level, tag, format, ...) \ + do { \ + 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__); } \ + 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__); } \ + 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__); } \ + else { esp_log_write(ESP_LOG_DEBUG, tag, NIMBLE_CPP_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_CPP_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_CPP_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__) @@ -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_ */ \ No newline at end of file