BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
logging.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_LOGGING_H
7 #define BITCOIN_LOGGING_H
8 
9 #include <fs.h>
10 #include <tinyformat.h>
11 
12 #include <atomic>
13 #include <cstdint>
14 #include <list>
15 #include <mutex>
16 #include <string>
17 #include <vector>
18 
19 static const bool DEFAULT_LOGTIMEMICROS = false;
20 static const bool DEFAULT_LOGIPS = false;
21 static const bool DEFAULT_LOGTIMESTAMPS = true;
22 extern const char * const DEFAULT_DEBUGLOGFILE;
23 
24 extern bool fLogIPs;
25 
27 {
28  std::string category;
29  bool active;
30 };
31 
32 namespace BCLog {
33  enum LogFlags : uint32_t {
34  NONE = 0,
35  NET = (1 << 0),
36  TOR = (1 << 1),
37  MEMPOOL = (1 << 2),
38  HTTP = (1 << 3),
39  BENCH = (1 << 4),
40  ZMQ = (1 << 5),
41  DB = (1 << 6),
42  RPC = (1 << 7),
43  ESTIMATEFEE = (1 << 8),
44  ADDRMAN = (1 << 9),
45  SELECTCOINS = (1 << 10),
46  REINDEX = (1 << 11),
47  CMPCTBLOCK = (1 << 12),
48  RAND = (1 << 13),
49  PRUNE = (1 << 14),
50  PROXY = (1 << 15),
51  MEMPOOLREJ = (1 << 16),
52  LIBEVENT = (1 << 17),
53  COINDB = (1 << 18),
54  QT = (1 << 19),
55  LEVELDB = (1 << 20),
56  ALL = ~(uint32_t)0,
57  };
58 
59  class Logger
60  {
61  private:
62  FILE* m_fileout = nullptr;
63  std::mutex m_file_mutex;
64  std::list<std::string> m_msgs_before_open;
65 
71  std::atomic_bool m_started_new_line{true};
72 
74  std::atomic<uint32_t> m_categories{0};
75 
76  std::string LogTimestampStr(const std::string& str);
77 
78  public:
79  bool m_print_to_console = false;
80  bool m_print_to_file = false;
81 
82  bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
83  bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
84 
85  fs::path m_file_path;
86  std::atomic<bool> m_reopen_file{false};
87 
89  void LogPrintStr(const std::string &str);
90 
92  bool Enabled() const { return m_print_to_console || m_print_to_file; }
93 
94  bool OpenDebugLog();
95  void ShrinkDebugFile();
96 
97  uint32_t GetCategoryMask() const { return m_categories.load(); }
98 
99  void EnableCategory(LogFlags flag);
100  bool EnableCategory(const std::string& str);
101  void DisableCategory(LogFlags flag);
102  bool DisableCategory(const std::string& str);
103 
104  bool WillLogCategory(LogFlags category) const;
105 
106  bool DefaultShrinkDebugFile() const;
107  };
108 
109 } // namespace BCLog
110 
111 extern BCLog::Logger* const g_logger;
112 
114 static inline bool LogAcceptCategory(BCLog::LogFlags category)
115 {
116  return g_logger->WillLogCategory(category);
117 }
118 
120 std::string ListLogCategories();
121 
123 std::vector<CLogCategoryActive> ListActiveLogCategories();
124 
126 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
127 
128 // Be conservative when using LogPrintf/error or other things which
129 // unconditionally log to debug.log! It should not be the case that an inbound
130 // peer can fill up a user's disk with debug.log entries.
131 
132 template <typename... Args>
133 static inline void LogPrintf(const char* fmt, const Args&... args)
134 {
135  if (g_logger->Enabled()) {
136  std::string log_msg;
137  try {
138  log_msg = tfm::format(fmt, args...);
139  } catch (tinyformat::format_error& fmterr) {
140  /* Original format string will have newline so don't add one here */
141  log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
142  }
143  g_logger->LogPrintStr(log_msg);
144  }
145 }
146 
147 template <typename... Args>
148 static inline void LogPrint(const BCLog::LogFlags& category, const Args&... args)
149 {
150  if (LogAcceptCategory((category))) {
151  LogPrintf(args...);
152  }
153 }
154 
155 #endif // BITCOIN_LOGGING_H
void EnableCategory(LogFlags flag)
Definition: logging.cpp:55
fs::path m_file_path
Definition: logging.h:85
Definition: logging.h:32
bool fLogIPs
Definition: logging.cpp:26
std::vector< CLogCategoryActive > ListActiveLogCategories()
Returns a vector of the active log categories.
Definition: logging.cpp:156
std::atomic< bool > m_reopen_file
Definition: logging.h:86
bool m_print_to_console
Definition: logging.h:79
bool m_print_to_file
Definition: logging.h:80
void LogPrintStr(const std::string &str)
Send a string to the log output.
Definition: logging.cpp:201
bool m_log_time_micros
Definition: logging.h:83
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:967
uint32_t GetCategoryMask() const
Definition: logging.h:97
void DisableCategory(LogFlags flag)
Definition: logging.cpp:68
bool WillLogCategory(LogFlags category) const
Definition: logging.cpp:81
std::atomic< uint32_t > m_categories
Log categories bitfield.
Definition: logging.h:74
std::string ListLogCategories()
Returns a string with the log categories.
Definition: logging.cpp:141
std::mutex m_file_mutex
Definition: logging.h:63
std::atomic_bool m_started_new_line
m_started_new_line is a state variable that will suppress printing of the timestamp when multiple cal...
Definition: logging.h:71
FILE * m_fileout
Definition: logging.h:62
LogFlags
Definition: logging.h:33
std::string category
Definition: logging.h:28
BCLog::Logger *const g_logger
NOTE: the logger instances is leaked on exit.
Definition: logging.cpp:24
std::list< std::string > m_msgs_before_open
Definition: logging.h:64
bool OpenDebugLog()
Definition: logging.cpp:33
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
Definition: logging.cpp:126
bool m_log_timestamps
Definition: logging.h:82
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:9
bool Enabled() const
Returns whether logs will be written to any output.
Definition: logging.h:92
void ShrinkDebugFile()
Definition: logging.cpp:234
std::string LogTimestampStr(const std::string &str)
Definition: logging.cpp:171
bool DefaultShrinkDebugFile() const
Definition: logging.cpp:86