C

Implementing Logging Support

Printing log

Implement the Qul::Platform::consoleWrite function to support printing logs. This function is called by the Qt Quick Ultralite core 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 constexpr 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 consoleWrite(char character)
{
    logBuffer[logBufferCharCount] = character;
    logBufferCharCount++;

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

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 exec()
{
    while (true) {
        logFlush(); // Flush partially filled log buffer
        const uint64_t timestamp = Platform::update();

        if (timestamp > Platform::currentTimestamp()) {
            // The device may go to a sleep mode until timestamp or interrupt
        }
    }
}

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 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.