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: 298
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: 298
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.

Who is online

Users browsing this forum: No registered users and 137 guests