forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogManager.cpp
282 lines (245 loc) · 7.91 KB
/
LogManager.cpp
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
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <algorithm>
#include "LogManager.h"
#include "ConsoleListener.h"
#include "Timer.h"
#include "Thread.h"
#include "FileUtil.h"
#ifdef __SYMBIAN32__
#include <e32debug.h>
#endif
// Don't need to savestate this.
const char *hleCurrentThreadName = NULL;
// Unfortunately this is quite slow.
#define LOG_MSC_OUTPUTDEBUG false
// #define LOG_MSC_OUTPUTDEBUG true
void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
const char *file, int line, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
if (LogManager::GetInstance())
LogManager::GetInstance()->Log(level, type,
file, line, fmt, args);
va_end(args);
}
LogManager *LogManager::m_logManager = NULL;
LogManager::LogManager()
{
// create log files
m_Log[LogTypes::MASTER_LOG] = new LogContainer("*", "Master Log");
m_Log[LogTypes::BOOT] = new LogContainer("BOOT", "Boot");
m_Log[LogTypes::COMMON] = new LogContainer("COMMON", "Common");
m_Log[LogTypes::CPU] = new LogContainer("CPU", "CPU");
m_Log[LogTypes::LOADER] = new LogContainer("LOAD", "Loader");
m_Log[LogTypes::IO] = new LogContainer("IO", "IO");
m_Log[LogTypes::DISCIO] = new LogContainer("DIO", "DiscIO");
m_Log[LogTypes::PAD] = new LogContainer("PAD", "Pad");
m_Log[LogTypes::FILESYS] = new LogContainer("FileSys", "File System");
m_Log[LogTypes::G3D] = new LogContainer("G3D", "3D Graphics");
m_Log[LogTypes::DMA] = new LogContainer("DMA", "DMA");
m_Log[LogTypes::INTC] = new LogContainer("INTC", "Interrupts");
m_Log[LogTypes::MEMMAP] = new LogContainer("MM", "Memory Map");
m_Log[LogTypes::SOUND] = new LogContainer("SND", "Sound");
m_Log[LogTypes::SAS] = new LogContainer("SAS", "Sound Mixer (Sas)");
m_Log[LogTypes::HLE] = new LogContainer("HLE", "HLE");
m_Log[LogTypes::TIMER] = new LogContainer("TMR", "Timer");
m_Log[LogTypes::VIDEO] = new LogContainer("VID", "Video");
m_Log[LogTypes::DYNA_REC] = new LogContainer("Jit", "JIT compiler");
m_Log[LogTypes::NETPLAY] = new LogContainer("NET", "Net play");
m_Log[LogTypes::ME] = new LogContainer("ME", "Media Engine");
// Remove file logging on small devices
#if !defined(USING_GLES2) || defined(_DEBUG)
m_fileLog = new FileLogListener(File::GetUserPath(F_MAINLOG_IDX).c_str());
m_consoleLog = new ConsoleListener();
m_debuggerLog = new DebuggerLogListener();
#else
m_fileLog = NULL;
m_consoleLog = NULL;
m_debuggerLog = NULL;
#endif
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
m_Log[i]->SetEnable(true);
#if !defined(USING_GLES2) || defined(_DEBUG)
m_Log[i]->AddListener(m_fileLog);
m_Log[i]->AddListener(m_consoleLog);
#ifdef _MSC_VER
if (IsDebuggerPresent() && m_debuggerLog != NULL && LOG_MSC_OUTPUTDEBUG)
m_Log[i]->AddListener(m_debuggerLog);
#endif
#endif
}
}
LogManager::~LogManager()
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
#if !defined(USING_GLES2) || defined(_DEBUG)
if (m_fileLog != NULL)
m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_fileLog);
m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_consoleLog);
#ifdef _MSC_VER
m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_debuggerLog);
#endif
#endif
}
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
delete m_Log[i];
if (m_fileLog != NULL)
delete m_fileLog;
#if !defined(USING_GLES2) || defined(_DEBUG)
delete m_consoleLog;
delete m_debuggerLog;
#endif
}
void LogManager::ChangeFileLog(const char *filename)
{
if (m_fileLog != NULL)
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
m_logManager->RemoveListener((LogTypes::LOG_TYPE)i, m_fileLog);
delete m_fileLog;
}
if (filename != NULL)
{
m_fileLog = new FileLogListener(filename);
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
m_Log[i]->AddListener(m_fileLog);
}
}
void LogManager::SaveConfig(IniFile::Section *section)
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; i++)
{
section->Set((std::string(m_Log[i]->GetShortName()) + "Enabled").c_str(), m_Log[i]->IsEnabled());
section->Set((std::string(m_Log[i]->GetShortName()) + "Level").c_str(), (int)m_Log[i]->GetLevel());
}
}
void LogManager::LoadConfig(IniFile::Section *section)
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; i++)
{
bool enabled;
int level;
section->Get((std::string(m_Log[i]->GetShortName()) + "Enabled").c_str(), &enabled, true);
section->Get((std::string(m_Log[i]->GetShortName()) + "Level").c_str(), &level, 0);
m_Log[i]->SetEnable(enabled);
m_Log[i]->SetLevel((LogTypes::LOG_LEVELS)level);
}
}
void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char *file, int line, const char *format, va_list args)
{
std::lock_guard<std::mutex> lk(m_log_lock);
char msg[MAX_MSGLEN * 2];
LogContainer *log = m_Log[type];
if (!log || !log->IsEnabled() || level > log->GetLevel() || ! log->HasListeners())
return;
static const char level_to_char[8] = "-NEWIDV";
char formattedTime[13];
Common::Timer::GetTimeFormatted(formattedTime);
#ifdef _DEBUG
#ifdef _WIN32
static const char sep = '\\';
#else
static const char sep = '/';
#endif
const char *fileshort = strrchr(file, sep);
if (fileshort != NULL) {
do
--fileshort;
while (fileshort > file && *fileshort != sep);
if (fileshort != file)
file = fileshort + 1;
}
#endif
char *msgPos = msg;
if (hleCurrentThreadName != NULL)
{
msgPos += sprintf(msgPos, "%s %-12.12s %c[%s]: %s:%d ",
formattedTime,
hleCurrentThreadName, level_to_char[(int)level],
log->GetShortName(),
file, line);
}
else
{
msgPos += sprintf(msgPos, "%s %s:%d %c[%s]: ",
formattedTime,
file, line, level_to_char[(int)level],
log->GetShortName());
}
msgPos += vsnprintf(msgPos, MAX_MSGLEN, format, args);
// This will include the null terminator.
memcpy(msgPos, "\n", sizeof("\n"));
log->Trigger(level, msg);
}
void LogManager::Init()
{
m_logManager = new LogManager();
}
void LogManager::Shutdown()
{
delete m_logManager;
m_logManager = NULL;
}
LogContainer::LogContainer(const char* shortName, const char* fullName, bool enable)
: m_enable(enable)
{
strncpy(m_fullName, fullName, 128);
strncpy(m_shortName, shortName, 32);
m_level = LogTypes::LDEBUG;
}
// LogContainer
void LogContainer::AddListener(LogListener *listener)
{
std::lock_guard<std::mutex> lk(m_listeners_lock);
m_listeners.insert(listener);
}
void LogContainer::RemoveListener(LogListener *listener)
{
std::lock_guard<std::mutex> lk(m_listeners_lock);
m_listeners.erase(listener);
}
void LogContainer::Trigger(LogTypes::LOG_LEVELS level, const char *msg)
{
#ifdef __SYMBIAN32__
RDebug::Printf("%s",msg);
#else
std::lock_guard<std::mutex> lk(m_listeners_lock);
std::set<LogListener*>::const_iterator i;
for (i = m_listeners.begin(); i != m_listeners.end(); ++i)
{
(*i)->Log(level, msg);
}
#endif
}
FileLogListener::FileLogListener(const char *filename)
{
m_logfile.open(filename, std::ios::app);
SetEnable(true);
}
void FileLogListener::Log(LogTypes::LOG_LEVELS, const char *msg)
{
if (!IsEnabled() || !IsValid())
return;
std::lock_guard<std::mutex> lk(m_log_lock);
m_logfile << msg << std::flush;
}
void DebuggerLogListener::Log(LogTypes::LOG_LEVELS, const char *msg)
{
#if _MSC_VER
::OutputDebugStringA(msg);
#endif
}