-
Notifications
You must be signed in to change notification settings - Fork 10
/
log.cpp
73 lines (61 loc) · 1.47 KB
/
log.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
#include "log.h"
#include <stdio.h>
#include <sys/time.h>
inline std::string NowTime();
TLogLevel Log::logLevel = ERROR;
TLogLevel& operator++(TLogLevel& level)
{
switch(level) {
case ERROR : return level = WARNING;
case WARNING : return level = INFO;
case INFO : return level = DEBUG;
case DEBUG : return level = DEBUG1;
case DEBUG1 : return level = DEBUG2;
case DEBUG2 : return level = DEBUG3;
case DEBUG3 : return level = DEBUG4;
case DEBUG4 : return level;
}
throw "Invalid LogLevel-Value";
}
Log::Log()
{
}
std::ostringstream& Log::Get(TLogLevel level)
{
os << "- " << NowTime();
os << " " << ToString(level) << ": ";
os << '\t';
return os;
}
Log::~Log()
{
os << std::endl;
fprintf(stderr, "%s", os.str().c_str());
fflush(stderr);
}
TLogLevel Log::IncrementReportingLevel()
{
return ++Log::logLevel;
}
TLogLevel Log::ReportingLevel()
{
return Log::logLevel;
}
std::string Log::ToString(TLogLevel level)
{
static const char* const buffer[] = {"ERROR", "WARNING", "INFO", "DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4"};
return buffer[level];
}
inline std::string NowTime()
{
char buffer[11];
time_t t;
time(&t);
tm r = {};
strftime(buffer, sizeof(buffer), "%X", localtime_r(&t, &r));
struct timeval tv;
gettimeofday(&tv, 0);
char result[100] = {0};
std::sprintf(result, "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
return result;
}