-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.hpp
75 lines (57 loc) · 1.32 KB
/
logger.hpp
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
#pragma once
#include <string>
#include <iostream>
#include <ctime>
namespace remexec {
class Log {
private:
Log();
~Log();
static std::ostream *_log;
static std::string date(const std::string &format){
time_t rawtime;
struct tm * timeinfo;
char buffer[format.size()*2];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime(buffer, format.size()*2, format.c_str(), timeinfo);
return buffer;
}
template<typename T, typename ...Args>
static void do_log(std::ostream &s, const T &arg, const Args &...args){
s << arg;
do_log(s, args...);
}
template<typename T>
static void do_log(std::ostream &s, const T &arg){
s << arg << std::endl;
}
public:
static void init(std::ostream &l){ _log = &l; }
template<typename ...Args>
static void log(const Args &...args){
std::ostream *s = &std::cout;
if (_log){
s = _log;
}
*s << date("[%Y-%m-%d %H:%M:%S] ");
do_log(*s, args...);
}
template<typename ...Args>
static void error(const Args &...args){
log("[ERROR] ", args...);
}
template<typename ...Args>
static void warn(const Args &...args){
log("[WARNING] ", args...);
}
template<typename ...Args>
static void info(const Args &...args){
log("[INFO] ", args...);
}
template<typename ...Args>
static void debug(const Args &...args){
log("[DEBUG] ", args...);
}
};
}