-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLog.h
392 lines (359 loc) · 12.3 KB
/
Log.h
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Aleth: Ethereum C++ client, tools and libraries.
// Copyright 2014-2019 Aleth Authors.
// Licensed under the GNU General Public License, Version 3.
#pragma once
#include "CommonIO.h"
#include "FixedHash.h"
#include "Terminal.h"
#include <string>
#include <vector>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
namespace dev
{
/// Set the current thread's log name.
///
/// It appears that there is not currently any cross-platform way of setting
/// thread names either in Boost or in the C++11 runtime libraries. What is
/// more, the API for 'pthread_setname_np' is not even consistent across
/// platforms which implement it.
///
/// A proposal to add such functionality on the Boost mailing list, which
/// I assume never happened, but which I should follow-up and ask about.
/// http://boost.2283326.n4.nabble.com/Adding-an-option-to-set-the-name-of-a-boost-thread-td4638283.html
///
/// man page for 'pthread_setname_np', including this crucial snippet of
/// information ... "These functions are nonstandard GNU extensions."
/// http://man7.org/linux/man-pages/man3/pthread_setname_np.3.html
///
/// Stack Overflow "Can I set the name of a thread in pthreads / linux?"
/// which includes useful information on the minor API differences between
/// Linux, BSD and OS X.
/// http://stackoverflow.com/questions/2369738/can-i-set-the-name-of-a-thread-in-pthreads-linux/7989973#7989973
///
/// musl mailng list posting "pthread set name on MIPs" which includes the
/// information that musl doesn't currently implement 'pthread_setname_np'
/// https://marc.info/?l=musl&m=146171729013062&w=1
///
/// For better formatting it is recommended to limit thread name to max 4 characters.
void setThreadName(std::string const& _n);
/// Set the current thread's log name.
std::string getThreadName();
#define LOG BOOST_LOG
enum Verbosity
{
VerbositySilent = -1,
VerbosityError = 0,
VerbosityWarning = 1,
VerbosityInfo = 2,
VerbosityDebug = 3,
VerbosityTrace = 4,
};
// Simple cout-like stream objects for accessing common log channels.
// Thread-safe
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_errorLogger,
boost::log::sources::severity_channel_logger_mt<>,
(boost::log::keywords::severity = VerbosityError)(boost::log::keywords::channel = "error"))
#define cerror LOG(dev::g_errorLogger::get())
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_warnLogger,
boost::log::sources::severity_channel_logger_mt<>,
(boost::log::keywords::severity = VerbosityWarning)(boost::log::keywords::channel = "warn"))
#define cwarn LOG(dev::g_warnLogger::get())
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_noteLogger,
boost::log::sources::severity_channel_logger_mt<>,
(boost::log::keywords::severity = VerbosityInfo)(boost::log::keywords::channel = "info"))
#define cnote LOG(dev::g_noteLogger::get())
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_debugLogger,
boost::log::sources::severity_channel_logger_mt<>,
(boost::log::keywords::severity = VerbosityDebug)(boost::log::keywords::channel = "debug"))
#define cdebug LOG(dev::g_debugLogger::get())
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_traceLogger,
boost::log::sources::severity_channel_logger_mt<>,
(boost::log::keywords::severity = VerbosityTrace)(boost::log::keywords::channel = "trace"))
#define ctrace LOG(dev::g_traceLogger::get())
// Simple macro to log to any channel a message without creating a logger object
// e.g. clog(VerbosityInfo, "channel") << "message";
// Thread-safe
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
g_clogLogger, boost::log::sources::severity_channel_logger_mt<>)
#define clog(SEVERITY, CHANNEL) \
BOOST_LOG_STREAM_WITH_PARAMS(dev::g_clogLogger::get(), \
(boost::log::keywords::severity = SEVERITY)(boost::log::keywords::channel = CHANNEL))
struct LoggingOptions
{
int verbosity = VerbosityInfo;
strings includeChannels;
strings excludeChannels;
bool vmTrace = false;
};
// Should be called in every executable
void setupLogging(LoggingOptions const& _options);
bool isVmTraceEnabled();
// Simple non-thread-safe logger with fixed severity and channel for each message
// For better formatting it is recommended to limit channel name to max 6 characters.
using Logger = boost::log::sources::severity_channel_logger<>;
inline Logger createLogger(int _severity, std::string const& _channel)
{
return Logger(
boost::log::keywords::severity = _severity, boost::log::keywords::channel = _channel);
}
// Below overloads for both const and non-const references are needed, because without overload for
// non-const reference generic operator<<(formatting_ostream& _strm, T& _value) will be preferred by
// overload resolution.
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, bigint const& _value)
{
_strm.stream() << EthNavy << _value << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, bigint& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, u256 const& _value)
{
_strm.stream() << EthNavy << _value << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, u256& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, u160 const& _value)
{
_strm.stream() << EthNavy << _value << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, u160& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <unsigned N>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, FixedHash<N> const& _value)
{
_strm.stream() << EthTeal "#" << _value.abridged() << EthReset;
return _strm;
}
template <unsigned N>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, FixedHash<N>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, h160 const& _value)
{
_strm.stream() << EthRed "@" << _value.abridged() << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, h160& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, h256 const& _value)
{
_strm.stream() << EthCyan "#" << _value.abridged() << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, h256& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, h512 const& _value)
{
_strm.stream() << EthTeal "##" << _value.abridged() << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, h512& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, bytesConstRef _value)
{
_strm.stream() << EthYellow "%" << toHex(_value) << EthReset;
return _strm;
}
} // namespace dev
// Overloads for types of std namespace can't be defined in dev namespace, because they won't be
// found due to Argument-Dependent Lookup Placing anything into std is not allowed, but we can put
// them into boost::log
namespace boost
{
namespace log
{
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, dev::bytes const& _value)
{
_strm.stream() << EthYellow "%" << dev::toHex(_value) << EthReset;
return _strm;
}
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, dev::bytes& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <typename T>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::vector<T> const& _value)
{
_strm.stream() << EthWhite "[" EthReset;
int n = 0;
for (T const& i : _value)
{
_strm.stream() << (n++ ? EthWhite ", " EthReset : "");
_strm << i;
}
_strm.stream() << EthWhite "]" EthReset;
return _strm;
}
template <typename T>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::vector<T>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <typename T>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::set<T> const& _value)
{
_strm.stream() << EthYellow "{" EthReset;
int n = 0;
for (T const& i : _value)
{
_strm.stream() << (n++ ? EthYellow ", " EthReset : "");
_strm << i;
}
_strm.stream() << EthYellow "}" EthReset;
return _strm;
}
template <typename T>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::set<T>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <typename T>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::unordered_set<T> const& _value)
{
_strm.stream() << EthYellow "{" EthReset;
int n = 0;
for (T const& i : _value)
{
_strm.stream() << (n++ ? EthYellow ", " EthReset : "");
_strm << i;
}
_strm.stream() << EthYellow "}" EthReset;
return _strm;
}
template <typename T>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::unordered_set<T>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <typename T, typename U>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::map<T, U> const& _value)
{
_strm.stream() << EthLime "{" EthReset;
int n = 0;
for (auto const& i : _value)
{
_strm << (n++ ? EthLime ", " EthReset : "");
_strm << i.first;
_strm << (n++ ? EthLime ": " EthReset : "");
_strm << i.second;
}
_strm.stream() << EthLime "}" EthReset;
return _strm;
}
template <typename T, typename U>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::map<T, U>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <typename T, typename U>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::unordered_map<T, U> const& _value)
{
_strm << EthLime "{" EthReset;
int n = 0;
for (auto const& i : _value)
{
_strm.stream() << (n++ ? EthLime ", " EthReset : "");
_strm << i.first;
_strm.stream() << (n++ ? EthLime ": " EthReset : "");
_strm << i.second;
}
_strm << EthLime "}" EthReset;
return _strm;
}
template <typename T, typename U>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::unordered_map<T, U>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
template <typename T, typename U>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::pair<T, U> const& _value)
{
_strm.stream() << EthPurple "(" EthReset;
_strm << _value.first;
_strm.stream() << EthPurple ", " EthReset;
_strm << _value.second;
_strm.stream() << EthPurple ")" EthReset;
return _strm;
}
template <typename T, typename U>
inline boost::log::formatting_ostream& operator<<(
boost::log::formatting_ostream& _strm, std::pair<T, U>& _value)
{
auto const& constValue = _value;
_strm << constValue;
return _strm;
}
}
} // namespace boost