2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:43:35 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-08-31 23:09:50 +00:00
|
|
|
|
2014-02-22 22:36:30 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <mutex>
|
|
|
|
#include <queue>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2016-06-24 08:43:46 +00:00
|
|
|
#include <wx/aui/framemanager.h>
|
2014-02-22 22:36:30 +00:00
|
|
|
#include <wx/button.h>
|
|
|
|
#include <wx/checkbox.h>
|
|
|
|
#include <wx/choice.h>
|
|
|
|
#include <wx/colour.h>
|
|
|
|
#include <wx/font.h>
|
|
|
|
#include <wx/panel.h>
|
|
|
|
#include <wx/sizer.h>
|
|
|
|
#include <wx/textctrl.h>
|
|
|
|
#include <wx/timer.h>
|
|
|
|
#include <wx/validate.h>
|
2010-01-19 19:28:27 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/IniFile.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
#include "Common/Logging/ConsoleListener.h"
|
|
|
|
#include "Common/Logging/LogManager.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "DolphinWX/Debugger/DebuggerUIUtil.h"
|
2014-02-22 22:36:30 +00:00
|
|
|
#include "DolphinWX/Frame.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "DolphinWX/LogWindow.h"
|
|
|
|
#include "DolphinWX/WxUtils.h"
|
|
|
|
|
2009-09-01 02:41:48 +00:00
|
|
|
// Milliseconds between msgQueue flushes to wxTextCtrl
|
2009-08-31 23:09:50 +00:00
|
|
|
#define UPDATETIME 200
|
2015-10-07 06:57:04 +00:00
|
|
|
// Max size of msgQueue, old messages will be discarded when there are too many.
|
|
|
|
#define MSGQUEUE_MAX_SIZE 100
|
2009-08-31 23:09:50 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
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), m_Log(nullptr), m_cmdline(nullptr), m_FontChoice(nullptr)
|
2011-01-20 06:05:52 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Bind(wxEVT_CLOSE_WINDOW, &CLogWindow::OnClose, this);
|
|
|
|
Bind(wxEVT_TIMER, &CLogWindow::OnLogTimer, this);
|
2014-11-14 05:46:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_LogManager = LogManager::GetInstance();
|
|
|
|
m_LogManager->RegisterListener(LogListener::LOG_WINDOW_LISTENER, this);
|
2009-08-31 23:09:50 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
CreateGUIControls();
|
2009-08-31 23:09:50 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_LogTimer.SetOwner(this);
|
|
|
|
m_LogTimer.Start(UPDATETIME);
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogWindow::CreateGUIControls()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
IniFile ini;
|
|
|
|
ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
|
|
|
|
|
|
|
IniFile::Section* options = ini.GetOrCreateSection("Options");
|
|
|
|
IniFile::Section* log_window = ini.GetOrCreateSection("LogWindow");
|
|
|
|
log_window->Get("x", &x, Parent->GetSize().GetX() / 2);
|
|
|
|
log_window->Get("y", &y, Parent->GetSize().GetY());
|
|
|
|
log_window->Get("pos", &winpos, wxAUI_DOCK_RIGHT);
|
|
|
|
|
|
|
|
// Set up log listeners
|
|
|
|
int verbosity;
|
|
|
|
options->Get("Verbosity", &verbosity, 0);
|
|
|
|
|
|
|
|
// Ensure the verbosity level is valid
|
|
|
|
if (verbosity < 1)
|
|
|
|
verbosity = 1;
|
|
|
|
if (verbosity > MAX_LOGLEVEL)
|
|
|
|
verbosity = MAX_LOGLEVEL;
|
|
|
|
|
|
|
|
// Get the logger output settings from the config ini file.
|
|
|
|
options->Get("WriteToFile", &m_writeFile, false);
|
|
|
|
options->Get("WriteToWindow", &m_writeWindow, true);
|
|
|
|
|
|
|
|
IniFile::Section* logs = ini.GetOrCreateSection("Logs");
|
|
|
|
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
|
|
|
|
{
|
|
|
|
bool enable;
|
|
|
|
logs->Get(m_LogManager->GetShortName((LogTypes::LOG_TYPE)i), &enable, false);
|
|
|
|
|
|
|
|
if (m_writeWindow && enable)
|
|
|
|
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, LogListener::LOG_WINDOW_LISTENER);
|
|
|
|
else
|
|
|
|
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, LogListener::LOG_WINDOW_LISTENER);
|
|
|
|
|
|
|
|
if (m_writeFile && enable)
|
|
|
|
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, LogListener::FILE_LISTENER);
|
|
|
|
else
|
|
|
|
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, LogListener::FILE_LISTENER);
|
|
|
|
|
|
|
|
m_LogManager->SetLogLevel((LogTypes::LOG_TYPE)i, (LogTypes::LOG_LEVELS)(verbosity));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Font
|
|
|
|
m_FontChoice = new wxChoice(this, wxID_ANY);
|
|
|
|
m_FontChoice->Bind(wxEVT_CHOICE, &CLogWindow::OnFontChange, this);
|
|
|
|
m_FontChoice->Append(_("Default font"));
|
|
|
|
m_FontChoice->Append(_("Monospaced font"));
|
|
|
|
m_FontChoice->Append(_("Selected font"));
|
|
|
|
|
|
|
|
DefaultFont = GetFont();
|
|
|
|
MonoSpaceFont.SetNativeFontInfoUserDesc("lucida console windows-1252");
|
|
|
|
LogFont.push_back(DefaultFont);
|
|
|
|
LogFont.push_back(MonoSpaceFont);
|
|
|
|
LogFont.push_back(DebuggerFont);
|
|
|
|
|
|
|
|
int font;
|
|
|
|
options->Get("Font", &font, 0);
|
|
|
|
m_FontChoice->SetSelection(font);
|
|
|
|
|
|
|
|
// Word wrap
|
|
|
|
bool wrap_lines;
|
|
|
|
options->Get("WrapLines", &wrap_lines, false);
|
|
|
|
m_WrapLine = new wxCheckBox(this, wxID_ANY, _("Word Wrap"));
|
|
|
|
m_WrapLine->Bind(wxEVT_CHECKBOX, &CLogWindow::OnWrapLineCheck, this);
|
|
|
|
m_WrapLine->SetValue(wrap_lines);
|
|
|
|
|
|
|
|
// Log viewer
|
|
|
|
m_Log = CreateTextCtrl(this, wxID_ANY, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY |
|
|
|
|
(wrap_lines ? wxTE_WORDWRAP : wxTE_DONTWRAP));
|
|
|
|
|
|
|
|
// submit row
|
|
|
|
m_cmdline = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
|
|
|
|
wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB);
|
|
|
|
|
|
|
|
// Clear log button
|
|
|
|
m_clear_log_btn =
|
|
|
|
new wxButton(this, wxID_ANY, _("Clear"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT);
|
|
|
|
m_clear_log_btn->Bind(wxEVT_BUTTON, &CLogWindow::OnClear, this);
|
|
|
|
|
|
|
|
// Sizers
|
|
|
|
wxBoxSizer* sTop = new wxBoxSizer(wxHORIZONTAL);
|
|
|
|
sTop->Add(m_clear_log_btn);
|
|
|
|
sTop->Add(m_FontChoice, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 3);
|
|
|
|
sTop->Add(m_WrapLine, 0, wxALIGN_CENTER_VERTICAL);
|
|
|
|
|
|
|
|
sBottom = new wxBoxSizer(wxVERTICAL);
|
|
|
|
PopulateBottom();
|
|
|
|
|
|
|
|
wxBoxSizer* sMain = new wxBoxSizer(wxVERTICAL);
|
|
|
|
sMain->Add(sTop, 0, wxEXPAND);
|
|
|
|
sMain->Add(sBottom, 1, wxEXPAND);
|
|
|
|
SetSizer(sMain);
|
|
|
|
|
|
|
|
m_cmdline->SetFocus();
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CLogWindow::~CLogWindow()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
|
|
|
|
{
|
|
|
|
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, LogListener::LOG_WINDOW_LISTENER);
|
|
|
|
}
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogWindow::OnClose(wxCloseEvent& event)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
SaveSettings();
|
|
|
|
event.Skip();
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogWindow::SaveSettings()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
IniFile ini;
|
|
|
|
ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
|
|
|
|
|
|
|
if (!Parent->g_pCodeWindow)
|
|
|
|
{
|
|
|
|
IniFile::Section* log_window = ini.GetOrCreateSection("LogWindow");
|
|
|
|
log_window->Set("x", x);
|
|
|
|
log_window->Set("y", y);
|
|
|
|
log_window->Set("pos", winpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
IniFile::Section* options = ini.GetOrCreateSection("Options");
|
|
|
|
options->Set("Font", m_FontChoice->GetSelection());
|
|
|
|
options->Set("WrapLines", m_WrapLine->IsChecked());
|
|
|
|
|
|
|
|
ini.Save(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void CLogWindow::OnClear(wxCommandEvent& WXUNUSED(event))
|
2009-08-31 23:09:50 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_Log->Clear();
|
2009-08-31 23:09:50 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_LogSection);
|
|
|
|
while (!msgQueue.empty())
|
|
|
|
msgQueue.pop();
|
|
|
|
}
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 22:47:48 +00:00
|
|
|
void CLogWindow::UnPopulateBottom()
|
2009-09-02 12:09:51 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
sBottom->Detach(m_Log);
|
|
|
|
sBottom->Detach(m_cmdline);
|
2009-09-02 12:09:51 +00:00
|
|
|
}
|
2011-02-18 12:34:24 +00:00
|
|
|
|
2011-03-03 22:47:48 +00:00
|
|
|
void CLogWindow::PopulateBottom()
|
2009-09-02 12:09:51 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
sBottom->Add(m_Log, 1, wxEXPAND | wxSHRINK);
|
|
|
|
sBottom->Add(m_cmdline, 0, wxEXPAND);
|
|
|
|
Layout();
|
2009-09-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
2009-09-07 21:37:27 +00:00
|
|
|
wxTextCtrl* CLogWindow::CreateTextCtrl(wxPanel* parent, wxWindowID id, long Style)
|
2009-09-02 12:09:51 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
wxTextCtrl* TC =
|
|
|
|
new wxTextCtrl(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, Style);
|
2010-12-12 05:38:37 +00:00
|
|
|
#ifdef __APPLE__
|
2016-06-24 08:43:46 +00:00
|
|
|
TC->SetBackgroundColour(*wxLIGHT_GREY);
|
2010-12-12 05:38:37 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
TC->SetBackgroundColour(*wxBLACK);
|
2010-12-12 05:38:37 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_FontChoice && m_FontChoice->GetSelection() < (int)LogFont.size() &&
|
|
|
|
m_FontChoice->GetSelection() >= 0)
|
|
|
|
TC->SetDefaultStyle(
|
|
|
|
wxTextAttr(wxNullColour, wxNullColour, LogFont[m_FontChoice->GetSelection()]));
|
2011-03-03 22:47:48 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return TC;
|
2009-09-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
2011-02-18 12:34:24 +00:00
|
|
|
void CLogWindow::OnFontChange(wxCommandEvent& event)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Update selected font
|
|
|
|
LogFont[LogFont.size() - 1] = DebuggerFont;
|
|
|
|
m_Log->SetStyle(0, m_Log->GetLastPosition(),
|
|
|
|
wxTextAttr(wxNullColour, wxNullColour, LogFont[event.GetSelection()]));
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, LogFont[event.GetSelection()]));
|
2011-02-18 12:34:24 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SaveSettings();
|
2011-02-18 12:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogWindow::OnWrapLineCheck(wxCommandEvent& event)
|
2011-02-19 04:43:51 +00:00
|
|
|
{
|
2010-08-03 03:20:44 +00:00
|
|
|
#ifdef __WXGTK__
|
2016-06-24 08:43:46 +00:00
|
|
|
// Clear the old word wrap state and set the new
|
|
|
|
m_Log->SetWindowStyleFlag(m_Log->GetWindowStyleFlag() ^ (wxTE_WORDWRAP | wxTE_DONTWRAP));
|
2010-08-03 03:20:44 +00:00
|
|
|
#else
|
2016-06-24 08:43:46 +00:00
|
|
|
wxString Text;
|
|
|
|
// Unfortunately wrapping styles can only be changed dynamically with wxGTK
|
|
|
|
// Notice: To retain the colors when changing word wrapping we need to
|
|
|
|
// loop through every letter with GetStyle and then reapply them letter by letter
|
|
|
|
// Prevent m_Log access while it's being destroyed
|
|
|
|
m_LogAccess = false;
|
|
|
|
UnPopulateBottom();
|
|
|
|
Text = m_Log->GetValue();
|
|
|
|
m_Log->Destroy();
|
|
|
|
if (event.IsChecked())
|
|
|
|
m_Log =
|
|
|
|
CreateTextCtrl(this, wxID_ANY, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_WORDWRAP);
|
|
|
|
else
|
|
|
|
m_Log =
|
|
|
|
CreateTextCtrl(this, wxID_ANY, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP);
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
|
|
|
|
m_Log->AppendText(Text);
|
|
|
|
PopulateBottom();
|
|
|
|
m_LogAccess = true;
|
2010-08-03 03:20:44 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
SaveSettings();
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_LogAccess)
|
|
|
|
return;
|
2009-09-02 12:09:51 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
UpdateLog();
|
2014-03-16 23:56:57 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Scroll to the last line
|
|
|
|
if (!msgQueue.empty())
|
|
|
|
{
|
|
|
|
m_Log->ScrollLines(1);
|
|
|
|
m_Log->ShowPosition(m_Log->GetLastPosition());
|
|
|
|
}
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLogWindow::UpdateLog()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!m_LogAccess || !m_Log || msgQueue.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_LogTimer.Stop();
|
|
|
|
|
|
|
|
// This function runs on the main gui thread, and needs to finish in a finite time otherwise
|
|
|
|
// the GUI will lock up, which could be an issue if new messages are flooding in faster than
|
|
|
|
// this function can render them to the screen.
|
|
|
|
// So we limit this function to processing MSGQUEUE_MAX_SIZE messages each time it's called.
|
|
|
|
for (int num = 0; num < MSGQUEUE_MAX_SIZE; num++)
|
|
|
|
{
|
|
|
|
u8 log_level;
|
|
|
|
wxString log_msg;
|
|
|
|
|
|
|
|
// We can't hold this mutex while calling Wx functions, due to deadlocks
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_LogSection);
|
|
|
|
|
|
|
|
if (msgQueue.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
log_level = msgQueue.front().first;
|
|
|
|
log_msg = std::move(msgQueue.front().second);
|
|
|
|
msgQueue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (log_level)
|
|
|
|
{
|
|
|
|
case LogTypes::LOG_LEVELS::LERROR:
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LogTypes::LOG_LEVELS::LWARNING:
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxYELLOW));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LogTypes::LOG_LEVELS::LNOTICE:
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LogTypes::LOG_LEVELS::LINFO:
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LogTypes::LOG_LEVELS::LDEBUG:
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (log_msg.size())
|
|
|
|
{
|
|
|
|
int i = m_Log->GetLastPosition();
|
|
|
|
m_Log->AppendText(log_msg);
|
|
|
|
// White timestamp
|
|
|
|
m_Log->SetStyle(i, i + 9, wxTextAttr(*wxWHITE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_LogTimer.Start();
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char* text)
|
2009-08-31 23:09:50 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_LogSection);
|
2011-03-05 06:11:26 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (msgQueue.size() >= MSGQUEUE_MAX_SIZE)
|
|
|
|
msgQueue.pop();
|
2013-03-03 02:38:40 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
msgQueue.push(std::make_pair(u8(level), StrToWxStr(text)));
|
2009-08-31 23:09:50 +00:00
|
|
|
}
|