Why do we put logs in the code? Basically, it has two purposes information and debugging. Logs are very useful during development time, they help a lot, and we can’t imagine code without logs right. But in production firmware we can not put logs as it is, we have to change and removed logs that are unused and confidential. Because if we don’t remove those logs it will make our product vulnerable.
What if we want those logs back. This means imagine we visit the site where our ESP32 based product is running and it has an issue. so we want to see logs used idf.py monitor
, but that log we had to disable or remove. So in this situation how if we have a remote control on a log, By clicking that remote button we enable or disable specific or all the logs, How nice it will be?
How to do it
As I have shown in my previous tutorial and explained about setting log level in ESP-IDF you can check it out by clicking here. That is compiled time log level setting. And also we can set level in run time using esp_log_level_set()
. By using this API we can implement features like enabling disabled logs. So for that, I created a project whose directory hierarchy is as below.
log_sample_1.h & log_sample_2.h
These two files have a simple function that prints logs using ESP_LOGx
macro. And here as we see define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
this macro is used to set the local file's default log level, Important thing is we can’t set a log level higher than this macro. And if we don’t use that macro then the default log level will set as per menucofig
.
index.html
Index HTML has HTML code which has six buttons that demonstrate remote control on logs.
main.c
Inside main. c it create esp32 hotspot with name ESP server and password 123456789. And start the webserver. Now anyone connects with esp32 using wifi and by entering the IP address of esp32 in the browser open the above page.
log_remote_control.h
This file has the function of controlling logs like enabling disabled logs or setting different log levels.
Here is the output of logs when we press all the six buttons on our hosted page.
GitHub Repository
The full code is on below GitHub link.
So now we are ready to get remote control on ESP-IDF logs using esp_log.h
component. If this tutorial will be useful for you please follow for more.
Thank you for reading.