-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
193 lines (187 loc) · 4.81 KB
/
logger.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
#include "logger.h"
#include <iostream>
using namespace SomeLogger;
#ifdef _WIN32
#else
std::map<Color, std::string> colorToFgColor {
{ Color::Black, "30" },
{ Color::Grey, "1;30" },
{ Color::LightGrey, "1;37" },
{ Color::White, "37" },
{ Color::Blue, "34"} ,
{ Color::Green, "32" },
{ Color::Cyan, "36" },
{ Color::Red, "31" },
{ Color::Purple, "35" },
{ Color::LightBlue, "1;34" },
{ Color::LightGreen, "1;32" },
{ Color::LightCyan, "1;36" },
{ Color::LightRed, "1;31" },
{ Color::LightPurple, "1;35" },
{ Color::Orange, "1;33" },
{ Color::Yellow, "33" }
};
std::map<Color, std::string> colorToBgColor {
{ Color::Black, "40" },
{ Color::Grey, "1;40" },
{ Color::LightGrey, "1;47" },
{ Color::White, "47" },
{ Color::Blue, "44"} ,
{ Color::Green, "42" },
{ Color::Cyan, "46" },
{ Color::Red, "41" },
{ Color::Purple, "45" },
{ Color::LightBlue, "1;44" },
{ Color::LightGreen, "1;42" },
{ Color::LightCyan, "1;46" },
{ Color::LightRed, "1;41" },
{ Color::LightPurple, "1;45" },
{ Color::Orange, "1;43" },
{ Color::Yellow, "43" }
};
#endif
Logger::Logger() {
#ifdef _WIN32
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#else
#endif
}
char* Logger::convert(unsigned int num, int base) {
static char Representation[]= "0123456789ABCDEF";
static char buffer[50];
char *ptr;
ptr = &buffer[49];
*ptr = '\0';
do {
*--ptr = Representation[num % base];
num /= base;
} while(num != 0);
return(ptr);
}
void Logger::out(const String& msg) {
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, (static_cast<int>(fgColor) + (static_cast<int>(bgColor)<<1)));
std::cout <<levelLabel[level] << msg.asStr() << (isEndl ? "\n" : "");
SetConsoleTextAttribute(hConsole, 0x0f);
#else
//FILE *f = fopen("/home/daniil/Documents/unknowwm/log", "a");
//fprintf(f, msg.asStr().c_str());
//fclose(f);
std::cout << std::string("\033[") + colorToFgColor[fgColor] + std::string(";") + colorToBgColor[bgColor] +
std::string("m") + levelLabel[level] + msg.asStr() + std::string("\033[0m\n") + (isEndl ? "\n" : "");
#endif
}
void Logger::setLabelForLevel(LoggerLevel level, std::string label) {
levelLabel[level] = label;
}
Logger& Logger::operator<<(const std::string& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const int& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const long& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const long long& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const unsigned& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const unsigned long& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const unsigned long long& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const float& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const double& msg) {
out(String(msg));
return *this;
}
Logger& Logger::operator<<(const long double& msg) {
out(String(msg));
return *this;
}
Logger& Logger::log(LoggerLevel level, Color fgColor, Color bgColor) {
this->fgColor = fgColor;
this->bgColor = bgColor;
this->level = level;
return *this;
}
Logger& Logger::printEndl(bool isEndl) {
this->isEndl = isEndl;
return *this;
}
void Logger::logFormat(char* format, ...) {
std::string allFormat = format;
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, (static_cast<int>(fgColor) + (static_cast<int>(bgColor)<<1)));
#else
puts((std::string("\033[") + colorToFgColor[fgColor] + std::string(";") + colorToBgColor[bgColor] + std::string("m")).c_str());
#endif
const char *traverse;
unsigned int i;
char *s;
allFormat = levelLabel[level] + allFormat;
va_list arg;
va_start(arg, allFormat.c_str());
bool needCheck = true;
for (auto& traverse : allFormat) {
if (traverse == '\0') {
break;
}
if (traverse != '%' && needCheck) {
putchar(traverse);
continue;
} else if (needCheck) {
needCheck = false;
continue;
}
needCheck = true;
switch (traverse) {
case 'c' : i = va_arg(arg, int); //Fetch char argument
putchar(i);
break;
case 'd' : i = va_arg(arg, int); //Fetch Decimal/Integer argument
if (i < 0) {
i = -i;
putchar('-');
}
puts(convert(i,10));
break;
case 'o': i = va_arg(arg, unsigned int); //Fetch Octal representation
puts(convert(i, 8));
break;
case 's': s = va_arg(arg, char *); //Fetch string
puts(s);
break;
case 'x': i = va_arg(arg, unsigned int); //Fetch Hexadecimal representation
puts(convert(i, 16));
break;
case 'b': i = va_arg(arg,unsigned int); //Fetch binary representation
puts(convert(i, 2));
break;
}
}
if (isEndl) {
putchar('\n');
}
va_end(arg);
#ifdef _WIN32
SetConsoleTextAttribute(hConsole, 0x0f);
#else
puts("\033[0m\n");
#endif
}