Simplify log queue clearing.

Simply just keep popping the queue until it's empty. No point in using a for loop for this.

Combined some if statements too.
This commit is contained in:
Lioncash 2014-03-16 19:56:57 -04:00
parent fe37a772a4
commit 5edce0eeb6
1 changed files with 38 additions and 41 deletions

View File

@ -205,8 +205,7 @@ void CLogWindow::OnClear(wxCommandEvent& WXUNUSED (event))
{ {
std::lock_guard<std::mutex> lk(m_LogSection); std::lock_guard<std::mutex> lk(m_LogSection);
int msgQueueSize = (int)msgQueue.size(); while (!msgQueue.empty())
for (int i = 0; i < msgQueueSize; i++)
msgQueue.pop(); msgQueue.pop();
} }
@ -280,10 +279,11 @@ void CLogWindow::OnWrapLineCheck(wxCommandEvent& event)
void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event)) void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
{ {
if (!m_LogAccess) return; if (!m_LogAccess || m_ignoreLogTimer)
if (m_ignoreLogTimer) return; return;
UpdateLog(); UpdateLog();
// Scroll to the last line // Scroll to the last line
if (!msgQueue.empty()) if (!msgQueue.empty())
{ {
@ -294,58 +294,55 @@ void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
void CLogWindow::UpdateLog() void CLogWindow::UpdateLog()
{ {
if (!m_LogAccess) return; if (!m_LogAccess || !m_Log)
if (!m_Log) return; return;
// m_LogTimer->Stop(); // m_LogTimer->Stop();
// instead of stopping the timer, let's simply ignore its calls during UpdateLog, // instead of stopping the timer, let's simply ignore its calls during UpdateLog,
// because repeatedly stopping and starting a timer churns memory (and potentially leaks it). // because repeatedly stopping and starting a timer churns memory (and potentially leaks it).
m_ignoreLogTimer = true; m_ignoreLogTimer = true;
if (!msgQueue.empty()) std::lock_guard<std::mutex> lk(m_LogSection);
while (!msgQueue.empty())
{ {
std::lock_guard<std::mutex> lk(m_LogSection); switch (msgQueue.front().first)
int msgQueueSize = (int)msgQueue.size();
for (int i = 0; i < msgQueueSize; i++)
{ {
switch (msgQueue.front().first) case ERROR_LEVEL:
{ m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
case ERROR_LEVEL: break;
m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
break;
case WARNING_LEVEL: case WARNING_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(wxColour(255, 255, 0))); // YELLOW m_Log->SetDefaultStyle(wxTextAttr(wxColour(255, 255, 0))); // YELLOW
break; break;
case NOTICE_LEVEL: case NOTICE_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN)); m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN));
break; break;
case INFO_LEVEL: case INFO_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN)); m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN));
break; break;
case DEBUG_LEVEL: case DEBUG_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY)); m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY));
break; break;
default: default:
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE)); m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
break; break;
}
if (msgQueue.front().second.size())
{
int j = m_Log->GetLastPosition();
m_Log->AppendText(msgQueue.front().second);
// White timestamp
m_Log->SetStyle(j, j + 9, wxTextAttr(*wxWHITE));
}
msgQueue.pop();
} }
} // unlock log
// m_LogTimer->Start(UPDATETIME); if (msgQueue.front().second.size())
{
int i = m_Log->GetLastPosition();
m_Log->AppendText(msgQueue.front().second);
// White timestamp
m_Log->SetStyle(i, i + 9, wxTextAttr(*wxWHITE));
}
msgQueue.pop();
}
m_ignoreLogTimer = false; m_ignoreLogTimer = false;
} }