-
Notifications
You must be signed in to change notification settings - Fork 5
/
tsp_log.cpp
60 lines (49 loc) · 1.34 KB
/
tsp_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
#include <iostream>
#include <ctime>
#include <sstream>
#include "tsp_log.h"
const std::string TspLogger::endl = "\n";
LogLevel currentLogLv = info;
TspLogger l[LogLevelSize];
TspLogger& TspLogger::operator<<(const std::string &msg) {
time_t rawtime;
struct tm * timeinfo;
char dt_buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(dt_buffer,sizeof(dt_buffer),"%d-%m-%Y %I:%M:%S",timeinfo);
if (this->level >= currentLogLv) {
if (printStatus) {
std::cout << "[" << dt_buffer << "]" << "[" << LogLevelString[this->level] << "] ";
printStatus = false;
} else if (msg == endl) {
printStatus = true;
}
std::cout << msg;
}
return *this;
}
template <typename T>
std::string to_string(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
TspLogger& TspLogger::operator<<(const int &msg) {
return this->operator<<(to_string<int>(msg));
}
TspLogger& TspLogger::operator<<(const double &msg) {
return this->operator<<(to_string<double>(msg));
}
TspLogger& TspLogger::operator<<(const float &msg) {
return this->operator<<(to_string<float>(msg));
}
TspLogger& tsplog(LogLevel n) {
TspLogger& logger = l[n];
logger.level = n;
return logger;
}
void setLogLevel(LogLevel l) {
currentLogLv = l;
}