Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log write order error caused by loop queue full when using asynchronous logging system #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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