Ethereum  PoC-8
The C++ Implementation of Ethereum
Log.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 #include "CommonIO.h"
21 #include "FixedHash.h"
22 #include "Terminal.h"
23 #include <string>
24 #include <vector>
25 
26 #include <boost/log/attributes/scoped_attribute.hpp>
27 #include <boost/log/sources/global_logger_storage.hpp>
28 #include <boost/log/sources/record_ostream.hpp>
29 #include <boost/log/sources/severity_channel_logger.hpp>
30 
31 namespace dev
32 {
58 void setThreadName(std::string const& _n);
59 
61 std::string getThreadName();
62 
63 #define LOG BOOST_LOG
64 
66 {
73 };
74 
75 // Simple cout-like stream objects for accessing common log channels.
76 // Thread-safe
78  boost::log::sources::severity_channel_logger_mt<>,
79  (boost::log::keywords::severity = VerbosityError)(boost::log::keywords::channel = "error"))
80 #define cerror LOG(dev::g_errorLogger::get())
81 
83  boost::log::sources::severity_channel_logger_mt<>,
84  (boost::log::keywords::severity = VerbosityWarning)(boost::log::keywords::channel = "warn"))
85 #define cwarn LOG(dev::g_warnLogger::get())
86 
88  boost::log::sources::severity_channel_logger_mt<>,
89  (boost::log::keywords::severity = VerbosityInfo)(boost::log::keywords::channel = "info"))
90 #define cnote LOG(dev::g_noteLogger::get())
91 
93  boost::log::sources::severity_channel_logger_mt<>,
94  (boost::log::keywords::severity = VerbosityDebug)(boost::log::keywords::channel = "debug"))
95 #define cdebug LOG(dev::g_debugLogger::get())
96 
98  boost::log::sources::severity_channel_logger_mt<>,
99  (boost::log::keywords::severity = VerbosityTrace)(boost::log::keywords::channel = "trace"))
100 #define ctrace LOG(dev::g_traceLogger::get())
101 
102 // Simple macro to log to any channel a message without creating a logger object
103 // e.g. clog(VerbosityInfo, "channel") << "message";
104 // Thread-safe
105 BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(
106  g_clogLogger, boost::log::sources::severity_channel_logger_mt<>)
107 #define clog(SEVERITY, CHANNEL) \
108  BOOST_LOG_STREAM_WITH_PARAMS(dev::g_clogLogger::get(), \
109  (boost::log::keywords::severity = SEVERITY)(boost::log::keywords::channel = CHANNEL))
110 
111 
112 struct LoggingOptions
113 {
114  int verbosity = VerbosityInfo;
115  strings includeChannels;
116  strings excludeChannels;
117 };
118 
119 // Should be called in every executable
120 void setupLogging(LoggingOptions const& _options);
121 
122 // Simple non-thread-safe logger with fixed severity and channel for each message
123 // For better formatting it is recommended to limit channel name to max 6 characters.
124 using Logger = boost::log::sources::severity_channel_logger<>;
125 inline Logger createLogger(int _severity, std::string const& _channel)
126 {
127  return Logger(
128  boost::log::keywords::severity = _severity, boost::log::keywords::channel = _channel);
129 }
130 
131 // Adds the context string to all log messages in the scope
132 #define LOG_SCOPED_CONTEXT(context) \
133  BOOST_LOG_SCOPED_THREAD_ATTR("Context", boost::log::attributes::constant<std::string>(context));
134 
135 
136 // Below overloads for both const and non-const references are needed, because without overload for
137 // non-const reference generic operator<<(formatting_ostream& _strm, T& _value) will be preferred by
138 // overload resolution.
139 inline boost::log::formatting_ostream& operator<<(
140  boost::log::formatting_ostream& _strm, bigint const& _value)
141 {
142  _strm.stream() << EthNavy << _value << EthReset;
143  return _strm;
144 }
145 inline boost::log::formatting_ostream& operator<<(
146  boost::log::formatting_ostream& _strm, bigint& _value)
147 {
148  auto const& constValue = _value;
149  _strm << constValue;
150  return _strm;
151 }
152 
153 inline boost::log::formatting_ostream& operator<<(
154  boost::log::formatting_ostream& _strm, u256 const& _value)
155 {
156  _strm.stream() << EthNavy << _value << EthReset;
157  return _strm;
158 }
159 inline boost::log::formatting_ostream& operator<<(
160  boost::log::formatting_ostream& _strm, u256& _value)
161 {
162  auto const& constValue = _value;
163  _strm << constValue;
164  return _strm;
165 }
166 
167 inline boost::log::formatting_ostream& operator<<(
168  boost::log::formatting_ostream& _strm, u160 const& _value)
169 {
170  _strm.stream() << EthNavy << _value << EthReset;
171  return _strm;
172 }
173 inline boost::log::formatting_ostream& operator<<(
174  boost::log::formatting_ostream& _strm, u160& _value)
175 {
176  auto const& constValue = _value;
177  _strm << constValue;
178  return _strm;
179 }
180 
181 template <unsigned N>
182 inline boost::log::formatting_ostream& operator<<(
183  boost::log::formatting_ostream& _strm, FixedHash<N> const& _value)
184 {
185  _strm.stream() << EthTeal "#" << _value.abridged() << EthReset;
186  return _strm;
187 }
188 template <unsigned N>
189 inline boost::log::formatting_ostream& operator<<(
190  boost::log::formatting_ostream& _strm, FixedHash<N>& _value)
191 {
192  auto const& constValue = _value;
193  _strm << constValue;
194  return _strm;
195 }
196 
197 inline boost::log::formatting_ostream& operator<<(
198  boost::log::formatting_ostream& _strm, h160 const& _value)
199 {
200  _strm.stream() << EthRed "@" << _value.abridged() << EthReset;
201  return _strm;
202 }
203 inline boost::log::formatting_ostream& operator<<(
204  boost::log::formatting_ostream& _strm, h160& _value)
205 {
206  auto const& constValue = _value;
207  _strm << constValue;
208  return _strm;
209 }
210 
211 inline boost::log::formatting_ostream& operator<<(
212  boost::log::formatting_ostream& _strm, h256 const& _value)
213 {
214  _strm.stream() << EthCyan "#" << _value.abridged() << EthReset;
215  return _strm;
216 }
217 inline boost::log::formatting_ostream& operator<<(
218  boost::log::formatting_ostream& _strm, h256& _value)
219 {
220  auto const& constValue = _value;
221  _strm << constValue;
222  return _strm;
223 }
224 
225 inline boost::log::formatting_ostream& operator<<(
226  boost::log::formatting_ostream& _strm, h512 const& _value)
227 {
228  _strm.stream() << EthTeal "##" << _value.abridged() << EthReset;
229  return _strm;
230 }
231 inline boost::log::formatting_ostream& operator<<(
232  boost::log::formatting_ostream& _strm, h512& _value)
233 {
234  auto const& constValue = _value;
235  _strm << constValue;
236  return _strm;
237 }
238 
239 inline boost::log::formatting_ostream& operator<<(
240  boost::log::formatting_ostream& _strm, bytesConstRef _value)
241 {
242  _strm.stream() << EthYellow "%" << toHex(_value) << EthReset;
243  return _strm;
244 }
245 } // namespace dev
246 
247 // Overloads for types of std namespace can't be defined in dev namespace, because they won't be
248 // found due to Argument-Dependent Lookup Placing anything into std is not allowed, but we can put
249 // them into boost::log
250 namespace boost
251 {
252 namespace log
253 {
254 inline boost::log::formatting_ostream& operator<<(
255  boost::log::formatting_ostream& _strm, dev::bytes const& _value)
256 {
257  _strm.stream() << EthYellow "%" << dev::toHex(_value) << EthReset;
258  return _strm;
259 }
260 inline boost::log::formatting_ostream& operator<<(
261  boost::log::formatting_ostream& _strm, dev::bytes& _value)
262 {
263  auto const& constValue = _value;
264  _strm << constValue;
265  return _strm;
266 }
267 
268 template <typename T>
269 inline boost::log::formatting_ostream& operator<<(
270  boost::log::formatting_ostream& _strm, std::vector<T> const& _value)
271 {
272  _strm.stream() << EthWhite "[" EthReset;
273  int n = 0;
274  for (T const& i : _value)
275  {
276  _strm.stream() << (n++ ? EthWhite ", " EthReset : "");
277  _strm << i;
278  }
279  _strm.stream() << EthWhite "]" EthReset;
280  return _strm;
281 }
282 template <typename T>
283 inline boost::log::formatting_ostream& operator<<(
284  boost::log::formatting_ostream& _strm, std::vector<T>& _value)
285 {
286  auto const& constValue = _value;
287  _strm << constValue;
288  return _strm;
289 }
290 
291 template <typename T>
292 inline boost::log::formatting_ostream& operator<<(
293  boost::log::formatting_ostream& _strm, std::set<T> const& _value)
294 {
295  _strm.stream() << EthYellow "{" EthReset;
296  int n = 0;
297  for (T const& i : _value)
298  {
299  _strm.stream() << (n++ ? EthYellow ", " EthReset : "");
300  _strm << i;
301  }
302  _strm.stream() << EthYellow "}" EthReset;
303  return _strm;
304 }
305 template <typename T>
306 inline boost::log::formatting_ostream& operator<<(
307  boost::log::formatting_ostream& _strm, std::set<T>& _value)
308 {
309  auto const& constValue = _value;
310  _strm << constValue;
311  return _strm;
312 }
313 
314 template <typename T>
315 inline boost::log::formatting_ostream& operator<<(
316  boost::log::formatting_ostream& _strm, std::unordered_set<T> const& _value)
317 {
318  _strm.stream() << EthYellow "{" EthReset;
319  int n = 0;
320  for (T const& i : _value)
321  {
322  _strm.stream() << (n++ ? EthYellow ", " EthReset : "");
323  _strm << i;
324  }
325  _strm.stream() << EthYellow "}" EthReset;
326  return _strm;
327 }
328 template <typename T>
329 inline boost::log::formatting_ostream& operator<<(
330  boost::log::formatting_ostream& _strm, std::unordered_set<T>& _value)
331 {
332  auto const& constValue = _value;
333  _strm << constValue;
334  return _strm;
335 }
336 
337 template <typename T, typename U>
338 inline boost::log::formatting_ostream& operator<<(
339  boost::log::formatting_ostream& _strm, std::map<T, U> const& _value)
340 {
341  _strm.stream() << EthLime "{" EthReset;
342  int n = 0;
343  for (auto const& i : _value)
344  {
345  _strm << (n++ ? EthLime ", " EthReset : "");
346  _strm << i.first;
347  _strm << (n++ ? EthLime ": " EthReset : "");
348  _strm << i.second;
349  }
350  _strm.stream() << EthLime "}" EthReset;
351  return _strm;
352 }
353 template <typename T, typename U>
354 inline boost::log::formatting_ostream& operator<<(
355  boost::log::formatting_ostream& _strm, std::map<T, U>& _value)
356 {
357  auto const& constValue = _value;
358  _strm << constValue;
359  return _strm;
360 }
361 
362 template <typename T, typename U>
363 inline boost::log::formatting_ostream& operator<<(
364  boost::log::formatting_ostream& _strm, std::unordered_map<T, U> const& _value)
365 {
366  _strm << EthLime "{" EthReset;
367  int n = 0;
368  for (auto const& i : _value)
369  {
370  _strm.stream() << (n++ ? EthLime ", " EthReset : "");
371  _strm << i.first;
372  _strm.stream() << (n++ ? EthLime ": " EthReset : "");
373  _strm << i.second;
374  }
375  _strm << EthLime "}" EthReset;
376  return _strm;
377 }
378 template <typename T, typename U>
379 inline boost::log::formatting_ostream& operator<<(
380  boost::log::formatting_ostream& _strm, std::unordered_map<T, U>& _value)
381 {
382  auto const& constValue = _value;
383  _strm << constValue;
384  return _strm;
385 }
386 
387 template <typename T, typename U>
388 inline boost::log::formatting_ostream& operator<<(
389  boost::log::formatting_ostream& _strm, std::pair<T, U> const& _value)
390 {
391  _strm.stream() << EthPurple "(" EthReset;
392  _strm << _value.first;
393  _strm.stream() << EthPurple ", " EthReset;
394  _strm << _value.second;
395  _strm.stream() << EthPurple ")" EthReset;
396  return _strm;
397 }
398 template <typename T, typename U>
399 inline boost::log::formatting_ostream& operator<<(
400  boost::log::formatting_ostream& _strm, std::pair<T, U>& _value)
401 {
402  auto const& constValue = _value;
403  _strm << constValue;
404  return _strm;
405 }
406 }
407 } // namespace boost
EthCyan
#define EthCyan
Definition: Terminal.h:97
EthWhite
#define EthWhite
Definition: Terminal.h:85
dev::vector_ref< byte const >
EthReset
#define EthReset
Definition: Terminal.h:79
FixedHash.h
EthTeal
#define EthTeal
Definition: Terminal.h:96
EthLime
#define EthLime
Definition: Terminal.h:89
dev::VerbosityWarning
@ VerbosityWarning
Definition: Log.h:69
boost
Definition: Log.h:251
dev::FixedHash
Definition: FixedHash.h:47
dev::VerbosityInfo
@ VerbosityInfo
Definition: Log.h:70
EthNavy
#define EthNavy
Definition: Terminal.h:92
Terminal.h
dev::VerbosityError
@ VerbosityError
Definition: Log.h:68
dev::operator<<
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:77
dev::setupLogging
void setupLogging(LoggingOptions const &_options)
Definition: Log.cpp:130
CommonIO.h
dev::Logger
boost::log::sources::severity_channel_logger<> Logger
Definition: Log.h:124
dev::BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS
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")) BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(g_warnLogger
EthRed
#define EthRed
Definition: Terminal.h:87
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::createLogger
Logger createLogger(int _severity, std::string const &_channel)
Definition: Log.h:125
dev::FixedHash::abridged
std::string abridged() const
Definition: FixedHash.h:123
dev::u160
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u160
Definition: Common.h:123
dev::VerbosityDebug
@ VerbosityDebug
Definition: Log.h:71
dev::strings
std::vector< std::string > strings
Definition: Common.h:143
dev::VerbositySilent
@ VerbositySilent
Definition: Log.h:67
dev::bigint
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<> > bigint
Definition: Common.h:118
boost::log::operator<<
boost::log::formatting_ostream & operator<<(boost::log::formatting_ostream &_strm, dev::bytes const &_value)
Definition: Log.h:254
dev::u256
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition: Common.h:121
dev::Verbosity
Verbosity
Definition: Log.h:66
EthYellow
#define EthYellow
Definition: Terminal.h:91
dev
Definition: Address.cpp:21
dev::toHex
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:46
EthPurple
#define EthPurple
Definition: Terminal.h:95
dev::VerbosityTrace
@ VerbosityTrace
Definition: Log.h:72
dev::setThreadName
void setThreadName(std::string const &_n)
Definition: Log.cpp:119
dev::getThreadName
std::string getThreadName()
Set the current thread's log name.
Definition: Log.cpp:107