-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.cpp
99 lines (88 loc) · 2.38 KB
/
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
#include "Logger.h"
#include "Version.h"
// for documentation on using logger, see EACglobals.h
using namespace std;
Logger::Logger() : m_logging(false), m_using_file(false),
m_out(new LoggerStream(this,clog)) {}
Logger::~Logger() {
out(LOGGER_INFO) << "Exiting logger." << endl;
delete m_out;
}
void Logger::setOutputStream(ostream& outStream) {
delete m_out;
m_out = new LoggerStream(this,outStream);
}
void Logger::setOutputFile(const string filePath) {
std::remove(filePath.c_str());
delete m_out;
m_out = new LoggerStream(this,*(new ofstream(filePath, fstream::app)));
}
void Logger::setlogging(bool state) {
if (state == m_logging) return;
if (state) {
m_logging = state;
out(LOGGER_INFO) << "Logging enabled." << endl;
out(LOGGER_INFO) << "EACirc framework (build " << GIT_COMMIT_SHORT << ")." << endl;
out(LOGGER_INFO) << "current date: " << getDate() << endl;
} else {
out(LOGGER_INFO) << "Logging disabled" << endl;
m_logging = state;
}
}
string Logger::getTime() const {
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"%H:%M:%S",timeinfo);
stringstream temp;
temp << "[" << buffer << "] ";
return temp.str();
}
string Logger::getDate() const {
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime (&rawtime);
strftime (buffer,80,"%Y-%m-%d",timeinfo);
return string(buffer);
}
// When we sync the stream with the output.
// 1) Output time then the buffer
// 2) Reset the buffer
// 3) flush the actual output stream we are using.
int Logger::LoggerStream::LoggerBuffer::sync() {
if (!m_parentLogger->getLogging()) {
str("");
return 0;
}
m_out << m_parentLogger->getTime() << str();
str("");
m_out.flush();
return 0;
}
bool Logger::getLogging() {
return m_logging;
}
ostream& Logger::out() {
return *m_out;
}
ostream& Logger::out(int prefix) {
switch (prefix) {
case LOGGER_INFO:
out() << "info: ";
break;
case LOGGER_WARNING:
out() << "warning: ";
break;
case LOGGER_ERROR:
out() << "error: ";
break;
default:
out(LOGGER_WARNING) << "Unknown logger prefix." << endl;
break;
}
return out();
}