-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLogging.hpp
61 lines (50 loc) · 1.26 KB
/
Logging.hpp
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
#import <iostream>
namespace ssvim {
typedef enum LogLevel {
LogLevelError = 1,
LogLevelInfo,
LogLevelExtreme
} LogLevel;
class Logger : public std::enable_shared_from_this<Logger> {
LogLevel const _level;
std::string _messagePrefix;
public:
Logger(LogLevel level, std::string channel = "SS")
: _level(level), _messagePrefix("__" + channel + ": ") {
}
template <class... Args> Logger log(LogLevel level, Args const &... args) {
if (level <= _level) {
return logArgs(level, args...);
}
return *this;
}
// By default this logs debugging messages
template <class Args> Logger &operator<<(Args args) {
log(LogLevelInfo, args);
return *this;
}
auto level() {
return _level;
};
private:
Logger &logArgs(LogLevel level) {
return *this;
}
template <class Arg, class... Args>
Logger &logArgs(LogLevel level, Arg const &arg, Args const &... args) {
if (level == LogLevelError) {
writeLock();
std::cerr << _messagePrefix << arg << std::endl;
writeUnLock();
} else {
writeLock();
std::cout << _messagePrefix << arg << std::endl;
writeUnLock();
}
logArgs(level, args...);
return *this;
}
void writeLock();
void writeUnLock();
};
} // namespace ssvim