-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.cpp
76 lines (68 loc) · 1.92 KB
/
logging.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
#include "logging.hpp"
#include "termcolor.hpp"
std::string logging::vm_size() {
std::ifstream info("/proc/self/status");
std::string line;
while (std::getline(info, line)) {
if (line.find("VmSize") != std::string::npos){
break;
}
}
info.close();
std::vector<std::string> tokens;
boost::split(tokens, line, boost::is_space());
size_t ntok = tokens.size();
if (ntok < 2) {
return "xxx Kb";
}
try {
int num_kb = stoi(tokens[ntok - 2]);
std::stringstream sstr;
if (num_kb > 1048576) {
sstr << std::setprecision(3) << (num_kb / 1048576.0) << " Gb";
} else if (num_kb > 1024) {
sstr << std::setprecision(3) << (num_kb / 1024.0) << " Mb";
} else {
sstr << num_kb << " Kb";
}
return sstr.str();
} catch (const std::invalid_argument& e) {
return "xxx Kb";
}
}
logging::Level log_level = logging::Level::Info;
void logging::set_level(logging::Level level) {
log_level = level;
}
void logging::trace(std::string & msg) {
if (log_level <= logging::Level::Trace){
std::cerr << termcolor::grey << "- " << termcolor::reset
<< msg << std::endl;
}
}
void logging::debug(std::string & msg) {
if (log_level <= logging::Level::Debug){
std::cerr << termcolor::cyan << "= " << termcolor::reset
<< msg << std::endl;
}
}
void logging::info(std::string & msg) {
if (log_level <= logging::Level::Info){
std::cerr << termcolor::green << "* " << termcolor::reset
/* << "[" << vm_size() << "] " */
<< msg
<< std::endl;
}
}
void logging::warn(std::string & msg) {
if (log_level <= logging::Level::Warn){
std::cerr << termcolor::yellow << "! " << termcolor::reset
<< msg << std::endl;
}
}
void logging::error(std::string & msg) {
if (log_level <= logging::Level::Error){
std::cerr << termcolor::red << "X " << termcolor::reset
<< msg << std::endl;
}
}