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

Using multiple NRF_LOG backends at once

I'd like to log some debug stuff to a mobile app over BLE, when in a connection, using the NRF_LOG library and the Nordic UART service. I have this working just fine without NRF_LOG, just using printf() calls for the log statements and then re-wiring printf() as follows:

  1. When in a BLE connection, take the first 20 characters of the string and write them over BLE using ble_nus_string_send().

  2. Always pass everything to SEGGER_RTT_Write() as well, whether we're in a BLE connection or not.

I'd now like to do the same using NRF_LOG for consistency, as well as getting the log statements in the SDK sources out the same way. At first glance the NRF_LOG library claims to support multiple backends, so I thought I'd write a new backend that just does the BLE stuff. It would send log statements over BLE when in a connection, and do nothing with them when not.

But there doesn't seem to be any way to configure multiple backends at once. Can there be only one nrf_log_backend_init() function linked in at once?

I note that he UART and Segger RTT backends were NOT written as separate backends at all - they're just one backend with the calls all mashed into one implementation.

The SDK docs say "The NRF_LOG_HANDLERS_SET macro can be used to change handlers on the fly." I think this still isn't what I want. I don't want to change the handler, I want to add an additional handler. Would also like to avoid wrapping the default backend handler in my handler and making a further mess of it.

Can this be done?

Parents
  • Hi,

    there is a macro called NRF_LOG_HANDLERS_SET() where you can provide functions for your backend. That would of course replace default handlers from nrf_log_backend_serial.c. You can try to create you backend that will wrap default backend and extend it. Something like:

    bool my_std_handler(....)
    {
    nrf_log_backend_serial_std_handler(...);
    //customize handling
    }
    

    And use NRF_LOG_HANDLERS_SET() to replace the default one.

Reply
  • Hi,

    there is a macro called NRF_LOG_HANDLERS_SET() where you can provide functions for your backend. That would of course replace default handlers from nrf_log_backend_serial.c. You can try to create you backend that will wrap default backend and extend it. Something like:

    bool my_std_handler(....)
    {
    nrf_log_backend_serial_std_handler(...);
    //customize handling
    }
    

    And use NRF_LOG_HANDLERS_SET() to replace the default one.

Children
Related