-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogger.cpp
More file actions
61 lines (47 loc) · 1.23 KB
/
Logger.cpp
File metadata and controls
61 lines (47 loc) · 1.23 KB
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
#include "Logger.hpp"
#include <mutex>
#include <sstream>
#include <Windows.h>
//////////////////////////////////////////////////////////////////////////
bool Logger::CONSOLE = false;
Logger::OutputLevel Logger::LEVEL = Logger::OL_ERROR;
static std::mutex gs_loggerMutex;
const static char *gs_format = "[%d]------------------------\n%s\n\n";
std::string Format(const char *msg) {
std::ostringstream ss;
ss << '[' << GetCurrentThreadId() << ']'
<< "------------------------\n"
<< msg << "\n\n";
return ss.str();
}
void Logger::Log(const char *msg, OutputLevel level) {
if (level == OL_INFO) {
LogInfo(msg);
}
else {
LogError(msg);
}
}
void Logger::LogInfo(const char *msg) {
if (LEVEL != OL_INFO) {
return;
}
gs_loggerMutex.lock();
if (CONSOLE) {
printf(gs_format, GetCurrentThreadId(), msg);
}
else {
OutputDebugStringA(Format(msg).c_str());
}
gs_loggerMutex.unlock();
}
void Logger::LogError(const char *msg) {
gs_loggerMutex.lock();
if (CONSOLE) {
fprintf(stderr, gs_format, GetCurrentThreadId(), msg);
}
else {
OutputDebugStringA(Format(msg).c_str());
}
gs_loggerMutex.unlock();
}