-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cpp
103 lines (92 loc) · 3.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
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
#include "log.h"
namespace myLog {
Log loli;
Log::Log()
{
}
Log::Log(const int level, const std::string &logfile, const int type) : minlevel_(level)
{
assert((this -> ERR == level || this -> INFO == level || this -> DEBUG == level) && "Logfile create failed, please check the level(Log::ERR or Log::INFO.");
if (type == this -> ADD) {
this -> of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::app);
} else if (type == this -> OVER) {
this -> of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::trunc);
} else {
assert(0 && "Logfile create failed, please check the type(Log::OVER or Log::ADD).");
}
assert(this -> of_.is_open() && "Logfile create failed, please check the logfile's name and path.");
}
Log::~Log()
{
if (this -> of_.is_open()) {
this -> of_.close();
}
}
void Log::Init(const int level, const std::string &logfile, const int type)
{
minlevel_ = level;
assert((this -> ERR == level || this -> INFO == level || this -> DEBUG == level) && "Logfile create failed, please check the level(Log::ERR or Log::INFO or LOG::DEBUG.");
if (type == this -> ADD) {
this -> of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::app);
} else if (type == this -> OVER) {
this -> of_.open(logfile.c_str(), std::ios_base::out | std::ios_base::trunc);
} else {
assert(0 && "Logfile create failed, please check the type(Log::OVER or Log::ADD).");
}
assert(this -> of_.is_open() && "Logfile create failed, please check the logfile's name and path.");
}
void Log::logWrite(const std::string &codefile, const int codeline, const int level, const char* format, ...)
{
char message[MESSAGELEN];
va_list args;
memset(message, 0, sizeof(message));
va_start(args, format);
// printf("%c\n", va_arg(args, char*));
vsprintf(message, format, args);
va_end(args);
std::string res(message);
if(sizeof(message) <= res.size()){
assert("parameter too long");
}
assert(this -> of_.is_open() && "Logfile write failed.");
if(level == this -> ERR) {
this -> of_ << "[ERROR] ";
} else if (level == this -> INFO) {
if (this -> INFO >= this -> minlevel_) {
this -> of_ << "[INFO] ";
} else {
return ;
}
} else if(level == this -> DEBUG) {
if(this -> DEBUG >= this -> minlevel_) {
this -> of_ << "[DEBUG]";
} else {
return ;
}
} else {
assert(0 && "Log write failed, please check the level(Log::ERR or Log::INFO.");
}
time_t sectime = time(NULL);
tm tmtime;
#ifdef _WIN32
#if _MSC_VER < 1600
tmtime = *localtime(§ime);
#else
localtime_s(&tmtime, §ime);
#endif
#else
localtime_r(§ime, &tmtime);
#endif
this -> of_ << tmtime.tm_year + 1900
<< '-' << tmtime.tm_mon + 1
<< '-' << tmtime.tm_mday
<< ' ' << tmtime.tm_hour
<< ':' << tmtime.tm_min
<< ':' << tmtime.tm_sec
<< ' ' << codefile
<< '(' << codeline << ") "
<< message;
return;
}
}
// myLog::Log loli(myLog::Log::INFO, "log.txt", myLog::Log::OVER);