-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlogger.hpp
105 lines (93 loc) · 2.16 KB
/
logger.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
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
// LGPL 3 or higher Robert Burner Schadek rburners@gmail.com
#pragma once
#include <iostream>
#include <unordered_set>
#include "format.hpp"
namespace sweet {
#ifndef SWEET_NO_LOGGER
inline std::unordered_set<size_t>& getAvaiLogger() {
static std::unordered_set<size_t> logger;
return logger;
}
inline bool enableLogger(const size_t id) {
return getAvaiLogger().insert(id).second;
}
inline bool disableLogger(const size_t id) {
return static_cast<bool>(getAvaiLogger().erase(id));
}
inline std::string shortenString(const std::string& str) {
size_t idx = str.rfind('/');
if(idx == std::string::npos) {
return str;
} else {
return str.substr(idx+1);
}
}
struct Log {
private:
std::string fn;
int line;
bool warn;
public:
explicit Log(const char* f, int l, bool w = false) {
fn = shortenString(f);
line = l;
warn = w;
}
void operator()() {
format(std::cerr, "%s:%d ", fn, line);
std::cerr<<std::endl;
}
void operator()(const size_t ll) {
if(getAvaiLogger().count(ll)) {
format(std::cerr, "%s:%d ", fn, line);
std::cerr<<std::endl;
}
}
template<typename... Args>
void operator()(const std::string& form, Args... args) {
if(warn) {
std::cerr<<"WARN ";
}
format(std::cerr, "%s:%d ", fn, line);
format(std::cerr, form, args...);
std::cerr<<std::endl;
}
template<typename... Args>
void operator()(const size_t ll, const std::string& form, Args... args) {
if(getAvaiLogger().count(ll)) {
if(warn) {
std::cerr<<"WARN ";
}
format(std::cerr, "%s:%d ", fn, line);
format(std::cerr, form, args...);
std::cerr<<std::endl;
}
}
};
#define LOG sweet::Log(__FILE__,__LINE__)
#define WARN sweet::Log(__FILE__,__LINE__,true)
#else
struct Log {
explicit Log(const char*, int, bool = false) { }
void operator()() {
}
void operator()(const size_t) {
}
template<typename... Args>
void operator()(std::string, Args...) {
}
template<typename... Args>
void operator()(size_t b, std::string, Args... ) {
}
};
inline bool enableLogger(const size_t) {
return false;
}
inline bool disableLogger(const size_t) {
return false;
}
#define LOG sweet::Log(__FILE__, __LINE__)
#define WARN sweet::Log(__FILE__, __LINE__,true)
#endif
}