forked from manzali/lseb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.hpp
More file actions
183 lines (162 loc) · 4.32 KB
/
Copy pathlog.hpp
File metadata and controls
183 lines (162 loc) · 4.32 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
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
#ifndef COMMON_LOG_HPP
#define COMMON_LOG_HPP
#include <iostream>
#include <sstream>
#include <string>
#include <mutex>
#include <ctime>
#include <cassert>
#include <syslog.h>
#define LOG(level) \
if (lseb::Log::level > lseb::Log::BaseLevel()) ; \
else lseb::Log(lseb::Log::level).tmp_stream()
namespace lseb {
namespace detail {
int const BUFSIZE = 200;
inline std::string Now() {
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
char tmdescr[BUFSIZE] = { 0 };
const char fmt[] = "%F - %T";
std::strftime(tmdescr, sizeof(tmdescr) - 1, fmt, std::localtime(&in_time_t));
return std::string(tmdescr);
}
inline void log_to_syslog(
std::istream& input_stream,
std::mutex& mx,
int level,
std::string const& level_str) {
std::unique_lock<std::mutex> l(mx);
bool continuation = false;
for (std::string s; getline(input_stream, s);) {
std::ostringstream os;
os << level_str << (continuation ? "+ " : ": ") << s;
syslog(level, "%s", os.str().c_str());
continuation = true;
}
}
inline void log_to_stream(
std::ostream& output_stream,
std::istream& input_stream,
std::mutex& mx,
std::string const& ident,
std::string const& level) {
std::ostringstream os;
std::string const now = Now();
bool continuation = false;
for (std::string s; getline(input_stream, s);) {
if (continuation)
os << '\n';
os << now << ' ' << ident << ": ";
os << level << (continuation ? "+ " : ": ") << s;
continuation = true;
}
std::unique_lock<std::mutex> l(mx);
output_stream << os.str().c_str() << std::endl;
}
}
class Log {
public:
enum Level {
ERROR, WARNING, NOTICE, INFO, DEBUG
};
public:
Log(Level level = NOTICE)
:
m_level(level) {
}
~Log() {
StaticMembers& sm(static_members());
assert(!sm.ident.empty() && "Did you forget to call Log::Init()?");
if (sm.use_syslog) {
detail::log_to_syslog(
m_tmp_stream,
sm.mx,
ToSyslog(m_level),
ToString(m_level));
} else {
detail::log_to_stream(
*sm.log_stream,
m_tmp_stream,
sm.mx,
sm.ident,
ToString(m_level));
}
}
Log(const Log&) = delete;
void operator=(const Log&) = delete;
std::ostream& tmp_stream() {
return m_tmp_stream;
}
;
private:
Level m_level;
std::stringstream m_tmp_stream;
struct StaticMembers {
std::string ident;
bool use_syslog;
std::mutex mx;
std::ostream* log_stream;
Level base_level;
StaticMembers()
:
use_syslog(false),
log_stream(&std::clog),
base_level(INFO) {
}
StaticMembers(const StaticMembers&) = delete;
void operator=(const StaticMembers&) = delete;
};
static StaticMembers& static_members() {
static StaticMembers sm;
return sm;
}
public:
static void init(
std::string const& ident,
Level base_level = INFO,
std::ostream& log_stream = std::clog) {
assert(!ident.empty() && "The ident string must be non empty");
assert(
base_level >= ERROR && base_level <= DEBUG && "Invalid logging level");
assert(log_stream.good() && "The log stream is not in a good state");
StaticMembers& sm(static_members());
sm.ident = ident;
sm.base_level = base_level;
sm.log_stream = &log_stream;
}
static Level BaseLevel() {
return static_members().base_level;
}
static void UseSyslog(int option = 0, int facility = LOG_USER) {
StaticMembers& sm(static_members());
sm.use_syslog = true;
openlog(sm.ident.c_str(), option, facility);
}
static int ToSyslog(Level level) {
static int const syslog_level[] = {
LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG };
assert(level >= ERROR && level <= DEBUG);
return syslog_level[level];
}
static std::string ToString(Level level) {
static const char* const buffer[] = { "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG" };
assert(level >= ERROR && level <= DEBUG);
return buffer[level];
}
static Level FromString(const std::string& level) {
if (level == "DEBUG")
return DEBUG;
if (level == "INFO")
return INFO;
if (level == "NOTICE")
return NOTICE;
if (level == "WARNING")
return WARNING;
if (level == "ERROR")
return ERROR;
assert(!"Unknown logging level");
}
};
}
#endif