-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.h
More file actions
38 lines (29 loc) · 847 Bytes
/
logger.h
File metadata and controls
38 lines (29 loc) · 847 Bytes
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
#include <chrono>
#include <iomanip>
#include <ctime>
#include <vector>
#include <mutex>
#include <string>
#include <ostream>
class Logger {
private:
std::stringstream messages;
std::mutex mutex;
std::ostream& out;
public:
Logger(std::ostream& o) : out(o) { };
~Logger(void) { };
bool verbose = false;
void log(std::string msg) {
std::lock_guard<std::mutex> lock(mutex);
auto now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
auto timestr = std::put_time(std::localtime(&now_c), "%T");
messages << '[' << timestr << "] " << msg << '\n';
if(verbose)
out << '[' << timestr << "] " << msg << std::endl;
};
void dump(std::ostream& o) {
o << messages.str() << std::flush;
};
};