-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlowLog.h
193 lines (163 loc) · 5.37 KB
/
FlowLog.h
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once
#ifdef LOG_BOOST_DLL
#include <boost/dll.hpp>
#endif
#include <chrono>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
#include <mutex>
#include <fstream>
#include <memory>
#ifdef ERROR
#undef ERROR
#endif
namespace logging {
enum severity {
TRACE, DEBUG, INFO, WARNING, ERROR, FATAL
};
class config {
public:
void setLevel(logging::severity level) {
this->level = level;
}
void setLogFile(const std::string &logFile) {
_logFile = std::make_shared<std::ofstream>(logFile.c_str());
}
logging::severity getLevel() const {
return level;
}
std::shared_ptr<std::ofstream> getLogFile() {
return _logFile;
}
static std::shared_ptr<logging::config> getInstance() {
if (!_instance)
_instance = std::make_shared<logging::config>();
return _instance;
}
config(const config &) = delete;
constexpr config() :
level(INFO), _logFile(nullptr) {};
~config() {
// delete _instance;
if (_logFile != nullptr) {
_logFile->close();
// delete _logFile;
}
}
private:
static std::shared_ptr<config> _instance;
logging::severity level;
std::shared_ptr<std::ofstream> _logFile;
};
inline std::shared_ptr<logging::config> logging::config::_instance;
static std::shared_ptr<logging::config> getConfig() {
return config::getInstance();
};
static std::mutex loggingMutex;
struct LOGGER {
std::stringstream buffer;
severity level;
severity currentLogLevel;
std::shared_ptr<std::ofstream> logFile;
~LOGGER() {
std::lock_guard<std::mutex> lock(loggingMutex);
if (currentLogLevel <= level) {
#ifdef _WIN32
if (level == severity::ERROR) {
std::cerr << buffer.str() << "\r\n";
} else {
std::cout << buffer.str() << "\r\n";
}
#else
if (level == severity::ERROR) {
std::cerr << buffer.str() << '\n';
} else {
std::cout << buffer.str() << '\n';
}
#endif
// std::cout << buffer.str() << std::endl;
if (logFile != nullptr && logFile->is_open()) {
*logFile << buffer.str() << std::endl << std::flush;
}
};
// delete buffer;
}
LOGGER &operator<<(std::ostream &(*txt)(std::ostream &)) {
// std::cout << "ostream Function: " << txt << " - " << &logging::level << " - " << logging::level << " test with " << level << endl;
if (currentLogLevel <= level) buffer << txt;
return *this;
}
template<class T>
LOGGER &operator<<(const T &txt) {
if (currentLogLevel <= level) buffer << txt;
return *this;
}
};
static std::unique_ptr<LOGGER> logger(severity level, std::shared_ptr<logging::config> config) {
std::unique_ptr<LOGGER> logInst = std::make_unique<LOGGER>();
logInst->level = level;
logInst->currentLogLevel = config->getLevel();
logInst->logFile = config->getLogFile();
return logInst;
};
static std::unique_ptr<LOGGER> log(logging::severity severity) {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::unique_ptr<LOGGER> logInst = logger(severity, logging::getConfig());
*logInst << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
switch (severity) {
case TRACE: {
*logInst << " [trace] - ";
break;
}
case DEBUG: {
*logInst << " [debug] - ";
break;
}
case INFO: {
*logInst << " [info] - ";
break;
}
case WARNING: {
*logInst << " [warning] - ";
break;
}
case ERROR: {
*logInst << " [error] - ";
break;
}
case FATAL: {
*logInst << " [fatal] - ";
break;
}
}
return logInst;
}
static inline void setLogLevel(logging::severity level) {
logging::getConfig()->setLevel(level);
};
static inline void setLogFile(const std::string &logFile) {
logging::getConfig()->setLogFile(logFile);
};
} // namespace logging
// ===== log macros =====
#define LOG_TRACE *logging::log(logging::TRACE)
#define LOG_DEBUG *logging::log(logging::DEBUG)
#define LOG_INFO *logging::log(logging::INFO)
#define LOG_WARNING *logging::log(logging::WARNING)
#define LOG_ERROR *logging::log(logging::ERROR)
#define LOG_FATAL *logging::log(logging::FATAL)
//#ifdef LOG_BOOST_DLL
//BOOST_DLL_ALIAS(logging::setLogLevel, // <-- this function is exported with...
// setLogLevel // <-- ...this alias name
//)
//
//BOOST_DLL_ALIAS(logging::setLogFile, // <-- this function is exported with...
// setLogFile // <-- ...this alias name
//)
//#endif