RTC source for log module timestamp

Hi all,

I'm doing some tests with the nRF5340 DK to log with a custom timestamp source. I took the logging exercise 2 example and verified that the log messages are correctly printed with the timestamp associated with the internal clock. However, for my application, I would like to use an RTC as a log timestamp source and have it formatted automatically in the log messages. Based on the exercise, this could be possible since "the logger gets the timestamp by internally calling the kernel function k_cycle_get_32(). This routine returns the current time since bootup (uptime), as measured by the system’s hardware clock. You could change this to return an actual time and date if an external Real-time clock is present on the system". And also, some logging-related functions should help me format the timestamp as I wish.

Given these, I have some questions:

  • Do I have any limitations on what RTC I can use? This is a test, but I'll be using BLE in my real application, and I read that some issues might limit what RTCs are available. 
  • Are there any updated examples of configuring an RTC as a log timestamp source? I'm not able to find any related examples or examples that are not outdated since I'm using the Nordic SDK 2.6.0.
  • Will I be able to use the Message formatting options included in the include/zephyr/logging/log_output.h to format this custom timestamp? 
    Thank you in advance for any help!

Parents Reply Children
  • Hi Hieu, I closed it since the original issue was resolved, and, in the tests I made, minor changes to the timestamp format were necessary. This answer is still helpful, since I now want to format UNIX time to a human-readable format. From what I understand, I should define a log_custom_timesamp_print function, but I'm still a bit confused about the printer and the formatters, and if I should define some custom ones.
    I'll do some further investigation and check if there's an example code on how to integrate it.
    Thank you again for your time and answers!   

  • Hi Valentine,

    No problem. I am glad my answers help. Please feel free to follow-up if you have any further questions on the same topic.

  • Hi Hieu, I have another question regarding the custom timestamp logging. I added the log_output_custom.h library and defined a formatter function like this:

    int UNIX_timestamp_formatter(const struct log_output *output,
            const log_timestamp_t timestamp,
            const log_timestamp_printer_t printer)
        {
            time_t raw_time = (time_t)timestamp;
            struct tm tm_info;

            /* Convert to UTC time */
            gmtime_r(&raw_time, &tm_info);

            /* Format: yyyy-mm-ddThh:mm:ssZ */
            return printer(output, "%04d-%02d-%02dT%02d:%02d:%02dZ",
            tm_info.tm_year + 1900,
            tm_info.tm_mon + 1,
            tm_info.tm_mday,
            tm_info.tm_hour,
            tm_info.tm_min,
            tm_info.tm_sec);
        }

    And use the 
    log_custom_timestamp_set function to associate my custom formatter. The thing is that if I dont do this before any log that uses the timestamp, my code crashes because no formatter is associated. 

    Is there a way to use the default timestamp formatting like [hh:mm:ss.ms,package_number], and once I know everything is configured correctly, use the UNIX_timestamp_formatter?
  • Hi Valentine,

    Did you build with CONFIG_ASSERT set?

    I ran a test and without CONFIG_ASSERT, everything works normally if no timestamp formatter is set yet, except logs don't have timestamps.

    You can also consider using the System Initialization API. I found this seems to work after some tests:

    #if defined(CONFIG_LOG_OUTPUT_FORMAT_CUSTOM_TIMESTAMP)
    
    int test_timestamp_formatter(const struct log_output *output,
    	const log_timestamp_t timestamp,
    	const log_timestamp_printer_t printer)
    {
    	return printer(output, "%04d-test", timestamp);
    }
    
    void test_set_custom_timestamp_formatter(void)
    {
    	/* Set the custom timestamp formatter. */
    	log_custom_timestamp_set(test_timestamp_formatter);
    }
    
    SYS_INIT(test_set_custom_timestamp_formatter, APPLICATION, 32);
    
    #endif /* CONFIG_LOG_OUTPUT_FORMAT_CUSTOM_TIMESTAMP */

    If you need even earlier logging working, you can set other value for the "level" parameter of SYS_INIT, referring to the linked documentation.

Related