1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <cstddef>
#include <mutex>
#include <queue>
#include <utility>
#include <vector>
#include <wx/anybutton.h>
#include <wx/button.h>
#include <wx/chartype.h>
#include <wx/checkbox.h>
#include <wx/choice.h>
#include <wx/colour.h>
#include <wx/defs.h>
#include <wx/event.h>
#include <wx/font.h>
#include <wx/gdicmn.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/string.h>
#include <wx/textctrl.h>
#include <wx/timer.h>
#include <wx/translation.h>
#include <wx/validate.h>
#include <wx/window.h>
#include <wx/windowid.h>
#include <wx/aui/framemanager.h>
#include "Common/Common.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/Logging/ConsoleListener.h"
#include "Common/Logging/LogManager.h"
#include "DolphinWX/Frame.h"
#include "DolphinWX/LogWindow.h"
#include "DolphinWX/WxUtils.h"
#include "DolphinWX/Debugger/DebuggerUIUtil.h"
// Milliseconds between msgQueue flushes to wxTextCtrl
#define UPDATETIME 200
BEGIN_EVENT_TABLE(CLogWindow, wxPanel)
EVT_CLOSE(CLogWindow::OnClose)
EVT_BUTTON(IDM_CLEARLOG, CLogWindow::OnClear)
EVT_CHOICE(IDM_FONT, CLogWindow::OnFontChange)
EVT_CHECKBOX(IDM_WRAPLINE, CLogWindow::OnWrapLineCheck)
EVT_TIMER(IDTM_UPDATELOG, CLogWindow::OnLogTimer)
END_EVENT_TABLE()
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_ignoreLogTimer(false), m_LogAccess(true)
, m_Log(nullptr), m_cmdline(nullptr), m_FontChoice(nullptr)
{
m_LogManager = LogManager::GetInstance();
CreateGUIControls();
m_LogTimer = new wxTimer(this, IDTM_UPDATELOG);
m_LogTimer->Start(UPDATETIME);
}
void CLogWindow::CreateGUIControls()
{
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);
#ifdef _MSC_VER
if (IsDebuggerPresent())
{
options->Get("WriteToDebugger", &m_writeDebugger, true);
}
else
#endif
{
m_writeDebugger = false;
}
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, true);
if (m_writeWindow && enable)
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, this);
else
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, this);
if (m_writeFile && enable)
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, m_LogManager->GetFileListener());
else
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, m_LogManager->GetFileListener());
if (m_writeDebugger && enable)
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, m_LogManager->GetDebuggerListener());
else
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, m_LogManager->GetDebuggerListener());
m_LogManager->SetLogLevel((LogTypes::LOG_TYPE)i, (LogTypes::LOG_LEVELS)(verbosity));
}
// Font
m_FontChoice = new wxChoice(this, IDM_FONT);
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, IDM_WRAPLINE, _("Word Wrap"));
m_WrapLine->SetValue(wrap_lines);
// Log viewer
m_Log = CreateTextCtrl(this, IDM_LOG, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY |
(wrap_lines ? wxTE_WORDWRAP : wxTE_DONTWRAP));
// submit row
m_cmdline = new wxTextCtrl(this, IDM_SUBMITCMD, wxEmptyString, wxDefaultPosition, wxDefaultSize,
wxTE_PROCESS_ENTER | wxTE_PROCESS_TAB);
// Sizers
wxBoxSizer *sTop = new wxBoxSizer(wxHORIZONTAL);
sTop->Add(new wxButton(this, IDM_CLEARLOG, _("Clear"), wxDefaultPosition, wxDefaultSize, wxBU_EXACTFIT));
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();
}
CLogWindow::~CLogWindow()
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, this);
}
m_LogTimer->Stop();
delete m_LogTimer;
}
void CLogWindow::OnClose(wxCloseEvent& event)
{
SaveSettings();
event.Skip();
}
void CLogWindow::SaveSettings()
{
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));
}
void CLogWindow::OnClear(wxCommandEvent& WXUNUSED (event))
{
m_Log->Clear();
{
std::lock_guard<std::mutex> lk(m_LogSection);
while (!msgQueue.empty())
msgQueue.pop();
}
m_LogManager->GetConsoleListener()->ClearScreen();
}
void CLogWindow::UnPopulateBottom()
{
sBottom->Detach(m_Log);
sBottom->Detach(m_cmdline);
}
void CLogWindow::PopulateBottom()
{
sBottom->Add(m_Log, 1, wxEXPAND | wxSHRINK);
sBottom->Add(m_cmdline, 0, wxEXPAND);
Layout();
}
wxTextCtrl* CLogWindow::CreateTextCtrl(wxPanel* parent, wxWindowID id, long Style)
{
wxTextCtrl* TC = new wxTextCtrl(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, Style);
#ifdef __APPLE__
TC->SetBackgroundColour(*wxLIGHT_GREY);
#else
TC->SetBackgroundColour(*wxBLACK);
#endif
if (m_FontChoice && m_FontChoice->GetSelection() < (int)LogFont.size() && m_FontChoice->GetSelection() >= 0)
TC->SetDefaultStyle(wxTextAttr(wxNullColour, wxNullColour, LogFont[m_FontChoice->GetSelection()]));
return TC;
}
void CLogWindow::OnFontChange(wxCommandEvent& event)
{
// 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()]));
SaveSettings();
}
void CLogWindow::OnWrapLineCheck(wxCommandEvent& event)
{
#ifdef __WXGTK__
// Clear the old word wrap state and set the new
m_Log->SetWindowStyleFlag(m_Log->GetWindowStyleFlag() ^ (wxTE_WORDWRAP | wxTE_DONTWRAP));
#else
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, IDM_LOG, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_WORDWRAP);
else
m_Log = CreateTextCtrl(this, IDM_LOG, wxTE_RICH | wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP);
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
m_Log->AppendText(Text);
PopulateBottom();
m_LogAccess = true;
#endif
SaveSettings();
}
void CLogWindow::OnLogTimer(wxTimerEvent& WXUNUSED(event))
{
if (!m_LogAccess || m_ignoreLogTimer)
return;
UpdateLog();
// Scroll to the last line
if (!msgQueue.empty())
{
m_Log->ScrollLines(1);
m_Log->ShowPosition( m_Log->GetLastPosition() );
}
}
void CLogWindow::UpdateLog()
{
if (!m_LogAccess || !m_Log)
return;
// 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;
std::lock_guard<std::mutex> lk(m_LogSection);
while (!msgQueue.empty())
{
switch (msgQueue.front().first)
{
case ERROR_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxRED));
break;
case WARNING_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxYELLOW));
break;
case NOTICE_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxGREEN));
break;
case INFO_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxCYAN));
break;
case DEBUG_LEVEL:
m_Log->SetDefaultStyle(wxTextAttr(*wxLIGHT_GREY));
break;
default:
m_Log->SetDefaultStyle(wxTextAttr(*wxWHITE));
break;
}
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;
}
void CLogWindow::Log(LogTypes::LOG_LEVELS level, const char *text)
{
std::lock_guard<std::mutex> lk(m_LogSection);
if (msgQueue.size() >= 100)
msgQueue.pop();
msgQueue.push(std::make_pair(u8(level), StrToWxStr(text)));
}
|