-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.h
More file actions
executable file
·88 lines (64 loc) · 1.8 KB
/
logger.h
File metadata and controls
executable file
·88 lines (64 loc) · 1.8 KB
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
#ifndef LOGGER_H
#define LOGGER_H
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <chrono>
#include <mutex>
#include <iomanip>
using namespace std;
enum LogType {
DEBUG,
INFO,
WARNING,
ERROR
};
class Logger {
public:
static Logger &getInstance();
template<typename T> void debug(const T &message, int rank=-1);
template<typename T> void info(const T &message, int rank=-1);
template<typename T> void warning(const T &message, int rank=-1);
template<typename T> void error(const T &message, int rank=-1);
private:
Logger();
~Logger();
template<typename T> void log(LogType level, const T &message, int rank);
static string getTimestamp();
static string getLevelString(LogType level);
ofstream file_;
mutex mutex_;
bool console_output_;
static Logger *instance_;
chrono::steady_clock::time_point start_time_;
};
template<typename T> void Logger::debug(const T &message, int rank) {
log(DEBUG, message, rank);
}
template<typename T> void Logger::info(const T &message, int rank) {
log(INFO, message, rank);
}
template<typename T> void Logger::warning(const T &message, int rank) {
log(WARNING, message, rank);
}
template<typename T> void Logger::error(const T &message, int rank) {
log(ERROR, message, rank);
}
template<typename T> void Logger::log(const LogType level, const T &message, const int rank) {
lock_guard lock(mutex_);
stringstream ss;
ss << "[" << getTimestamp() << "] "
<< "[" << getLevelString(level) << "] ";
if (rank >= 0) {
ss << "[Node " << rank << "] ";
}
ss << message;
if (console_output_) {
cout << ss.str() << '\n';
}
if (file_.is_open()) {
file_ << ss.str() << '\n';
}
}
#endif //LOGGER_H