forked from benoitryder/megumi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
75 lines (60 loc) · 1.22 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
74
#include <iostream>
#include <iomanip>
#include <sys/time.h>
#include <time.h>
#include <string.h>
#include "log.h"
#if defined __WIN32
#ifndef localtime_r
static struct ::tm* localtime_r(const time_t* timep, struct ::tm* tm)
{
struct ::tm* tmp = ::localtime(timep);
memset(tm, 0, sizeof(*tm));
if(tmp) {
*tm = *tmp;
}
return tmp;
}
#endif
#endif
namespace Log {
static const char* severity_names[] = {
"INFO",
"NOTICE",
"WARNING",
"ERROR",
};
static Severity minimum_severity = Severity::INFO;
void setMinimumSeverity(Severity severity)
{
minimum_severity = severity;
}
Message::Message(Severity severity):
severity_(severity)
{
struct ::timeval tnow;
::gettimeofday(&tnow, NULL);
struct ::tm tloc = {};
time_t timep = tnow.tv_sec;
localtime_r(&timep, &tloc);
stream_
<< std::setfill('0')
<< std::setw(2) << tloc.tm_hour << ':'
<< std::setw(2) << tloc.tm_min << ':'
<< std::setw(2) << tloc.tm_sec << '.'
<< std::setw(6) << tnow.tv_usec
<< ' ' << severity_names[severity]
<< " - "
;
}
Message::~Message()
{
flush();
}
void Message::flush()
{
if(severity_ >= minimum_severity) {
std::cout << stream_.str() << std::endl << std::flush;
}
}
}