Skip to content

Commit ea02eb9

Browse files
committed
Add config option for log verbosity.
1 parent 798726c commit ea02eb9

File tree

5 files changed

+83
-49
lines changed

5 files changed

+83
-49
lines changed

Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
menu "ESP-NimBLE-CPP configuration"
22

3+
choice NIMBLE_CPP_LOG_LEVEL
4+
prompt "NimBLE CPP log verbosity"
5+
default NIMBLE_CPP_LOG_LEVEL_NONE
6+
help
7+
Select NimBLE CPP log verbosity level.
8+
9+
config NIMBLE_CPP_LOG_LEVEL_NONE
10+
bool "No logs"
11+
config NIMBLE_CPP_LOG_LEVEL_ERROR
12+
bool "Error logs"
13+
config NIMBLE_CPP_LOG_LEVEL_WARNING
14+
bool "Warning logs"
15+
config NIMBLE_CPP_LOG_LEVEL_INFO
16+
bool "Info logs"
17+
config NIMBLE_CPP_LOG_LEVEL_DEBUG
18+
bool "Debug logs"
19+
endchoice #NIMBLE_CPP_LOG_LEVEL
20+
21+
config NIMBLE_CPP_LOG_LEVEL
22+
int
23+
default 0 if NIMBLE_CPP_LOG_LEVEL_NONE
24+
default 1 if NIMBLE_CPP_LOG_LEVEL_ERROR
25+
default 2 if NIMBLE_CPP_LOG_LEVEL_WARNING
26+
default 3 if NIMBLE_CPP_LOG_LEVEL_INFO
27+
default 4 if NIMBLE_CPP_LOG_LEVEL_DEBUG
28+
329
config NIMBLE_CPP_ENABLE_RETURN_CODE_TEXT
430
bool "Show NimBLE return codes as text in debug log."
531
default "n"

