-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs_logger.cpp
executable file
·169 lines (135 loc) · 3.62 KB
/
dfs_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
#include "dfs_logger.hpp"
DFS_Logger::DFS_Logger(std::string full_filename){
if(full_filename.empty())
fname = std::string("/tmp/dfs_logger.log");
else
fname = full_filename;
logfile = NULL;
to_console = false;
logging = this->checkLogFile();
}
DFS_Logger::~DFS_Logger(){
if(logfile != NULL)
fclose(logfile);
}
bool DFS_Logger::logExists(){
return access( fname.c_str(), F_OK | W_OK ) == 0;
}
void DFS_Logger::printToForeground(){
to_console = true;
}
bool DFS_Logger::checkLogFile() {
bool r = false;
if ( this->logExists() ) { // our log file exists
if ( logfile == NULL ) {
if ((logfile = fopen(fname.c_str(), "w")) != NULL) {
fprintf(logfile, "successfully created logger\n");
r = true;
}
}else r = true;
} else if (logfile != NULL) {
logfile = NULL;
}
logging = r;
return r;
}
void DFS_Logger::logMe(const char* msg){
if( !this->logExists() ) return;
// std::string s = std::string(this->RESET);
// s.append(std::string(msg));
// this->logMe(s);
this->logMe(std::string(msg));
}
void DFS_Logger::logMe(std::string msg) {
if( !this->logExists() ) return;
if ( msg.empty()) return;
msg.insert(0,this->RESET);
msg.append("\n"+this->RESET);
this->doOut(msg);
}
void DFS_Logger::logMeSL(const char* msg){
if( !this->logExists() ) return;
this->logMeSL(std::string(msg));
}
void DFS_Logger::logMeSL(std::string msg) {
if( !this->logExists() ) return;
if ( msg.empty()) return;
msg.insert(0,this->RESET);
this->doOut(msg);
}
void DFS_Logger::logError(const char* msg){
if( !this->logExists() ) return;
this->logError(std::string(msg));
}
void DFS_Logger::logError(std::string msg){
if( !this->logExists() ) return;
if ( msg.empty()) return;
msg.insert(0,std::string(this->RED));
msg.append(std::string(this->RESET));
msg.append("\n");
this->doOut(msg);
}
void DFS_Logger::logGood(const char* msg){
if( !this->logExists() ) return;
this->logGood(std::string(msg));
}
void DFS_Logger::logGood(std::string msg){
if( !this->logExists() ) return;
if ( msg.empty()) return;
msg.insert(0,std::string(this->GREEN));
msg.append(std::string(this->RESET));
msg.append("\n");
this->doOut(msg);
}
void DFS_Logger::logWarning(const char* msg){
if( !this->logExists() ) return;
this->logWarning(std::string(msg));
}
void DFS_Logger::logWarning(std::string msg){
if( !this->logExists() ) return;
if ( msg.empty()) return;
msg.insert(0,std::string(this->YELLOW));
msg.append(std::string(this->RESET));
msg.append("\n");
this->doOut(msg);
}
std::string DFS_Logger::getColor(std::string color){
std::string c = color;
if( c.empty() ) return this->RESET;
std::transform(c.begin(), c.end(),c.begin(), ::toupper);
if( c == "RED")
return this->RED;
else if( c == "GREEN")
return this->GREEN;
else if( c == "YELLOW")
return this->YELLOW;
else if( c == "BLUE")
return this->BLUE;
else if( c == "MAGENTA")
return this->MAGENTA;
else if( c == "CYAN")
return this->CYAN;
else
return this->RESET;
}
void DFS_Logger::logColor(const char* msg, const char* color){
this->logColor(std::string(msg), std::string(color));
}
void DFS_Logger::logColor(std::string msg, std::string color){
if( !this->logExists() || msg.empty() ) return;
msg.insert(0,this->getColor(color));
msg.append(std::string(this->RESET));
msg.append("\n");
this->doOut(msg);
}
void DFS_Logger::doOut(std::string msg){
if ( msg.empty()) return;
if ( checkLogFile() && logfile != NULL ) {
fprintf(logfile, msg.c_str() );
fflush(logfile);
return;
}
if(to_console){
printf( msg.c_str() );
}
}