Repository navigation
Expand file tree
/
Copy pathLogger.cpp
More file actions
82 lines (70 loc) · 1.59 KB
/
Copy pathLogger.cpp
File metadata and controls
82 lines (70 loc) · 1.59 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
#include "Logger.hpp"
#include <chrono>
#include <ctime>
bool Log::PrintDebug = false;
bool Log::NoLog = false;
const std::string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
return buf;
}
Log::Log(const char *file, int line) {
file_ = file;
line_ = line;
mode_ = kInfo;
PrintHeader();
}
Log::Log(const char *file, int line, Mode mode) {
file_ = file;
line_ = line;
mode_ = mode;
PrintHeader();
}
Log::~Log() {
if (!CanPrint()) return;
std::cout << std::endl;
}
bool Log::CanPrint() {
if (NoLog) return false;
if (mode_ == kDebug && !PrintDebug) return false;
return true;
}
void Log::PrintHeader() {
if (!CanPrint()) return;
std::cout << currentDateTime();
switch (mode_) {
case kDebug:
std::cout << " \033[1;34mDEBUG\033[0m " ;
break;
case kInfo:
std::cout << " \033[1;36mINFO\033[0m ";
break;
case kError:
std::cout << " \033[1;31mERROR\033[0m ";
break;
case kWarning:
std::cout << " \033[1;33mWARNING\033[0m ";
break;
}
if (PrintDebug) {
std::cout << file_ << ":" << line_ << " ";
}
}
Log &Log::operator<<(int t) {
if (!CanPrint()) return *this;
std::cout << t;
return *this;
}
Log &Log::operator<<(const char *t) {
if (!CanPrint()) return *this;
std::cout << t;
return *this;
}
Log &Log::operator<<(const std::string &t) {
if (!CanPrint()) return *this;
std::cout << t;
return *this;
}