NevermindZZT / letter-shell

letter shell
MIT License
1.24k stars 447 forks source link

log缓冲区大小宏定义小于真实格式化输出字符串,导致shell提示符(尾行模式测试的)和打印信息始终只在第一行显示,用户感官为死机的感觉 #152

Open luckyxrq opened 1 year ago

luckyxrq commented 1 year ago

测试环境为

#define     LOG_BUFFER_SIZE    (53-6)              /**< log输出缓冲大小

connect(&m_timer,&QTimer::timeout,this,&MainUI::on_timeout);
m_timer.start(10);

void MainUI::on_timeout()
{
       shell_log_task();
       logError("2023年4月14日15:25:23");
}

建议修改为,完美解决,已测试

void logWrite(Log *log, LogLevel level, const char *fmt, ...)
{
    va_list vargs;
    int len;

#if LOG_USING_LOCK == 1
    logLock(log);
#endif /* LOG_USING_LOCK == 1 */
    va_start(vargs, fmt);
    len = vsnprintf(logBuffer, LOG_BUFFER_SIZE, fmt, vargs);
    va_end(vargs);

    if(len >= LOG_BUFFER_SIZE - 1 || len < 0)
    {
        logBuffer[LOG_BUFFER_SIZE -1] = 0;
        logBuffer[LOG_BUFFER_SIZE -2] ='\n';
        logBuffer[LOG_BUFFER_SIZE -3] = '\r';
        logWriteBuffer(log, level, logBuffer, LOG_BUFFER_SIZE - 1);
    }
    else
    {
        logWriteBuffer(log, level, logBuffer, len);
    }

#if LOG_USING_LOCK == 1
    logUnlock(log);
#endif /* LOG_USING_LOCK == 1 */
}
NevermindZZT commented 1 year ago

vsnprintf 的使用确实有问题,感谢指正