-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
106 lines (82 loc) · 1.68 KB
/
Copy pathLogger.cpp
File metadata and controls
106 lines (82 loc) · 1.68 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
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
#include "Logger.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <mutex>
using namespace std;
ofstream Logger::stream;
mutex Logger::logMutex;
void Logger::init(const string& file)
{
stream.open(file, ios::app);
}
string Logger::timestamp()
{
time_t now = time(nullptr);
tm localTime = *localtime(&now);
stringstream ss;
ss << put_time(&localTime, "%Y-%m-%d %H:%M:%S");
return ss.str();
}
string Logger::levelToString(Level level)
{
switch (level)
{
case Level::INFO:
return "INFO";
case Level::WARN:
return "WARN";
case Level::ERROR:
return "ERROR";
case Level::DEBUG:
return "DEBUG";
}
return "";
}
void Logger::log(Level level, const string& message)
{
lock_guard<std::mutex> lock(logMutex);
string line =
"[" +
timestamp() +
"] [" +
levelToString(level) +
"] " +
message;
cout << line << endl;
if (stream.is_open())
{
stream << line << endl;
stream.flush();
}
}
void Logger::info(const string& message)
{
log(Level::INFO, message);
}
void Logger::warn(const string& message)
{
log(Level::WARN, message);
}
void Logger::warning(const string& message)
{
log(Level::WARN, message);
}
void Logger::error(const string& message)
{
log(Level::ERROR, message);
}
void Logger::debug(const string& message)
{
log(Level::DEBUG, message);
}
void Logger::logRaw(const string& rawText)
{
lock_guard<std::mutex> lock(logMutex);
cout << rawText << endl;
if (stream.is_open())
{
stream << rawText << endl;
stream.flush();
}
}