LCOV - code coverage report
Current view: top level - src - logging.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 45 47 95.7 %
Date: 2022-08-30 15:50:09 Functions: 293 732 40.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 346 1058 32.7 %

           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                 :            :         HEADERSSYNC = (1 << 27),
#      69                 :            :         ALL         = ~(uint32_t)0,
#      70                 :            :     };
#      71                 :            :     enum class Level {
#      72                 :            :         Debug = 0,
#      73                 :            :         None = 1,
#      74                 :            :         Info = 2,
#      75                 :            :         Warning = 3,
#      76                 :            :         Error = 4,
#      77                 :            :     };
#      78                 :            : 
#      79                 :            :     class Logger
#      80                 :            :     {
#      81                 :            :     private:
#      82                 :            :         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
#      83                 :            : 
#      84                 :            :         FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
#      85                 :            :         std::list<std::string> m_msgs_before_open GUARDED_BY(m_cs);
#      86                 :            :         bool m_buffering GUARDED_BY(m_cs) = true; //!< Buffer messages before logging can be started.
#      87                 :            : 
#      88                 :            :         /**
#      89                 :            :          * m_started_new_line is a state variable that will suppress printing of
#      90                 :            :          * the timestamp when multiple calls are made that don't end in a
#      91                 :            :          * newline.
#      92                 :            :          */
#      93                 :            :         std::atomic_bool m_started_new_line{true};
#      94                 :            : 
#      95                 :            :         /** Log categories bitfield. */
#      96                 :            :         std::atomic<uint32_t> m_categories{0};
#      97                 :            : 
#      98                 :            :         std::string LogTimestampStr(const std::string& str);
#      99                 :            : 
#     100                 :            :         /** Slots that connect to the print signal */
#     101                 :            :         std::list<std::function<void(const std::string&)>> m_print_callbacks GUARDED_BY(m_cs) {};
#     102                 :            : 
#     103                 :            :     public:
#     104                 :            :         bool m_print_to_console = false;
#     105                 :            :         bool m_print_to_file = false;
#     106                 :            : 
#     107                 :            :         bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
#     108                 :            :         bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
#     109                 :            :         bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
#     110                 :            :         bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
#     111                 :            : 
#     112                 :            :         fs::path m_file_path;
#     113                 :            :         std::atomic<bool> m_reopen_file{false};
#     114                 :            : 
#     115                 :            :         /** Send a string to the log output */
#     116                 :            :         void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level);
#     117                 :            : 
#     118                 :            :         /** Returns whether logs will be written to any output */
#     119                 :            :         bool Enabled() const
#     120                 :    4480315 :         {
#     121                 :    4480315 :             StdLockGuard scoped_lock(m_cs);
#     122 [ +  + ][ -  + ]:    4480315 :             return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
#         [ +  + ][ -  + ]
#     123                 :    4480315 :         }
#     124                 :            : 
#     125                 :            :         /** Connect a slot to the print signal and return the connection */
#     126                 :            :         std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun)
#     127                 :        869 :         {
#     128                 :        869 :             StdLockGuard scoped_lock(m_cs);
#     129                 :        869 :             m_print_callbacks.push_back(std::move(fun));
#     130                 :        869 :             return --m_print_callbacks.end();
#     131                 :        869 :         }
#     132                 :            : 
#     133                 :            :         /** Delete a connection */
#     134                 :            :         void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it)
#     135                 :         19 :         {
#     136                 :         19 :             StdLockGuard scoped_lock(m_cs);
#     137                 :         19 :             m_print_callbacks.erase(it);
#     138                 :         19 :         }
#     139                 :            : 
#     140                 :            :         /** Start logging (and flush all buffered messages) */
#     141                 :            :         bool StartLogging();
#     142                 :            :         /** Only for testing */
#     143                 :            :         void DisconnectTestLogger();
#     144                 :            : 
#     145                 :            :         void ShrinkDebugFile();
#     146                 :            : 
#     147                 :         14 :         uint32_t GetCategoryMask() const { return m_categories.load(); }
#     148                 :            : 
#     149                 :            :         void EnableCategory(LogFlags flag);
#     150                 :            :         bool EnableCategory(const std::string& str);
#     151                 :            :         void DisableCategory(LogFlags flag);
#     152                 :            :         bool DisableCategory(const std::string& str);
#     153                 :            : 
#     154                 :            :         bool WillLogCategory(LogFlags category) const;
#     155                 :            :         /** Returns a vector of the log categories in alphabetical order. */
#     156                 :            :         std::vector<LogCategory> LogCategoriesList() const;
#     157                 :            :         /** Returns a string with the log categories in alphabetical order. */
#     158                 :            :         std::string LogCategoriesString() const
#     159                 :       3355 :         {
#     160                 :      93940 :             return Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; });
#     161                 :       3355 :         };
#     162                 :            : 
#     163                 :            :         bool DefaultShrinkDebugFile() const;
#     164                 :            :     };
#     165                 :            : 
#     166                 :            : } // namespace BCLog
#     167                 :            : 
#     168                 :            : BCLog::Logger& LogInstance();
#     169                 :            : 
#     170                 :            : /** Return true if log accepts specified category, at the specified level. */
#     171                 :            : static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
#     172                 :   36690274 : {
#     173                 :            :     // Log messages at Warning and Error level unconditionally, so that
#     174                 :            :     // important troubleshooting information doesn't get lost.
#     175 [ #  # ][ -  + ]:   36690274 :     if (level >= BCLog::Level::Warning) {
#         [ #  # ][ #  # ]
#         [ -  + ][ #  # ]
#         [ -  + ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ +  + ][ -  + ]
#         [ #  # ][ -  + ]
#         [ -  + ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ #  # ][ -  + ]
#         [ +  + ][ -  + ]
#         [ #  # ][ #  # ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ #  # ][ -  + ]
#         [ #  # ][ #  # ]
#         [ -  + ][ #  # ]
#         [ -  + ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#                 [ #  # ]
#     176                 :        642 :         return true;
#     177                 :        642 :     }
#     178                 :   36689632 :     return LogInstance().WillLogCategory(category);
#     179                 :   36690274 : }
#     180                 :            : 
#     181                 :            : /** Return true if str parses as a log category and set the flag */
#     182                 :            : bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
#     183                 :            : 
#     184                 :            : // Be conservative when using LogPrintf/error or other things which
#     185                 :            : // unconditionally log to debug.log! It should not be the case that an inbound
#     186                 :            : // peer can fill up a user's disk with debug.log entries.
#     187                 :            : 
#     188                 :            : template <typename... Args>
#     189                 :            : static inline void LogPrintf_(const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char* fmt, const Args&... args)
#     190                 :    4480459 : {
#     191 [ #  # ][ #  # ]:    4480600 :     if (LogInstance().Enabled()) {
#         [ #  # ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ #  # ][ +  + ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  + ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  + ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  + ]
#         [ +  - ][ +  - ]
#         [ +  + ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  + ]
#         [ +  + ][ +  + ]
#         [ #  # ][ +  + ]
#         [ +  - ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  + ][ #  # ]
#         [ +  + ][ +  + ]
#         [ #  # ][ +  + ]
#         [ +  + ][ #  # ]
#         [ +  + ][ +  - ]
#         [ #  # ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  + ]
#         [ #  # ][ +  + ]
#         [ +  - ][ +  + ]
#         [ +  + ][ +  + ]
#         [ #  # ][ +  + ]
#         [ +  + ][ +  - ]
#         [ +  + ][ #  # ]
#         [ +  + ][ #  # ]
#         [ +  + ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ #  # ][ +  + ]
#         [ +  + ][ +  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  + ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  + ]
#         [ +  + ][ #  # ]
#         [ +  + ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ #  # ]
#         [ +  + ][ +  + ]
#         [ +  + ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  + ][ +  - ]
#         [ +  + ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  + ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  + ]
#         [ +  - ][ #  # ]
#         [ #  # ][ +  + ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ #  # ][ +  + ]
#         [ #  # ][ +  + ]
#         [ +  - ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  + ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  + ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ +  - ][ +  + ]
#         [ +  - ][ +  - ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  + ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  - ]
#         [ +  - ][ #  # ]
#         [ +  - ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ #  # ]
#         [ #  # ][ +  - ]
#         [ #  # ][ #  # ]
#         [ +  - ][ #  # ]
#         [ +  + ][ +  + ]
#         [ #  # ][ +  - ]
#         [ +  - ][ +  - ]
#         [ +  - ][ #  # ]
#         [ #  # ][ #  # ]
#         [ +  - ][ +  - ]
#         [ #  # ][ +  + ]
#         [ +  - ][ +  - ]
#         [ +  + ][ #  # ]
#         [ #  # ][ +  + ]
#     192                 :    4480600 :         std::string log_msg;
#     193                 :    4480600 :         try {
#     194                 :    4480600 :             log_msg = tfm::format(fmt, args...);
#     195                 :    4480600 :         } catch (tinyformat::format_error& fmterr) {
#     196                 :            :             /* Original format string will have newline so don't add one here */
#     197                 :          0 :             log_msg = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + fmt;
#     198                 :          0 :         }
#     199                 :    4480600 :         LogInstance().LogPrintStr(log_msg, logging_function, source_file, source_line, flag, level);
#     200                 :    4479609 :     }
#     201                 :    4480459 : }
#     202                 :            : 
#     203                 :    4487839 : #define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
#     204                 :            : 
#     205                 :            : // Log unconditionally.
#     206                 :     375901 : #define LogPrintf(...) LogPrintLevel_(BCLog::LogFlags::NONE, BCLog::Level::None, __VA_ARGS__)
#     207                 :            : 
#     208                 :            : // Log unconditionally, prefixing the output with the passed category name.
#     209                 :       1622 : #define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__)
#     210                 :            : 
#     211                 :            : // Use a macro instead of a function for conditional logging to prevent
#     212                 :            : // evaluating arguments when logging for the category is not enabled.
#     213                 :            : 
#     214                 :            : // Log conditionally, prefixing the output with the passed category name.
#     215                 :            : #define LogPrint(category, ...)                                        \
#     216                 :   36666049 :     do {                                                               \
#     217                 :   36666049 :         if (LogAcceptCategory((category), BCLog::Level::Debug)) {      \
#     218                 :    4109228 :             LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__); \
#     219                 :    4109228 :         }                                                              \
#     220                 :   36666049 :     } while (0)
#     221                 :            : 
#     222                 :            : // Log conditionally, prefixing the output with the passed category name and severity level.
#     223                 :            : #define LogPrintLevel(category, level, ...)               \
#     224                 :       1088 :     do {                                                  \
#     225                 :       1088 :         if (LogAcceptCategory((category), (level))) {     \
#     226                 :       1088 :             LogPrintLevel_(category, level, __VA_ARGS__); \
#     227                 :       1088 :         }                                                 \
#     228                 :       1088 :     } while (0)
#     229                 :            : 
#     230                 :            : #endif // BITCOIN_LOGGING_H

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a