How to show Date and Time in log?

victor.iorinescu
Posts: 2
Joined: Fri Dec 09, 2022 9:16 pm

How to show Date and Time in log?

Postby victor.iorinescu » Fri Dec 09, 2022 9:31 pm

I searched the documentation and there is no mention on how to view the date and time in log. The current second timestamp is unreadable.

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: How to show Date and Time in log?

Postby mbratch » Sun Dec 11, 2022 3:33 am

There's a setting in the SDK config for this. Just run menuconfig to change it. Of course the date/time won't be correct unless you have a mechanism in your app to set it either manually or from, say, an NTP server.

In my application log messages, since I don't use actual date/time, I include an elapsed time since boot in hh:mm:ss format which I derive from the ms since boot number.

victor.iorinescu
Posts: 2
Joined: Fri Dec 09, 2022 9:16 pm

Re: How to show Date and Time in log?

Postby victor.iorinescu » Thu Dec 15, 2022 6:02 pm

Yes, there are two options:
Milliseconds Since Boot (LOG_TIMESTAMP_SOURCE_RTOS)
System Time (LOG_TIMESTAMP_SOURCE_SYSTEM)
Unfortunately, both of them display a number; I would like something like "YYYY-MM-DD hh:mm:ss"

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: How to show Date and Time in log?

Postby mbratch » Mon Dec 19, 2022 11:35 pm

The ESP32 only knows elapsed time either milliseconds since boot (if you use LOG_TIMESTAMP_SOURCE_RTOS) or HH:MM:SS.SSS since the last power up (if you use LOG_TIMESTAMP_SOURCE_SYSTEM). It has no reference point for the date. You'd have to establish that on every power cycle at least. There will also be some time drift due to the crystal frequency tolerance/variation.

If you want to display an accurate date/time on log messages, you will probably have to roll your own logger, perhaps modify the one that ESP-IDF provides. Then you can hook it into whatever time sync scheme you devise. It may be just a matter of writing your own version of esp_log_system_timestamp.

KoolPox
Posts: 1
Joined: Sun Nov 17, 2024 11:50 am

Re: How to show Date and Time in log?

Postby KoolPox » Sun Nov 17, 2024 12:04 pm

One method to do this is to edit the original routine to be weakly linked (STM do this extensively in their APIs), thus:
[esp32/esp-idf_v5.3/components/log/log_freertos.c]
// plc 2024Nov17 - make weak linkage
#pragma weak esp_log_system_timestamp
char *esp_log_system_timestamp(void)

then you can just copy and paste the original to your code, modify the format to your specific requirements and the linker will favour your version over the weak one.
The downside is that when you update to the latest esp-idf you will have to remember to reedit the pragma (I pur a comment on my copy to remind me what I did)
Enjoy

Who is online

Users browsing this forum: No registered users and 145 guests