prevent CLogWindow::UpdateLog from constantly burning through timer objects, because it was allocating memory a lot and making it hard to debug some things. I think this should be faster even in release builds, but any speedup is probably too tiny to measure.

This commit is contained in:
nitsuja 2011-12-30 20:51:17 -08:00
parent f0d7b8122f
commit 0d9e87da18
2 changed files with 42 additions and 36 deletions

View File

@ -39,7 +39,7 @@ CLogWindow::CLogWindow(CFrame *parent, wxWindowID id, const wxPoint& pos,
const wxSize& size, long style, const wxString& name)
: wxPanel(parent, id, pos, size, style, name)
, x(0), y(0), winpos(0)
, Parent(parent) , m_LogAccess(true)
, Parent(parent), m_ignoreLogTimer(false), m_LogAccess(true)
, m_Log(NULL), m_cmdline(NULL), m_FontChoice(NULL)
, m_SJISConv(*(wxCSConv*)wxConvCurrent)
{
@ -279,6 +279,7 @@ void CLogWindow::OnWrapLineCheck(wxCommandEvent& event)
void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
{
if (!m_LogAccess) return;
if (m_ignoreLogTimer) return;
UpdateLog();
// Scroll to the last line
@ -294,52 +295,56 @@ void CLogWindow::UpdateLog()
if (!m_LogAccess) return;
if (!m_Log) return;
m_LogTimer->Stop();
// m_LogTimer->Stop();
// 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).
m_ignoreLogTimer = true;
if (!msgQueue.empty())
{
std::lock_guard<std::mutex> lk(m_LogSection);
int msgQueueSize = (int)msgQueue.size();
for (int i = 0; i < msgQueueSize; i++)
{
switch (msgQueue.front().first)
std::lock_guard<std::mutex> lk(m_LogSection);
int msgQueueSize = (int)msgQueue.size();
for (int i = 0; i < msgQueueSize; i++)
{
case ERROR_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
break;
switch (msgQueue.front().first)
{
case ERROR_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
break;
case WARNING_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(wxColour(255, 255, 0))); // YELLOW
break;
case WARNING_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(wxColour(255, 255, 0))); // YELLOW
break;
case NOTICE_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN));
break;
case NOTICE_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN));
break;
case INFO_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN));
break;
case INFO_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN));
break;
case DEBUG_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY));
break;
case DEBUG_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY));
break;
default:
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
break;
default:
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
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();
}
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);
// m_LogTimer->Start(UPDATETIME);
m_ignoreLogTimer = false;
}
void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char *text)

View File

@ -62,6 +62,7 @@ private:
wxFont DefaultFont, MonoSpaceFont;
std::vector<wxFont> LogFont;
wxTimer *m_LogTimer;
bool m_ignoreLogTimer;
LogManager *m_LogManager;
std::queue<std::pair<u8, wxString> > msgQueue;
bool m_writeFile, m_writeConsole, m_writeWindow, m_LogAccess;