Fabcoin Core  0.16.2
P2P Digital Currency
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 */
24 #pragma once
25 
26 #include <ctime>
27 #include <chrono>
28 #include "vector_ref.h"
29 #include "Common.h"
30 #include "CommonIO.h"
31 #include "CommonData.h"
32 #include "FixedHash.h"
33 #include "Terminal.h"
34 
35 namespace boost { namespace asio { namespace ip { template<class T>class basic_endpoint; class tcp; } } }
36 
37 namespace dev
38 {
39 
42 {
43 public:
44  template <class T> NullOutputStream& operator<<(T const&) { return *this; }
45 };
46 
48 void simpleDebugOut(std::string const&, char const*);
49 
51 extern int g_logVerbosity;
52 
54 extern std::function<void(std::string const&, char const*)> g_logPost;
55 
57 {
58 protected:
59  LogOverrideAux(std::type_info const* _ch, bool _value);
60  ~LogOverrideAux();
61 
62 private:
63  std::type_info const* m_ch;
64  static const int c_null = -1;
65  int m_old;
66 };
67 
68 template <class Channel>
70 {
71 public:
72  LogOverride(bool _value): LogOverrideAux(&typeid(Channel), _value) {}
73 };
74 
75 bool isChannelVisible(std::type_info const* _ch, bool _default);
76 template <class Channel> bool isChannelVisible() { return isChannelVisible(&typeid(Channel), Channel::verbosity <= g_logVerbosity); }
77 
81 {
82  VerbosityHolder(int _temporaryValue, bool _force = false): oldLogVerbosity(g_logVerbosity) { if (g_logVerbosity >= 0 || _force) g_logVerbosity = _temporaryValue; }
83  ~VerbosityHolder() { g_logVerbosity = oldLogVerbosity; }
85 };
86 
87 #define ETH_THREAD_CONTEXT(name) for (std::pair<dev::ThreadContext, bool> __eth_thread_context(name, true); p.second; p.second = false)
88 
90 {
91 public:
92  ThreadContext(std::string const& _info) { push(_info); }
93  ~ThreadContext() { pop(); }
94 
95  static void push(std::string const& _n);
96  static void pop();
97  static std::string join(std::string const& _prior);
98 };
99 
123 void setThreadName(std::string const& _n);
124 
126 std::string getThreadName();
127 
130 struct LogChannel { static const char* name(); static const int verbosity = 1; static const bool debug = true; };
131 struct LeftChannel: public LogChannel { static const char* name(); };
132 struct RightChannel: public LogChannel { static const char* name(); };
133 struct WarnChannel: public LogChannel { static const char* name(); static const int verbosity = 0; static const bool debug = false; };
134 struct NoteChannel: public LogChannel { static const char* name(); static const bool debug = false; };
135 struct DebugChannel: public LogChannel { static const char* name(); static const int verbosity = 0; };
136 struct TraceChannel: public LogChannel { static const char* name(); static const int verbosity = 4; static const bool debug = true; };
137 
138 enum class LogTag
139 {
140  None,
141  Url,
142  Error,
143  Special
144 };
145 
147 {
148 public:
149  LogOutputStreamBase(char const* _id, std::type_info const* _info, unsigned _v, bool _autospacing);
150 
151  void comment(std::string const& _t)
152  {
153  switch (m_logTag)
154  {
155  case LogTag::Url: m_sstr << EthNavyUnder; break;
156  case LogTag::Error: m_sstr << EthRedBold; break;
157  case LogTag::Special: m_sstr << EthWhiteBold; break;
158  default:;
159  }
160  m_sstr << _t << EthReset;
161  m_logTag = LogTag::None;
162  }
163 
164  void append(unsigned long _t) { m_sstr << EthBlue << _t << EthReset; }
165  void append(long _t) { m_sstr << EthBlue << _t << EthReset; }
166  void append(unsigned int _t) { m_sstr << EthBlue << _t << EthReset; }
167  void append(int _t) { m_sstr << EthBlue << _t << EthReset; }
168  void append(bigint const& _t) { m_sstr << EthNavy << _t << EthReset; }
169  void append(u256 const& _t) { m_sstr << EthNavy << _t << EthReset; }
170  void append(u160 const& _t) { m_sstr << EthNavy << _t << EthReset; }
171  void append(double _t) { m_sstr << EthBlue << _t << EthReset; }
172  template <unsigned N> void append(FixedHash<N> const& _t) { m_sstr << EthTeal "#" << _t.abridged() << EthReset; }
173  void append(h160 const& _t) { m_sstr << EthRed "@" << _t.abridged() << EthReset; }
174  void append(h256 const& _t) { m_sstr << EthCyan "#" << _t.abridged() << EthReset; }
175  void append(h512 const& _t) { m_sstr << EthTeal "##" << _t.abridged() << EthReset; }
176  void append(std::string const& _t) { m_sstr << EthGreen "\"" + _t + "\"" EthReset; }
177  void append(bytes const& _t) { m_sstr << EthYellow "%" << toHex(_t) << EthReset; }
178  void append(bytesConstRef _t) { m_sstr << EthYellow "%" << toHex(_t) << EthReset; }
179  template <class T> void append(std::vector<T> const& _t)
180  {
181  m_sstr << EthWhite "[" EthReset;
182  int n = 0;
183  for (auto const& i: _t)
184  {
185  m_sstr << (n++ ? EthWhite ", " EthReset : "");
186  append(i);
187  }
188  m_sstr << EthWhite "]" EthReset;
189  }
190  template <class T> void append(std::set<T> const& _t)
191  {
192  m_sstr << EthYellow "{" EthReset;
193  int n = 0;
194  for (auto const& i: _t)
195  {
196  m_sstr << (n++ ? EthYellow ", " EthReset : "");
197  append(i);
198  }
199  m_sstr << EthYellow "}" EthReset;
200  }
201  template <class T, class U> void append(std::map<T, U> const& _t)
202  {
203  m_sstr << EthLime "{" EthReset;
204  int n = 0;
205  for (auto const& i: _t)
206  {
207  m_sstr << (n++ ? EthLime ", " EthReset : "");
208  append(i.first);
209  m_sstr << (n++ ? EthLime ": " EthReset : "");
210  append(i.second);
211  }
212  m_sstr << EthLime "}" EthReset;
213  }
214  template <class T> void append(std::unordered_set<T> const& _t)
215  {
216  m_sstr << EthYellow "{" EthReset;
217  int n = 0;
218  for (auto const& i: _t)
219  {
220  m_sstr << (n++ ? EthYellow ", " EthReset : "");
221  append(i);
222  }
223  m_sstr << EthYellow "}" EthReset;
224  }
225  template <class T, class U> void append(std::unordered_map<T, U> const& _t)
226  {
227  m_sstr << EthLime "{" EthReset;
228  int n = 0;
229  for (auto const& i: _t)
230  {
231  m_sstr << (n++ ? EthLime ", " EthReset : "");
232  append(i.first);
233  m_sstr << (n++ ? EthLime ": " EthReset : "");
234  append(i.second);
235  }
236  m_sstr << EthLime "}" EthReset;
237  }
238  template <class T, class U> void append(std::pair<T, U> const& _t)
239  {
240  m_sstr << EthPurple "(" EthReset;
241  append(_t.first);
242  m_sstr << EthPurple ", " EthReset;
243  append(_t.second);
244  m_sstr << EthPurple ")" EthReset;
245  }
246  template <class T> void append(T const& _t)
247  {
248  m_sstr << toString(_t);
249  }
250 
251 protected:
252  bool m_autospacing = false;
253  unsigned m_verbosity = 0;
254  std::stringstream m_sstr;
255  LogTag m_logTag = LogTag::None;
256 };
257 
259 template <class Id, bool _AutoSpacing = true>
261 {
262 public:
265  LogOutputStream(): LogOutputStreamBase(Id::name(), &typeid(Id), Id::verbosity, _AutoSpacing) {}
266 
268  ~LogOutputStream() { if (Id::verbosity <= g_logVerbosity) g_logPost(m_sstr.str(), Id::name()); }
269 
270  LogOutputStream& operator<<(std::string const& _t) { if (Id::verbosity <= g_logVerbosity) { if (_AutoSpacing && m_sstr.str().size() && m_sstr.str().back() != ' ') m_sstr << " "; comment(_t); } return *this; }
271 
272  LogOutputStream& operator<<(LogTag _t) { m_logTag = _t; return *this; }
273 
275  template <class T> LogOutputStream& operator<<(T const& _t) { if (Id::verbosity <= g_logVerbosity) { if (_AutoSpacing && m_sstr.str().size() && m_sstr.str().back() != ' ') m_sstr << " "; append(_t); } return *this; }
276 };
277 
281 #define DEV_STATEMENT_IF(COND) for (bool i_eth_if_ = (COND); i_eth_if_; i_eth_if_ = false)
282 #define DEV_STATEMENT_SKIP() while (/*CONSTCOND*/ false) /*NOTREACHED*/
286 // Kill all logs when when NLOG is defined.
287 #if NLOG
288 #define clog(X) nlog(X)
289 #define cslog(X) nslog(X)
290 #else
291 #if NDEBUG
292 #define clog(X) DEV_STATEMENT_IF(!(X::debug)) dev::LogOutputStream<X, true>()
293 #define cslog(X) DEV_STATEMENT_IF(!(X::debug)) dev::LogOutputStream<X, false>()
294 #else
295 #define clog(X) dev::LogOutputStream<X, true>()
296 #define cslog(X) dev::LogOutputStream<X, false>()
297 #endif
298 #endif
299 
300 // Simple cout-like stream objects for accessing common log channels.
301 // Dirties the global namespace, but oh so convenient...
302 #define cdebug clog(dev::DebugChannel)
303 #define cnote clog(dev::NoteChannel)
304 #define cwarn clog(dev::WarnChannel)
305 #define ctrace clog(dev::TraceChannel)
306 
307 // Null stream-like objects.
308 #define ndebug DEV_STATEMENT_SKIP() dev::NullOutputStream()
309 #define nlog(X) DEV_STATEMENT_SKIP() dev::NullOutputStream()
310 #define nslog(X) DEV_STATEMENT_SKIP() dev::NullOutputStream()
311 
312 }
LogOutputStream()
Construct a new object.
Definition: Log.h:265
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
void append(std::string const &_t)
Definition: Log.h:176
std::type_info const * m_ch
Definition: Log.h:63
Definition: Log.h:35
#define EthRedBold
Definition: Terminal.h:138
LogOverride(bool _value)
Definition: Log.h:72
#define EthCyan
Definition: Terminal.h:131
void simpleDebugOut(std::string const &, char const *)
A simple log-output function that prints log messages to stdout.
Definition: Log.cpp:199
void append(std::pair< T, U > const &_t)
Definition: Log.h:238
#define T(i, x)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
~LogOutputStream()
Destructor. Posts the accrued log entry to the g_logPost function.
Definition: Log.h:268
void append(T const &_t)
Definition: Log.h:246
LogOutputStream & operator<<(T const &_t)
Shift arbitrary data to the log. Spaces will be added between items as required.
Definition: Log.h:275
#define EthNavyUnder
Definition: Terminal.h:199
#define EthGreen
Definition: Terminal.h:122
EXPORT void debug(uint64_t a, uint64_t b, uint64_t c, uint64_t d, char z)
Definition: Arith256.cpp:415
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
ThreadContext(std::string const &_info)
Definition: Log.h:92
#define EthPurple
Definition: Terminal.h:129
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 160, 160, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u160
Definition: Common.h:127
void append(bigint const &_t)
Definition: Log.h:168
void append(std::unordered_set< T > const &_t)
Definition: Log.h:214
void append(u160 const &_t)
Definition: Log.h:170
std::stringstream m_sstr
The accrued log entry.
Definition: Log.h:254
VerbosityHolder(int _temporaryValue, bool _force=false)
Definition: Log.h:82
~ThreadContext()
Definition: Log.h:93
void append(unsigned int _t)
Definition: Log.h:166
void append(int _t)
Definition: Log.h:167
const char * name
Definition: rest.cpp:36
void append(std::map< T, U > const &_t)
Definition: Log.h:201
std::function< void(std::string const &, char const *)> g_logPost
The current method that the logging system uses to output the log messages. Defaults to simpleDebugOu...
Definition: Log.cpp:215
void append(bytes const &_t)
Definition: Log.h:177
LogOutputStream & operator<<(std::string const &_t)
Definition: Log.h:270
#define EthWhite
Definition: Terminal.h:119
void setThreadName(std::string const &_n)
Set the current thread&#39;s log name.
void append(h512 const &_t)
Definition: Log.h:175
void append(long _t)
Definition: Log.h:165
std::vector< byte > bytes
Definition: Common.h:75
void append(bytesConstRef _t)
Definition: Log.h:178
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
std::string getThreadName()
Set the current thread&#39;s log name.
Definition: Log.cpp:170
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
void append(std::unordered_map< T, U > const &_t)
Definition: Log.h:225
void append(h160 const &_t)
Definition: Log.h:173
Temporary changes system&#39;s verbosity for specific function.
Definition: Log.h:80
#define EthLime
Definition: Terminal.h:123
void append(FixedHash< N > const &_t)
Definition: Log.h:172
void append(unsigned long _t)
Definition: Log.h:164
int g_logVerbosity
The logging system&#39;s current verbosity.
Definition: Log.cpp:37
LogTag
Definition: Log.h:138
void append(std::vector< T > const &_t)
Definition: Log.h:179
NullOutputStream & operator<<(T const &)
Definition: Log.h:44
The null output stream. Used when logging is disabled.
Definition: Log.h:41
#define EthNavy
Definition: Terminal.h:126
LogOutputStream & operator<<(LogTag _t)
Definition: Log.h:272
void append(std::set< T > const &_t)
Definition: Log.h:190
#define EthTeal
Definition: Terminal.h:130
void append(h256 const &_t)
Definition: Log.h:174
void append(u256 const &_t)
Definition: Log.h:169
std::string abridged() const
Definition: FixedHash.h:124
#define EthWhiteBold
Definition: Terminal.h:136
The default logging channels.
Definition: Log.h:130
#define EthYellow
Definition: Terminal.h:125
#define EthBlue
Definition: Terminal.h:127
void comment(std::string const &_t)
Definition: Log.h:151
#define EthRed
Definition: Terminal.h:121
#define EthReset
Definition: Terminal.h:80
int oldLogVerbosity
Definition: Log.h:84
Logging class, iostream-like, that can be shifted to.
Definition: Log.h:260
bool isChannelVisible()
Definition: Log.h:76
void append(double _t)
Definition: Log.h:171