ESP32 has a very convenient Logging Library
In Arduino-like projects Normally what we do is that we print out our debug information using the Serial print function.
On the Other hand, the Esp-IDF logging library forces you to print debug/log messages in a constructive way so that later you can easily visualize the logs according to the debug type categories and module groups. If you have experience in Android Application development then you will find this library similar.
ESP-IDF has different Logging functions as listed below
type | MACRO type functions | Verbosity Level |
---|---|---|
Error | ESP_LOGE(tag, format, …) | Lowest |
Warning | ESP_LOGW(tag, format, …) | |
Info | ESP_LOGI(tag, format, …) | |
Debug | ESP_LOGD(tag, format, …) | |
Verbose | ESP_LOGV(tag, format, …) | Highest |
So what you can do is you can categorize your log messages into the above 5 groups according to your need.
- If you have an information log to print, use the ESP_LOGI() function.
- If you have a warning log to print use the ESP_LOGW() function
All the functions expect you to put a TAG of the type character string.
Let's say you have written a Kalman filter class by yourself and in this class, you are going to print different types of debug messages. So before using the macro function to print the logs, create a TAG saying “Kalman''. Then pass this TAG to all of the debug function calls.
The ESP logging library will print these “Kalman” tagged debug messages with the TAG first so that you can easily distinguish these particular module logs, in addition, you also get the warning/info/error kind of categorization. So if you follow this approach in all of your code modules then ultimately the debug dump gets very convenient to see.
static const char\* TAG ="Kalman";
ESP_LOGW(TAG,"threshold exceeded %d baud", error \*100);
ESP_LOGI(TAG,"filtering complete");
By default, the CONFIG_LOG_DEFAULT_LEVEL is set to info level,
which means that the logging statements whose verbosity levels are higher than the Info level will be disabled. So if you use ESP_LOGD( ) or ESP_LOGV( ) functions , their message will not get printed
By moving up or down the debug levels in menuconfig you can set the common debug level setting of all debug messages. Keeping the Debug level at default it is possible to change Debug levels of particular TAG Typed messages at Runtime using the following code
esp_log_level_set("Kalman", ESP_LOG_ERROR); // enable till ERROR level
This function will set the logging level for the “Kanman” TAGGed module to the ERROR level only. But the Other TAGGed module will follow the default level set by the CONFIG_LOG_DEFAULT_LEVEL macro set via menuconfig (compile-time)
Reference
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/log.html
0 comments:
Post a Comment