How to change the Connext DDS Micro logging buffer size and the function to write logs and traces

Connext DDS Micro writes logs and traces using macro

/*
 * param buf_ : pointer to buffer to write
 * param len_ : number of bytes to write
 */
OSAPI_LOG_WRITE_BUFFER(buf_,len_)

The default definition of this macro is:

Windows and POSIX

#define OSAPI_LOG_WRITE_BUFFER(buf_,len_) if (_write(1,buf_,len_)){}
VxWorks
#define OSAPI_LOG_WRITE_BUFFER(buf_,len_) UNUSED_ARG(len_); if (printf("%s",buf_)){}

In Windows and POSIX systems the macro just writes to the standard output.

There are two options to change the output destination where Connext DDS Micro logs and traces are written, e.g. serial port, file, etc.:

  1. Change the definition of macro OSAPI_LOG_WRITE_BUFFER as needed. You need to recompiled Connext DDS Micro libraries.
  2. Change Connext DDS Micro logging properties. It can only be done during runtime and must be done before the logging system is initialized. Note that when the domain participant factory is created, it automatically initializes the logging system. For that reason the logging properties have to be changed before the domain participant factory is created.

The later is the preferred method as it is not needed to recompiled Connext DDS Micro libraries nor to change the code.

You can also change the size of the buffer used to write logs and traces by changing the value of field property.max_buffer_size in the log properties. It is possible to change only the buffer size or the log function without changing the other.

If the size of the log buffer is too small, the log entries which do not fit in the buffer will be truncated.

The default log buffer size is 16 KBytes when Connext DDS Micro libraries are compiled with debug information and 128 bytes when the release version of the libraries are compiled.

Example to change the logging function and logging buffer size during runtime:

void Application_Log_write_buffer(const char *buffer, RTI_INT32 length)
{
    /* add here code to write length bytes started in address buffer */
}
/* this has to be done before logging system is initialized */
struct OSAPI_LogProperty property;

OSAPI_Log_get_property(&property);

property.write_buffer = Application_Log_write_buffer;  
property.max_buffer_size = 128;

if (!OSAPI_Log_set_property(&property))  
{
    /* properties can not be set. This should not happen is logging system is not yet initialized */
    /* add code to handle the error */
} 

The default value for property.write_buffer is NULL. In that case the function which write the logs/traces just have a call to macro OSAPI_LOG_WRITE_BUFFER.