From ea02eb9452447bce1e269b0a7b03dfe2b852fb00 Mon Sep 17 00:00:00 2001 From: h2zero Date: Fri, 14 Jan 2022 19:45:48 -0700 Subject: [PATCH] Add config option for log verbosity. --- Kconfig | 26 ++++++++++ src/NimBLECharacteristic.cpp | 2 +- src/NimBLELog.h | 98 +++++++++++++++++++----------------- src/NimBLEServer.cpp | 2 +- src/nimconfig.h | 2 +- 5 files changed, 82 insertions(+), 48 deletions(-) diff --git a/Kconfig b/Kconfig index 069b000..b2a655d 100644 --- a/Kconfig +++ b/Kconfig @@ -1,5 +1,31 @@ menu "ESP-NimBLE-CPP configuration" +choice NIMBLE_CPP_LOG_LEVEL + prompt "NimBLE CPP log verbosity" + default NIMBLE_CPP_LOG_LEVEL_NONE + help + Select NimBLE CPP log verbosity level. + + config NIMBLE_CPP_LOG_LEVEL_NONE + bool "No logs" + config NIMBLE_CPP_LOG_LEVEL_ERROR + bool "Error logs" + config NIMBLE_CPP_LOG_LEVEL_WARNING + bool "Warning logs" + config NIMBLE_CPP_LOG_LEVEL_INFO + bool "Info logs" + config NIMBLE_CPP_LOG_LEVEL_DEBUG + bool "Debug logs" +endchoice #NIMBLE_CPP_LOG_LEVEL + +config NIMBLE_CPP_LOG_LEVEL + int + default 0 if NIMBLE_CPP_LOG_LEVEL_NONE + default 1 if NIMBLE_CPP_LOG_LEVEL_ERROR + default 2 if NIMBLE_CPP_LOG_LEVEL_WARNING + default 3 if NIMBLE_CPP_LOG_LEVEL_INFO + default 4 if NIMBLE_CPP_LOG_LEVEL_DEBUG + config NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT bool "Show NimBLE return codes as text in debug log." default "n" diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index d6e25ac..60e708b 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -520,7 +520,7 @@ NimBLECharacteristicCallbacks* NimBLECharacteristic::getCallbacks() { * @param [in] length The length of the data in bytes. */ void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) { -#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4 +#if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4 char* pHex = NimBLEUtils::buildHexData(nullptr, data, length); NIMBLE_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str()); free(pHex); diff --git a/src/NimBLELog.h b/src/NimBLELog.h index a64e40c..75c3e44 100644 --- a/src/NimBLELog.h +++ b/src/NimBLELog.h @@ -13,59 +13,67 @@ #if defined(CONFIG_BT_ENABLED) #if defined(CONFIG_NIMBLE_CPP_IDF) // using esp-idf +# include "esp_log.h" +# ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL +# define CONFIG_NIMBLE_CPP_LOG_LEVEL 0 +# endif -#include "esp_log.h" +# 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__); \ + } while(0) -#define NIMBLE_LOGE(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__) -#define NIMBLE_LOGW(tag, format, ...) ESP_LOGW(tag, format, ##__VA_ARGS__) -#define NIMBLE_LOGI(tag, format, ...) ESP_LOGI(tag, format, ##__VA_ARGS__) -#define NIMBLE_LOGD(tag, format, ...) ESP_LOGD(tag, format, ##__VA_ARGS__) -#define NIMBLE_LOGC(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__) +# 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__) + +# define NIMBLE_LOGE(tag, format, ...) \ + NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__) + +# define NIMBLE_LOGC(tag, format, ...) \ + NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__) #else // using Arduino +# include "nimble/porting/nimble/include/syscfg/syscfg.h" +# include "nimble/console/console.h" +# ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL +# if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL) +# define CONFIG_NIMBLE_CPP_LOG_LEVEL CORE_DEBUG_LEVEL +# else +# define CONFIG_NIMBLE_CPP_LOG_LEVEL 0 +# endif +# endif -#include "nimble/porting/nimble/include/syscfg/syscfg.h" -#include "nimble/console/console.h" +# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4 +# define NIMBLE_LOGD( tag, format, ... ) console_printf("D %s: "#format"\n",tag,##__VA_ARGS__) +# else +# define NIMBLE_LOGD( tag, format, ... ) (void)tag +# endif -// If Arduino is being used, strip out the colors and ignore log printing below ui setting. -// Note: because CONFIG_LOG_DEFAULT_LEVEL is set at ERROR in Arduino we must use MODLOG_DFLT(ERROR -// otherwise no messages will be printed above that level. - -#ifndef CONFIG_NIMBLE_CPP_DEBUG_LEVEL - #if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL) - #define CONFIG_NIMBLE_CPP_DEBUG_LEVEL CORE_DEBUG_LEVEL - #else - #define CONFIG_NIMBLE_CPP_DEBUG_LEVEL 0 - #endif -#endif - -#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4 -#define NIMBLE_LOGD( tag, format, ... ) console_printf("D %s: "#format"\n",tag,##__VA_ARGS__) -#else -#define NIMBLE_LOGD( tag, format, ... ) (void)tag -#endif - -#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 3 -#define NIMBLE_LOGI( tag, format, ... ) console_printf("I %s: "#format"\n",tag,##__VA_ARGS__) -#else -#define NIMBLE_LOGI( tag, format, ... ) (void)tag -#endif - -#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 2 -#define NIMBLE_LOGW( tag, format, ... ) console_printf("W %s: "#format"\n",tag,##__VA_ARGS__) -#else -#define NIMBLE_LOGW( tag, format, ... ) (void)tag -#endif - -#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 1 -#define NIMBLE_LOGE( tag, format, ... ) console_printf("E %s: "#format"\n",tag,##__VA_ARGS__) -#define NIMBLE_LOGC( tag, format, ... ) console_printf("CRIT %s: "#format"\n",tag,##__VA_ARGS__) -#else -#define NIMBLE_LOGE( tag, format, ... ) (void)tag -#define NIMBLE_LOGC( tag, format, ... ) (void)tag -#endif +# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 3 +# define NIMBLE_LOGI( tag, format, ... ) console_printf("I %s: "#format"\n",tag,##__VA_ARGS__) +# else +# define NIMBLE_LOGI( tag, format, ... ) (void)tag +# endif +# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 2 +# define NIMBLE_LOGW( tag, format, ... ) console_printf("W %s: "#format"\n",tag,##__VA_ARGS__) +# else +# define NIMBLE_LOGW( tag, format, ... ) (void)tag +# endif +# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 1 +# define NIMBLE_LOGE( tag, format, ... ) console_printf("E %s: "#format"\n",tag,##__VA_ARGS__) +# define NIMBLE_LOGC( tag, format, ... ) console_printf("CRIT %s: "#format"\n",tag,##__VA_ARGS__) +# else +# define NIMBLE_LOGE( tag, format, ... ) (void)tag +# define NIMBLE_LOGC( tag, format, ... ) (void)tag +# endif #endif /* CONFIG_NIMBLE_CPP_IDF */ #endif /* CONFIG_BT_ENABLED */ diff --git a/src/NimBLEServer.cpp b/src/NimBLEServer.cpp index 18593d7..fd28582 100644 --- a/src/NimBLEServer.cpp +++ b/src/NimBLEServer.cpp @@ -181,7 +181,7 @@ void NimBLEServer::start() { abort(); } -#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4 +#if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4 ble_gatts_show_local(); #endif /*** Future use *** diff --git a/src/nimconfig.h b/src/nimconfig.h index f73efed..ea1840a 100644 --- a/src/nimconfig.h +++ b/src/nimconfig.h @@ -48,7 +48,7 @@ * Values: 0 = NONE, 1 = ERROR, 2 = WARNING, 3 = INFO, 4+ = DEBUG\n * Uses approx. 32kB of flash memory. */ -#define CONFIG_NIMBLE_CPP_DEBUG_LEVEL 0 +#define CONFIG_NIMBLE_CPP_LOG_LEVEL 0 /** @brief Un-comment to see NimBLE host return codes as text debug log messages. * Uses approx. 7kB of flash memory.