Skip to content

Conversation

@Yuichi1001
Copy link

@Yuichi1001 Yuichi1001 commented Apr 2, 2024

PR描述

修复了使用异步日志系统时由于循环队列已满造成的日志写入顺序错误

理由

假如日志系统使用的是异步模式,写入日志的原代码如下:

    if (m_is_async && !m_log_queue->full())
    {
        m_log_queue->push(log_str);
    }
    else
    {
        m_mutex.lock();
        fputs(log_str.c_str(), m_fp);
        m_mutex.unlock();
    }

此时如果循环队列已满,代码就会执行同步模式的那个分支,直接将现在产生的日志写入日志系统。但是循环队列中仍然有之前还没写入的日志,由此造成了日志写入顺序的错误和日志模式的混乱。

解决方法

为循环队列添加自适应扩容功能,如果循环队列已满,将自动进行扩容。此时原来的日志写入就无须判断m_log_queue->full()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant