Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2021 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 : : #include <threadsafety.h>
# 12 : : #include <util/string.h>
# 13 : :
# 14 : : #include <atomic>
# 15 : : #include <cstdint>
# 16 : : #include <functional>
# 17 : : #include <list>
# 18 : : #include <mutex>
# 19 : : #include <string>
# 20 : : #include <vector>
# 21 : :
# 22 : : static const bool DEFAULT_LOGTIMEMICROS = false;
# 23 : : static const bool DEFAULT_LOGIPS = false;
# 24 : : static const bool DEFAULT_LOGTIMESTAMPS = true;
# 25 : : static const bool DEFAULT_LOGTHREADNAMES = false;
# 26 : : static const bool DEFAULT_LOGSOURCELOCATIONS = false;
# 27 : : extern const char * const DEFAULT_DEBUGLOGFILE;
# 28 : :
# 29 : : extern bool fLogIPs;
# 30 : :
# 31 : : struct LogCategory {
# 32 : : std::string category;
# 33 : : bool active;
# 34 : : };
# 35 : :
# 36 : : namespace BCLog {
# 37 : : enum LogFlags : uint32_t {
# 38 : : NONE = 0,
# 39 : : NET = (1 << 0),
# 40 : : TOR = (1 << 1),
# 41 : : MEMPOOL = (1 << 2),
# 42 : : HTTP = (1 << 3),
# 43 : : BENCH = (1 << 4),
# 44 : : ZMQ = (1 << 5),
# 45 : : WALLETDB = (1 << 6),
# 46 : : RPC = (1 << 7),
# 47 : : ESTIMATEFEE = (1 << 8),
# 48 : : ADDRMAN = (1 << 9),
# 49 : : SELECTCOINS = (1 << 10),
# 50 : : REINDEX = (1 << 11),
# 51 : : CMPCTBLOCK = (1 << 12),
# 52 : : RAND = (1 << 13),
# 53 : : PRUNE = (1 << 14),
# 54 : : PROXY = (1 << 15),
# 55 : : MEMPOOLREJ = (1 << 16),
# 56 : : LIBEVENT = (1 << 17),
# 57 : : COINDB = (1 << 18),
# 58 : : QT = (1 << 19),
# 59 : : LEVELDB = (1 << 20),
# 60 : : VALIDATION = (1 << 21),
# 61 : : I2P = (1 << 22),
# 62 : : IPC = (1 << 23),
# 63 : : #ifdef DEBUG_LOCKCONTENTION
# 64 : : LOCK = (1 << 24),
# 65 : : #endif
# 66 : : UTIL = (1 << 25),
# 67 : : BLOCKSTORE = (1 << 26),
# 68 : : ALL = ~(uint32_t)0,
# 69 : : };
# 70 : :
# 71 : : class Logger
# 72 : : {
# 73 : : private:
# 74 : : mutable StdMutex m_cs; // Can not use Mutex from sync.h because in debug mode it would cause a deadlock when a potential deadlock was detected
# 75 : :
# 76 : : FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
# 77 : : std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
# 78 : : bool m_buffering GUARDED_BY(m_cs) = true; //!< Buffer messages before logging can be started.
# 79 : :
# 80 : : /**
# 81 : : * m_started_new_line is a state variable that will suppress printing of
# 82 : : * the timestamp when multiple calls are made that don't end in a
# 83 : : * newline.
# 84 : : */
# 85 : : std::atomic_bool m_started_new_line{true};
# 86 : :
# 87 : : /** Log categories bitfield. */
# 88 : : std::atomic<uint32_t> m_categories{0};
# 89 : :
# 90 : : std::string LogTimestampStr(const std::string& str);
# 91 : :
# 92 : : /** Slots that connect to the print signal */
# 93 : : std::list<std::function<void(const std::string&)>> m_print_callbacks GUARDED_BY(m_cs) {};
# 94 : :
# 95 : : public:
# 96 : : bool m_print_to_console = false;
# 97 : : bool m_print_to_file = false;
# 98 : :
# 99 : : bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
# 100 : : bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
# 101 : : bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
# 102 : : bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
# 103 : :
# 104 : : fs::path m_file_path;
# 105 : : std::atomic<bool> m_reopen_file{false};
# 106 : :
# 107 : : /** Send a string to the log output */
# 108 : : void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line);
# 109 : :
# 110 : : /** Returns whether logs will be written to any output */
# 111 : : bool Enabled() const
# 112 : 2748883 : {
# 113 : 2748883 : StdLockGuard scoped_lock(m_cs);
# 114 [ + + ][ - + ]: 2748883 : return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
# [ + + ][ - + ]
# 115 : 2748883 : }
# 116 : :
# 117 : : /** Connect a slot to the print signal and return the connection */
# 118 : : std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun)
# 119 : 852 : {
# 120 : 852 : StdLockGuard scoped_lock(m_cs);
# 121 : 852 : m_print_callbacks.push_back(std::move(fun));
# 122 : 852 : return --m_print_callbacks.end();
# 123 : 852 : }
# 124 : :
# 125 : : /** Delete a connection */
# 126 : : void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it)
# 127 : 19 : {
# 128 : 19 : StdLockGuard scoped_lock(m_cs);
# 129 : 19 : m_print_callbacks.erase(it);
# 130 : 19 : }
# 131 : :
# 132 : : /** Start logging (and flush all buffered messages) */
# 133 : : bool StartLogging();
# 134 : : /** Only for testing */
# 135 : : void DisconnectTestLogger();
# 136 : :
# 137 : : void ShrinkDebugFile();
# 138 : :
# 139 : 14 : uint32_t GetCategoryMask() const { return m_categories.load(); }
# 140 : :
# 141 : : void EnableCategory(LogFlags flag);
# 142 : : bool EnableCategory(const std::string& str);
# 143 : : void DisableCategory(LogFlags flag);
# 144 : : bool DisableCategory(const std::string& str);
# 145 : :
# 146 : : bool WillLogCategory(LogFlags category) const;
# 147 : : /** Returns a vector of the log categories in alphabetical order. */
# 148 : : std::vector<LogCategory> LogCategoriesList() const;
# 149 : : /** Returns a string with the log categories in alphabetical order. */
# 150 : : std::string LogCategoriesString() const
# 151 : 3264 : {
# 152 : 84864 : return Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; });
# 153 : 3264 : };
# 154 : :
# 155 : : bool DefaultShrinkDebugFile() const;
# 156 : : };
# 157 : :
# 158 : : } // namespace BCLog
# 159 : :
# 160 : : BCLog::Logger& LogInstance();
# 161 : :
# 162 : : /** Return true if log accepts specified category */
# 163 : : static inline bool LogAcceptCategory(BCLog::LogFlags category)
# 164 : 2424543 : {
# 165 : 2424543 : return LogInstance().WillLogCategory(category);
# 166 : 2424543 : }
# 167 : :
# 168 : : /** Return true if str parses as a log category and set the flag */
# 169 : : bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
# 170 : :
# 171 : : // Be conservative when using LogPrintf/error or other things which
# 172 : : // unconditionally log to debug.log! It should not be the case that an inbound
# 173 : : // peer can fill up a user's disk with debug.log entries.
# 174 : :
# 175 : : template <typename... Args>
# 176 : : static inline void LogPrintf_(const std::string& logging_function, const std::string& source_file, const int source_line, const char* fmt, const Args&... args)
# 177 : 2748870 : {
# 178 [ # # ][ + - ]: 2748870 : if (LogInstance().Enabled()) {
# [ + + ][ + - ]
# [ + - ][ + - ]
# [ # # ][ # # ]
# [ + + ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + + ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ + - ][ + + ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ # # ][ + - ]
# [ # # ][ + - ]
# [ + + ][ # # ]
# [ + + ][ + + ]
# [ + + ][ + + ]
# [ # # ][ + + ]
# [ + - ][ # # ]
# [ # # ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + - ]
# [ + - ][ # # ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ + + ][ + + ]
# [ # # ][ + - ]
# [ + - ][ + + ]
# [ + + ][ # # ]
# [ + + ][ + + ]
# [ + - ][ + + ]
# [ + - ][ # # ]
# [ + + ][ + + ]
# [ + - ][ + - ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ + + ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ + - ][ + + ]
# [ + + ][ # # ]
# [ + + ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + + ]
# [ + + ][ + + ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + + ]
# [ + - ][ + + ]
# [ # # ][ + - ]
# [ # # ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + + ]
# [ # # ][ # # ]
# [ + + ][ + - ]
# [ + + ][ + - ]
# [ # # ][ + - ]
# [ + - ][ + - ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ # # ][ # # ]
# [ + + ][ # # ]
# [ + + ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + + ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + + ]
# [ + - ][ # # ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ # # ][ + + ]
# [ + + ][ + + ]
# [ + + ][ # # ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + + ][ + - ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + + ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + - ][ # # ]
# [ # # ][ # # ]
# [ + - ][ + - ]
# [ + + ][ + + ]
# [ # # ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ + - ][ + + ]
# [ # # ][ # # ]
# [ + + ][ # # ]
# [ + - ][ # # ]
# [ # # ][ + + ]
# [ + - ][ # # ]
# [ # # ][ + - ]
# [ # # ][ # # ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + - ]
# [ # # ][ # # ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ + - ][ # # ]
# [ # # ][ + - ]
# [ # # ][ # # ]
# [ + - ][ + - ]
# [ # # ][ + - ]
# [ # # ][ + - ]
# [ + - ][ # # ]
# [ # # ][ # # ]
# [ # # ][ + - ]
# [ + - ][ # # ]
# [ + - ][ + - ]
# [ + - ][ + - ]
# [ # # ][ # # ]
# 179 : 2748726 : std::string log_msg;
# 180 : 2748726 : try {
# 181 : 2748726 : log_msg = tfm::format(fmt, args...);
# 182 : 2748726 : } catch (tinyformat::format_error& fmterr) {
# 183 : : /* Original format string will have newline so don't add one here */
# 184 : 0 : log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
# 185 : 0 : }
# 186 : 2748726 : LogInstance().LogPrintStr(log_msg, logging_function, source_file, source_line);
# 187 : 2748638 : }
# 188 : 2748870 : }
# 189 : :
# 190 : 2749348 : #define LogPrintf(...) LogPrintf_(__func__, __FILE__, __LINE__, __VA_ARGS__)
# 191 : :
# 192 : : // Use a macro instead of a function for conditional logging to prevent
# 193 : : // evaluating arguments when logging for the category is not enabled.
# 194 : : #define LogPrint(category, ...) \
# 195 : 2400580 : do { \
# 196 : 2400580 : if (LogAcceptCategory((category))) { \
# 197 : 2397387 : LogPrintf(__VA_ARGS__); \
# 198 : 2397387 : } \
# 199 : 2400580 : } while (0)
# 200 : :
# 201 : : #endif // BITCOIN_LOGGING_H
|