Printf thread safe

Hi All,
In fact _rtl_printf is not thread safe. But I would like to have linear output of logs without overlays like this:

Wifi::connect BS



Wifi::th 2

start read 1 3

Serial::thserial reade
WIFI is noread goto war thread.

t runningitWebServer s result 0


eriDir 0:/ opeal::thread ned

rtl.cfgstart sessi

rtl.cfg 0 on request 109

mkdir.hwait 0

I fired a little int _rtl_printf(const char *fmt, … with adding next cide:

    if (mutex == NULL) {
        mutex = xSemaphoreCreateMutex();
    xSemaphoreTake(mutex, portMAX_DELAY);

    if (ConfigDebugBuffer == 1 && ConfigDebugBufferGet != NULL) {
        buf = (log_buffer_t *)ConfigDebugBufferGet(fmt);

    if (buf != NULL) {
        int rs = DiagVSprintf(buf->buffer, fmt, ((const int *)&fmt) + 1);
        return rs;
    } else {
        ret = DiagVSprintf(NULL, fmt, ((const int *)&fmt) + 1);

        return ret;

But sometimes overlays still appear. Where else synchronization is needed?

Dear @Tuxford ,

Since Ameba D Standard SDK (ambiot/ambd_sdk) has Wi-Fi and main 2 threads.

Would you like to try your code in Ameba D Arduino SDK (ambiot/ambd_arduino) single-thread environment? Meanwhile, I will try to reproduce your bug in standard sdk.

Thank you