This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF_LOG_INFO: Only the last log statement is printed out

I inherited a Nordic project that is running on the 52832 board with the 15.3 version of the SDK.  The project uses CLI, TWI, and logging.  The developer has to jump through hoops to get all 3 functioning at the same time.  I am now expanding the project.  The issue.  NRF_LOG_INFO works fine if I provide a constant string (e.q. NRF_LOG_INFO("Hello world");)

However, the minute I create a generic logging utility which uses non-stack memory (using data segment memory - statically declared array), only the last logged data is printed several times. 

so something like

XXX_LOG_PRINTF(LOG_INFO, "sensor initialization start.");
.... some more code here.....
XXX_LOG_PRINTF(LOG_INFO, "sensor initialization complete.");
My logging utility can take variable length arguments, etc.  So the "constant strings" you see in the calls above gets copied to a statically declared string which gets passed to the Nordic logging utility.
I only see the last line printed twice.  I have turned off deferred, so I am at a loss.

I have read the chapter on logging and think I understand the buffering concept on the front and back end.   I have tried messing with the sdk_config.h to no avail.   Here are what I think are relevant:

All my CLI commands and output work great.

#define NRF_LOG_DEFERRED 0
#define NRF_LOG_CLI_CMDS 1
#define NRF_LOG_FILTERS_ENABLED 0
The ways I have tried to log the info:
NRF_LOG_INFO("%s\n", NRF_LOG_PUSH((char *)pLogMsg));
or
NRF_LOG_RAW_INFO
I am running on Ubuntu linux, using minicom (which works great for constant strings and cli).  Segger, RTT or any other IDE is not an option.  This must be done via UART<
Please let me know if you need additional info to assist in this.
Thanks,
Jim
  • Hello,

    sorry. I tried your logging function on the non-CLI example, and it worked, so I tried to simplify it, to see if there was something else in your project that could mess up the logging. Then I forgot to change it back when I changed back to the CLI example.

    I talked to someone who has worked closer to our CLI stack. He says that deferred logging and CLI can't be done at the same time. His final solution is to copy the string from the pointer before passing it into the NRF_LOG_RAW_INFO().

    Can you try this approach:

    void logMsg(const char *pMessage)
    {
        int cnt = 0;
    
        memset(message, 0, 256);
        cnt = snprintf(message, 255, pMessage); 
        message[cnt-1] = '\0';
        NRF_LOG_RAW_INFO("%s\n", nrf_log_push(message));
    
        return;
    }

    nrf_log_push() doesn't push the log itself (although the name may sound like it), but it creates a copy of the string from the message pointer.

    NB: In order for this to work, you must set NRF_LOG_DEFERRED to 1.

    Best regards,

    Edvin

  • Ironic..... That did work.  I was using the NRF_LOG_PUSH macro on several attempts, but the second, I reverted the DEFERRED log value back to 1, things started working.  It looks like you need the push and the deferred enabled.  That did resolve the issue.  Thank you for your help.

Related