Resources
https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/system/log.html
Why use the log instead of printf?
- You can selectively disable messages based on the log message type ordering. E.g. disable all messages above ESP_LOGE().
- You can redirect the log messages to other outputs, e.g. a file.
- You can change the logging level at runtime if needed (e.g. product is having a problem, your code could enable all logging and output it to a file).
Writing to log
//Have this locally for your file:
static const char *TAG = "This code area name";
//Log messages in order of importance (you can selectively disable messages from any point downwards in this ordering):
ESP_LOGE(TAG, "Critical errors, software module can not recover on its own");
ESP_LOGW(TAG, "Error conditions from which recovery measures have been taken");
ESP_LOGI(TAG, "Information messages which describe normal flow of events");
ESP_LOGD(TAG, "Extra information which is not necessary for normal use (values, pointers, sizes, etc)");
ESP_LOGV(TAG, "Bigger chunks of debugging information, or frequent messages which can potentially flood the output");
What we do
ESP_LOGE(TAG, "Critical error - operation is not correct");
ESP_LOGW(TAG, "Error condition that developer needs to always see");
ESP_LOGI(TAG, "Information to show when we're debugging this particular code area");
Including values
//Include an INTvalue
ESP_LOGI(TAG, "Switches: %i", (int)MyVariable);
//Include a 2 digit HEX value
ESP_LOGI(TAG, "Switches: 0x%02X", (uint16_t)MyVariable);
//Include a 4 digit HEX value
ESP_LOGI(TAG, "Switches: 0x%04X", (uint16_t)MyVariable);
Including strings
Value format codes
char MyString[10];
ESP_LOGI(TAG, "Switches: %s", MyString);
See here.
Enabling/disabling logs from being output
E.g. selectively including messages from certain files, production release code where you don’t want messages to appear at the serial port or take time away from your application waiting on the TX, etc.
//Log messages in order of importance (when you specify one of these, only messages at this and higher levels of importance will be shown):
ESP_LOG_NONE (No log output)
ESP_LOG_ERROR (Critical errors, software module can not recover on its own)
ESP_LOG_WARN (Error conditions from which recovery measures have been taken)
ESP_LOG_INFO (Information messages which describe normal flow of events)
ESP_LOG_DEBUG (Extra information which is not necessary for normal use (values, pointers, sizes, etc))
ESP_LOG_VERBOSE (Bigger chunks of debugging information, or frequent messages which can potentially flood the output)
Option 1 – before you include “esp_log.h”
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG //<Include before "esp_log.h"
#include "esp_log.h"
Option 2 – at runtime
//----- SET LOGGING LEVEL -----
esp_log_level_set("*", ESP_LOG_NONE); //<<<Default logging level for all tags
esp_log_level_set("OneOfMyTagNames", ESP_LOG_VERBOSE);
esp_log_level_set("AnotherOfMyTagNames", ESP_LOG_WARN);
//(Note that esp_log_level_set() can't raise log level above level set using CONFIG_LOG_MAXIMUM_LEVEL setting in menuconfig).
Logging via JTAG
See here.
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.
7 months ago
For printing a uint32_t you can use ESP_LOGx(TAG, “%lu”, uint32_t_var);