src/NimBLECharacteristic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ NimBLECharacteristicCallbacks* NimBLECharacteristic::getCallbacks() {
520520
* @param [in] length The length of the data in bytes.
521521
*/
522522
void NimBLECharacteristic::setValue(const uint8_t* data, size_t length) {
523-
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4
523+
#if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4
524524
char* pHex = NimBLEUtils::buildHexData(nullptr, data, length);
525525
NIMBLE_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str());
526526
free(pHex);

src/NimBLELog.h

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,59 +13,67 @@
1313
#if defined(CONFIG_BT_ENABLED)
1414

1515
#if defined(CONFIG_NIMBLE_CPP_IDF) // using esp-idf
16+
# include "esp_log.h"
17+
# ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL
18+
# define CONFIG_NIMBLE_CPP_LOG_LEVEL 0
19+
# endif
1620

17-
#include "esp_log.h"
21+
# define NIMBLE_CPP_LOG_PRINT(level, tag, format, ...) do { \
22+
if (CONFIG_NIMBLE_CPP_LOG_LEVEL >= level) \
23+
ESP_LOG_LEVEL_LOCAL(level, tag, format, ##__VA_ARGS__); \
24+
} while(0)
1825

19-
#define NIMBLE_LOGE(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__)
20-
#define NIMBLE_LOGW(tag, format, ...) ESP_LOGW(tag, format, ##__VA_ARGS__)
21-
#define NIMBLE_LOGI(tag, format, ...) ESP_LOGI(tag, format, ##__VA_ARGS__)
22-
#define NIMBLE_LOGD(tag, format, ...) ESP_LOGD(tag, format, ##__VA_ARGS__)
23-
#define NIMBLE_LOGC(tag, format, ...) ESP_LOGE(tag, format, ##__VA_ARGS__)
26+
# define NIMBLE_LOGD(tag, format, ...) \
27+
NIMBLE_CPP_LOG_PRINT(ESP_LOG_DEBUG, tag, format, ##__VA_ARGS__)
2428

25-
#else // using Arduino
26-
27-
#include "nimble/porting/nimble/include/syscfg/syscfg.h"
28-
#include "nimble/console/console.h"
29-
30-
// If Arduino is being used, strip out the colors and ignore log printing below ui setting.
31-
// Note: because CONFIG_LOG_DEFAULT_LEVEL is set at ERROR in Arduino we must use MODLOG_DFLT(ERROR
32-
// otherwise no messages will be printed above that level.
33-
34-
#ifndef CONFIG_NIMBLE_CPP_DEBUG_LEVEL
35-
#if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL)
36-
#define CONFIG_NIMBLE_CPP_DEBUG_LEVEL CORE_DEBUG_LEVEL
37-
#else
38-
#define CONFIG_NIMBLE_CPP_DEBUG_LEVEL 0
39-
#endif
40-
#endif
29+
# define NIMBLE_LOGI(tag, format, ...) \
30+
NIMBLE_CPP_LOG_PRINT(ESP_LOG_INFO, tag, format, ##__VA_ARGS__)
4131

42-
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4
43-
#define NIMBLE_LOGD( tag, format, ... ) console_printf("D %s: "#format"\n",tag,##__VA_ARGS__)
44-
#else
45-
#define NIMBLE_LOGD( tag, format, ... ) (void)tag
46-
#endif
32+
# define NIMBLE_LOGW(tag, format, ...) \
33+
NIMBLE_CPP_LOG_PRINT(ESP_LOG_WARN, tag, format, ##__VA_ARGS__)
4734

48-
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 3
49-
#define NIMBLE_LOGI( tag, format, ... ) console_printf("I %s: "#format"\n",tag,##__VA_ARGS__)
50-
#else
51-
#define NIMBLE_LOGI( tag, format, ... ) (void)tag
52-
#endif
53-
54-
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 2
55-
#define NIMBLE_LOGW( tag, format, ... ) console_printf("W %s: "#format"\n",tag,##__VA_ARGS__)
56-
#else
57-
#define NIMBLE_LOGW( tag, format, ... ) (void)tag
58-
#endif
59-
60-
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 1
61-
#define NIMBLE_LOGE( tag, format, ... ) console_printf("E %s: "#format"\n",tag,##__VA_ARGS__)
62-
#define NIMBLE_LOGC( tag, format, ... ) console_printf("CRIT %s: "#format"\n",tag,##__VA_ARGS__)
63-
#else
64-
#define NIMBLE_LOGE( tag, format, ... ) (void)tag
65-
#define NIMBLE_LOGC( tag, format, ... ) (void)tag
66-
#endif
35+
# define NIMBLE_LOGE(tag, format, ...) \
36+
NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__)
6737

38+
# define NIMBLE_LOGC(tag, format, ...) \
39+
NIMBLE_CPP_LOG_PRINT(ESP_LOG_ERROR, tag, format, ##__VA_ARGS__)
6840

41+
#else // using Arduino
42+
# include "nimble/porting/nimble/include/syscfg/syscfg.h"
43+
# include "nimble/console/console.h"
44+
# ifndef CONFIG_NIMBLE_CPP_LOG_LEVEL
45+
# if defined(ARDUINO_ARCH_ESP32) && defined(CORE_DEBUG_LEVEL)
46+
# define CONFIG_NIMBLE_CPP_LOG_LEVEL CORE_DEBUG_LEVEL
47+
# else
48+
# define CONFIG_NIMBLE_CPP_LOG_LEVEL 0
49+
# endif
50+
# endif
51+
52+
# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4
53+
# define NIMBLE_LOGD( tag, format, ... ) console_printf("D %s: "#format"\n",tag,##__VA_ARGS__)
54+
# else
55+
# define NIMBLE_LOGD( tag, format, ... ) (void)tag
56+
# endif
57+
58+
# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 3
59+
# define NIMBLE_LOGI( tag, format, ... ) console_printf("I %s: "#format"\n",tag,##__VA_ARGS__)
60+
# else
61+
# define NIMBLE_LOGI( tag, format, ... ) (void)tag
62+
# endif
63+
64+
# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 2
65+
# define NIMBLE_LOGW( tag, format, ... ) console_printf("W %s: "#format"\n",tag,##__VA_ARGS__)
66+
# else
67+
# define NIMBLE_LOGW( tag, format, ... ) (void)tag
68+
# endif
69+
70+
# if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 1
71+
# define NIMBLE_LOGE( tag, format, ... ) console_printf("E %s: "#format"\n",tag,##__VA_ARGS__)
72+
# define NIMBLE_LOGC( tag, format, ... ) console_printf("CRIT %s: "#format"\n",tag,##__VA_ARGS__)
73+
# else
74+
# define NIMBLE_LOGE( tag, format, ... ) (void)tag
75+
# define NIMBLE_LOGC( tag, format, ... ) (void)tag
76+
# endif
6977

7078
#endif /* CONFIG_NIMBLE_CPP_IDF */
7179
#endif /* CONFIG_BT_ENABLED */

src/NimBLEServer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void NimBLEServer::start() {
181181
abort();
182182
}
183183

184-
#if CONFIG_NIMBLE_CPP_DEBUG_LEVEL >= 4
184+
#if CONFIG_NIMBLE_CPP_LOG_LEVEL >= 4
185185
ble_gatts_show_local();
186186
#endif
187187
/*** Future use ***

src/nimconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* Values: 0 = NONE, 1 = ERROR, 2 = WARNING, 3 = INFO, 4+ = DEBUG\n
4949
* Uses approx. 32kB of flash memory.
5050
*/
51-
#define CONFIG_NIMBLE_CPP_DEBUG_LEVEL 0
51+
#define CONFIG_NIMBLE_CPP_LOG_LEVEL 0
5252

5353
/** @brief Un-comment to see NimBLE host return codes as text debug log messages.
5454
* Uses approx. 7kB of flash memory.

0 commit comments

Comments
 (0)