C

Implementing Logging Support

Printing log

Implement the PlatformContext::consoleWrite to support printing logs. This method is called by the Qt Quick Ultralite core on the context instance to print single characters and it acts as the output for Qul::PlatformInterface::log.

Example implementation

This example introduces a character buffer in the platform adaptation for logging.

// 80 characters + \r + \n for a typical terminal config
static const uint8_t logBufferLength = 80 + 1 + 1;
static char logBuffer[logBufferLength];
static uint8_t logBufferCharCount = 0;

Buffer content is flushed to the hardware output when the buffer gets full.

void ExamplePlatform::consoleWrite(char character)
{
    logBuffer[logBufferCharCount] = character;
    logBufferCharCount++;

    if (logBufferCharCount == logBufferLength) {
        // Buffer full, flush data
        logFlush();
        logBufferCharCount = 0;
    }
}

Log data is written to UART hardware interface by the logFlush function in this example.

static void logFlush()
{
    if (logBufferCharCount > 0) {
        // HW_UART_Write(...,
        //     logBuffer,
        //     logBufferCharCount);

        // HW_UART_Wait_Write_To_Complete();

        logBufferCharCount = 0;
    }
}

The loop in the exec handles flushing if the last write did not trigger it.

void ExamplePlatform::exec()
{
    while (true) {
        logFlush(); // Flush partially filled log buffer
        const uint64_t timestamp = this->update();

        if (timestamp > Platform::getPlatformInstance()->currentTimestamp()) {
            // The device may yield or go into sleep mode
        }
    }
}

Printing log from the platform adaptation

Platform adaptation can use Qul::PlatformInterface::log to print messages.

PlatformInterface::log("Qt for MCUs!\r\n");

A C API is also provided to support printing logs from C code. qul_printf and qul_vprintf uses the PlatformContext::consoleWrite as the output.

qul_printf("Ultimate performance. Tiny footprint.\r\n");

See also qul_snprintf(), qul_sprintf(), and qul_vsnprintf().

Available under certain Qt licenses.
Find out more.