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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions log/block_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,19 @@ class block_queue
m_mutex.lock();
if (m_size >= m_max_size)
{

m_cond.broadcast();
m_mutex.unlock();
return false;
int new_max_size = m_max_size * 2;
T* new_array = new T[new_max_size];
// 复制旧队列元素到新队列
for (int i = 0; i < m_max_size; i++) {
new_array[i] = m_array[(m_front + i + 1) % m_max_size];
}
// 删除旧数组
delete[] m_array;
// 更新队列指针和大小
m_array = new_array;
m_front = -1; // 重置队首位置
m_back = m_size - 1; // 重置队尾位置
m_max_size = new_max_size;
}

m_back = (m_back + 1) % m_max_size;
Expand Down
2 changes: 1 addition & 1 deletion log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void Log::write_log(int level, const char *format, ...)

m_mutex.unlock();

if (m_is_async && !m_log_queue->full())
if (m_is_async)
{
m_log_queue->push(log_str);
}
Expand Down