LCOV - code coverage report
Current view: top level - src - logging.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 49.0 % 49 24
Test Date: 2025-12-22 04:18:44 Functions: 29.1 % 175 51
Branches: 8.0 % 1684 134

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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 <crypto/siphash.h>
      10                 :             : #include <threadsafety.h>
      11                 :             : #include <tinyformat.h>
      12                 :             : #include <util/check.h>
      13                 :             : #include <util/fs.h>
      14                 :             : #include <util/string.h>
      15                 :             : #include <util/time.h>
      16                 :             : 
      17                 :             : #include <atomic>
      18                 :             : #include <cstdint>
      19                 :             : #include <cstring>
      20                 :             : #include <functional>
      21                 :             : #include <list>
      22                 :             : #include <memory>
      23                 :             : #include <mutex>
      24                 :             : #include <source_location>
      25                 :             : #include <string>
      26                 :             : #include <unordered_map>
      27                 :             : #include <unordered_set>
      28                 :             : #include <vector>
      29                 :             : 
      30                 :             : static const bool DEFAULT_LOGTIMEMICROS = false;
      31                 :             : static const bool DEFAULT_LOGIPS        = false;
      32                 :             : static const bool DEFAULT_LOGTIMESTAMPS = true;
      33                 :             : static const bool DEFAULT_LOGTHREADNAMES = false;
      34                 :             : static const bool DEFAULT_LOGSOURCELOCATIONS = false;
      35                 :             : static constexpr bool DEFAULT_LOGLEVELALWAYS = false;
      36                 :             : extern const char * const DEFAULT_DEBUGLOGFILE;
      37                 :             : 
      38                 :             : extern bool fLogIPs;
      39                 :             : 
      40                 :             : /// Like std::source_location, but allowing to override the function name.
      41                 :             : class SourceLocation
      42                 :             : {
      43                 :             : public:
      44                 :             :     /// The func argument must be constructed from the C++11 __func__ macro.
      45                 :             :     /// Ref: https://en.cppreference.com/w/cpp/language/function.html#func
      46                 :             :     /// Non-static string literals are not supported.
      47                 :     6066223 :     SourceLocation(const char* func,
      48                 :             :                    std::source_location loc = std::source_location::current())
      49   [ -  -  -  -  :     6066223 :         : m_func{func}, m_loc{loc} {}
          +  -  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ +  
          -  +  -  -  -  
          -  -  -  -  -  
             -  -  -  +  
           - ][ +  -  +  
          -  -  -  -  -  
          -  -  +  -  -  
             -  #  #  #  
           # ][ +  -  +  
          -  -  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
           - ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
                -  -  - ]
           [ +  -  +  - ]
           [ +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  -  
          -  -  +  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  +  -  
          +  -  +  -  -  
          -  -  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  +  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  -  -  
          +  -  -  -  +  
          -  +  -  +  -  
          +  -  +  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
             -  -  -  -  
           - ][ -  -  -  
          -  +  -  +  -  
          +  -  -  -  +  
          -  -  -  -  -  
          +  -  +  -  +  
          -  +  -  -  -  
          +  -  -  -  -  
           - ][ +  -  +  
          -  +  -  +  -  
          -  -  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  +  -  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
           [ -  -  +  -  
          +  -  +  -  -  
          -  -  -  +  -  
          -  -  -  -  +  
          -  +  -  -  -  
          -  -  #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
           -  - ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  #  # ][ -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  -  -  +  -  
          -  -  +  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
           - ][ +  -  -  
          -  -  -  -  -  
          +  -  +  -  +  
          -  -  -  -  -  
          -  -  -  -  +  
          -  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
             -  -  -  +  
           - ][ -  -  +  
             -  -  -  +  
           - ][ -  -  +  
           -  +  - ][ #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
           #  # ][ #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
           # ][ -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  +  -  +  
          -  +  -  +  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  +  -  -  -  
          +  -  +  -  +  
             -  -  -  -  
           - ][ #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
              # ][ +  - ]
      50                 :             : 
      51         [ +  - ]:     4043498 :     std::string_view file_name() const { return m_loc.file_name(); }
      52   [ -  -  -  -  :     4043498 :     std::uint_least32_t line() const { return m_loc.line(); }
                   -  - ]
      53   [ -  -  -  -  :     4043498 :     std::string_view function_name_short() const { return m_func; }
                   +  - ]
      54                 :             : 
      55                 :             : private:
      56                 :             :     std::string_view m_func;
      57                 :             :     std::source_location m_loc;
      58                 :             : };
      59                 :             : 
      60                 :             : struct SourceLocationEqual {
      61                 :           0 :     bool operator()(const SourceLocation& lhs, const SourceLocation& rhs) const noexcept
      62                 :             :     {
      63   [ #  #  #  #  :           0 :         return lhs.line() == rhs.line() && std::string_view(lhs.file_name()) == std::string_view(rhs.file_name());
             #  #  #  # ]
      64                 :             :     }
      65                 :             : };
      66                 :             : 
      67                 :             : struct SourceLocationHasher {
      68                 :           0 :     size_t operator()(const SourceLocation& s) const noexcept
      69                 :             :     {
      70                 :             :         // Use CSipHasher(0, 0) as a simple way to get uniform distribution.
      71         [ #  # ]:           0 :         return size_t(CSipHasher(0, 0)
      72                 :           0 :                       .Write(s.line())
      73                 :           0 :                       .Write(MakeUCharSpan(std::string_view{s.file_name()}))
      74                 :           0 :                       .Finalize());
      75                 :             :     }
      76                 :             : };
      77                 :             : 
      78                 :      251343 : struct LogCategory {
      79                 :             :     std::string category;
      80                 :             :     bool active;
      81                 :             : };
      82                 :             : 
      83                 :             : namespace BCLog {
      84                 :             :     using CategoryMask = uint64_t;
      85                 :             :     enum LogFlags : CategoryMask {
      86                 :             :         NONE        = CategoryMask{0},
      87                 :             :         NET         = (CategoryMask{1} <<  0),
      88                 :             :         TOR         = (CategoryMask{1} <<  1),
      89                 :             :         MEMPOOL     = (CategoryMask{1} <<  2),
      90                 :             :         HTTP        = (CategoryMask{1} <<  3),
      91                 :             :         BENCH       = (CategoryMask{1} <<  4),
      92                 :             :         ZMQ         = (CategoryMask{1} <<  5),
      93                 :             :         WALLETDB    = (CategoryMask{1} <<  6),
      94                 :             :         RPC         = (CategoryMask{1} <<  7),
      95                 :             :         ESTIMATEFEE = (CategoryMask{1} <<  8),
      96                 :             :         ADDRMAN     = (CategoryMask{1} <<  9),
      97                 :             :         SELECTCOINS = (CategoryMask{1} << 10),
      98                 :             :         REINDEX     = (CategoryMask{1} << 11),
      99                 :             :         CMPCTBLOCK  = (CategoryMask{1} << 12),
     100                 :             :         RAND        = (CategoryMask{1} << 13),
     101                 :             :         PRUNE       = (CategoryMask{1} << 14),
     102                 :             :         PROXY       = (CategoryMask{1} << 15),
     103                 :             :         MEMPOOLREJ  = (CategoryMask{1} << 16),
     104                 :             :         LIBEVENT    = (CategoryMask{1} << 17),
     105                 :             :         COINDB      = (CategoryMask{1} << 18),
     106                 :             :         QT          = (CategoryMask{1} << 19),
     107                 :             :         LEVELDB     = (CategoryMask{1} << 20),
     108                 :             :         VALIDATION  = (CategoryMask{1} << 21),
     109                 :             :         I2P         = (CategoryMask{1} << 22),
     110                 :             :         IPC         = (CategoryMask{1} << 23),
     111                 :             : #ifdef DEBUG_LOCKCONTENTION
     112                 :             :         LOCK        = (CategoryMask{1} << 24),
     113                 :             : #endif
     114                 :             :         BLOCKSTORAGE = (CategoryMask{1} << 25),
     115                 :             :         TXRECONCILIATION = (CategoryMask{1} << 26),
     116                 :             :         SCAN        = (CategoryMask{1} << 27),
     117                 :             :         TXPACKAGES  = (CategoryMask{1} << 28),
     118                 :             :         KERNEL      = (CategoryMask{1} << 29),
     119                 :             :         ALL         = ~NONE,
     120                 :             :     };
     121                 :             :     enum class Level {
     122                 :             :         Trace = 0, // High-volume or detailed logging for development/debugging
     123                 :             :         Debug,     // Reasonably noisy logging, but still usable in production
     124                 :             :         Info,      // Default
     125                 :             :         Warning,
     126                 :             :         Error,
     127                 :             :     };
     128                 :             :     constexpr auto DEFAULT_LOG_LEVEL{Level::Debug};
     129                 :             :     constexpr size_t DEFAULT_MAX_LOG_BUFFER{1'000'000}; // buffer up to 1MB of log data prior to StartLogging
     130                 :             :     constexpr uint64_t RATELIMIT_MAX_BYTES{1024 * 1024}; // maximum number of bytes per source location that can be logged within the RATELIMIT_WINDOW
     131                 :             :     constexpr auto RATELIMIT_WINDOW{1h}; // time window after which log ratelimit stats are reset
     132                 :             :     constexpr bool DEFAULT_LOGRATELIMIT{true};
     133                 :             : 
     134                 :             :     //! Fixed window rate limiter for logging.
     135                 :           0 :     class LogRateLimiter
     136                 :             :     {
     137                 :             :     public:
     138                 :             :         //! Keeps track of an individual source location and how many available bytes are left for logging from it.
     139                 :             :         struct Stats {
     140                 :             :             //! Remaining bytes
     141                 :             :             uint64_t m_available_bytes;
     142                 :             :             //! Number of bytes that were consumed but didn't fit in the available bytes.
     143                 :             :             uint64_t m_dropped_bytes{0};
     144                 :             : 
     145                 :           0 :             Stats(uint64_t max_bytes) : m_available_bytes{max_bytes} {}
     146                 :             :             //! Updates internal accounting and returns true if enough available_bytes were remaining
     147                 :             :             bool Consume(uint64_t bytes);
     148                 :             :         };
     149                 :             : 
     150                 :             :     private:
     151                 :             :         mutable StdMutex m_mutex;
     152                 :             : 
     153                 :             :         //! Stats for each source location that has attempted to log something.
     154                 :             :         std::unordered_map<SourceLocation, Stats, SourceLocationHasher, SourceLocationEqual> m_source_locations GUARDED_BY(m_mutex);
     155                 :             :         //! Whether any log locations are suppressed. Cached view on m_source_locations for performance reasons.
     156                 :             :         std::atomic<bool> m_suppression_active{false};
     157                 :             :         LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window);
     158                 :             : 
     159                 :             :     public:
     160                 :             :         using SchedulerFunction = std::function<void(std::function<void()>, std::chrono::milliseconds)>;
     161                 :             :         /**
     162                 :             :          * @param scheduler_func    Callable object used to schedule resetting the window. The first
     163                 :             :          *                          parameter is the function to be executed, and the second is the
     164                 :             :          *                          reset_window interval.
     165                 :             :          * @param max_bytes         Maximum number of bytes that can be logged for each source
     166                 :             :          *                          location.
     167                 :             :          * @param reset_window      Time window after which the stats are reset.
     168                 :             :          */
     169                 :             :         static std::shared_ptr<LogRateLimiter> Create(
     170                 :             :             SchedulerFunction&& scheduler_func,
     171                 :             :             uint64_t max_bytes,
     172                 :             :             std::chrono::seconds reset_window);
     173                 :             :         //! Maximum number of bytes logged per location per window.
     174                 :             :         const uint64_t m_max_bytes;
     175                 :             :         //! Interval after which the window is reset.
     176                 :             :         const std::chrono::seconds m_reset_window;
     177                 :             :         //! Suppression status of a source log location.
     178                 :             :         enum class Status {
     179                 :             :             UNSUPPRESSED,     // string fits within the limit
     180                 :             :             NEWLY_SUPPRESSED, // suppression has started since this string
     181                 :             :             STILL_SUPPRESSED, // suppression is still ongoing
     182                 :             :         };
     183                 :             :         //! Consumes `source_loc`'s available bytes corresponding to the size of the (formatted)
     184                 :             :         //! `str` and returns its status.
     185                 :             :         [[nodiscard]] Status Consume(
     186                 :             :             const SourceLocation& source_loc,
     187                 :             :             const std::string& str) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     188                 :             :         //! Resets all usage to zero. Called periodically by the scheduler.
     189                 :             :         void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
     190                 :             :         //! Returns true if any log locations are currently being suppressed.
     191         [ #  # ]:           0 :         bool SuppressionsActive() const { return m_suppression_active; }
     192                 :             :     };
     193                 :             : 
     194                 :             :     class Logger
     195                 :             :     {
     196                 :             :     public:
     197                 :       30884 :         struct BufferedLog {
     198                 :             :             SystemClock::time_point now;
     199                 :             :             std::chrono::seconds mocktime;
     200                 :             :             std::string str, threadname;
     201                 :             :             SourceLocation source_loc;
     202                 :             :             LogFlags category;
     203                 :             :             Level level;
     204                 :             :         };
     205                 :             : 
     206                 :             :     private:
     207                 :             :         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
     208                 :             : 
     209                 :             :         FILE* m_fileout GUARDED_BY(m_cs) = nullptr;
     210                 :             :         std::list<BufferedLog> m_msgs_before_open GUARDED_BY(m_cs);
     211                 :             :         bool m_buffering GUARDED_BY(m_cs) = true; //!< Buffer messages before logging can be started.
     212                 :             :         size_t m_max_buffer_memusage GUARDED_BY(m_cs){DEFAULT_MAX_LOG_BUFFER};
     213                 :             :         size_t m_cur_buffer_memusage GUARDED_BY(m_cs){0};
     214                 :             :         size_t m_buffer_lines_discarded GUARDED_BY(m_cs){0};
     215                 :             : 
     216                 :             :         //! Manages the rate limiting of each log location.
     217                 :             :         std::shared_ptr<LogRateLimiter> m_limiter GUARDED_BY(m_cs);
     218                 :             : 
     219                 :             :         //! Category-specific log level. Overrides `m_log_level`.
     220                 :             :         std::unordered_map<LogFlags, Level> m_category_log_levels GUARDED_BY(m_cs);
     221                 :             : 
     222                 :             :         //! If there is no category-specific log level, all logs with a severity
     223                 :             :         //! level lower than `m_log_level` will be ignored.
     224                 :             :         std::atomic<Level> m_log_level{DEFAULT_LOG_LEVEL};
     225                 :             : 
     226                 :             :         /** Log categories bitfield. */
     227                 :             :         std::atomic<CategoryMask> m_categories{BCLog::NONE};
     228                 :             : 
     229                 :             :         void FormatLogStrInPlace(std::string& str, LogFlags category, Level level, const SourceLocation& source_loc, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const;
     230                 :             : 
     231                 :             :         std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const;
     232                 :             : 
     233                 :             :         /** Slots that connect to the print signal */
     234                 :             :         std::list<std::function<void(const std::string&)>> m_print_callbacks GUARDED_BY(m_cs) {};
     235                 :             : 
     236                 :             :         /** Send a string to the log output (internal) */
     237                 :             :         void LogPrintStr_(std::string_view str, SourceLocation&& source_loc, BCLog::LogFlags category, BCLog::Level level, bool should_ratelimit)
     238                 :             :             EXCLUSIVE_LOCKS_REQUIRED(m_cs);
     239                 :             : 
     240                 :             :         std::string GetLogPrefix(LogFlags category, Level level) const;
     241                 :             : 
     242                 :             :     public:
     243                 :             :         bool m_print_to_console = false;
     244                 :             :         bool m_print_to_file = false;
     245                 :             : 
     246                 :             :         bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
     247                 :             :         bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
     248                 :             :         bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
     249                 :             :         bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
     250                 :             :         bool m_always_print_category_level = DEFAULT_LOGLEVELALWAYS;
     251                 :             : 
     252                 :             :         fs::path m_file_path;
     253                 :             :         std::atomic<bool> m_reopen_file{false};
     254                 :             : 
     255                 :             :         /** Send a string to the log output */
     256                 :             :         void LogPrintStr(std::string_view str, SourceLocation&& source_loc, BCLog::LogFlags category, BCLog::Level level, bool should_ratelimit)
     257                 :             :             EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
     258                 :             : 
     259                 :             :         /** Returns whether logs will be written to any output */
     260                 :     6066223 :         bool Enabled() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
     261                 :             :         {
     262                 :     6066223 :             StdLockGuard scoped_lock(m_cs);
     263   [ +  +  +  -  :     6066223 :             return m_buffering || m_print_to_console || m_print_to_file || !m_print_callbacks.empty();
             +  +  -  + ]
     264                 :     6066223 :         }
     265                 :             : 
     266                 :             :         /** Connect a slot to the print signal and return the connection */
     267                 :           0 :         std::list<std::function<void(const std::string&)>>::iterator PushBackCallback(std::function<void(const std::string&)> fun) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
     268                 :             :         {
     269                 :           0 :             StdLockGuard scoped_lock(m_cs);
     270         [ #  # ]:           0 :             m_print_callbacks.push_back(std::move(fun));
     271                 :           0 :             return --m_print_callbacks.end();
     272                 :           0 :         }
     273                 :             : 
     274                 :             :         /** Delete a connection */
     275                 :           0 :         void DeleteCallback(std::list<std::function<void(const std::string&)>>::iterator it) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
     276                 :             :         {
     277                 :           0 :             StdLockGuard scoped_lock(m_cs);
     278                 :           0 :             m_print_callbacks.erase(it);
     279                 :           0 :         }
     280                 :             : 
     281                 :             :         size_t NumConnections()
     282                 :             :         {
     283                 :             :             StdLockGuard scoped_lock(m_cs);
     284                 :             :             return m_print_callbacks.size();
     285                 :             :         }
     286                 :             : 
     287                 :             :         /** Start logging (and flush all buffered messages) */
     288                 :             :         bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
     289                 :             :         /** Only for testing */
     290                 :             :         void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
     291                 :             : 
     292                 :           0 :         void SetRateLimiting(std::shared_ptr<LogRateLimiter> limiter) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
     293                 :             :         {
     294                 :           0 :             StdLockGuard scoped_lock(m_cs);
     295                 :           0 :             m_limiter = std::move(limiter);
     296                 :           0 :         }
     297                 :             : 
     298                 :             :         /** Disable logging
     299                 :             :          * This offers a slight speedup and slightly smaller memory usage
     300                 :             :          * compared to leaving the logging system in its default state.
     301                 :             :          * Mostly intended for libbitcoin-kernel apps that don't want any logging.
     302                 :             :          * Should be used instead of StartLogging().
     303                 :             :          */
     304                 :             :         void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
     305                 :             : 
     306                 :             :         void ShrinkDebugFile();
     307                 :             : 
     308                 :             :         std::unordered_map<LogFlags, Level> CategoryLevels() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
     309                 :             :         {
     310                 :             :             StdLockGuard scoped_lock(m_cs);
     311                 :             :             return m_category_log_levels;
     312                 :             :         }
     313                 :             :         void SetCategoryLogLevel(const std::unordered_map<LogFlags, Level>& levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
     314                 :             :         {
     315                 :             :             StdLockGuard scoped_lock(m_cs);
     316                 :             :             m_category_log_levels = levels;
     317                 :             :         }
     318                 :             :         void AddCategoryLogLevel(LogFlags category, Level level)
     319                 :             :         {
     320                 :             :             StdLockGuard scoped_lock(m_cs);
     321                 :             :             m_category_log_levels[category] = level;
     322                 :             :         }
     323                 :             :         bool SetCategoryLogLevel(std::string_view category_str, std::string_view level_str) EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
     324                 :             : 
     325                 :     3744399 :         Level LogLevel() const { return m_log_level.load(); }
     326                 :             :         void SetLogLevel(Level level) { m_log_level = level; }
     327                 :             :         bool SetLogLevel(std::string_view level);
     328                 :             : 
     329         [ +  + ]:         180 :         CategoryMask GetCategoryMask() const { return m_categories.load(); }
     330                 :             : 
     331                 :             :         void EnableCategory(LogFlags flag);
     332                 :             :         bool EnableCategory(std::string_view str);
     333                 :             :         void DisableCategory(LogFlags flag);
     334                 :             :         bool DisableCategory(std::string_view str);
     335                 :             : 
     336                 :             :         bool WillLogCategory(LogFlags category) const;
     337                 :             :         bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
     338                 :             : 
     339                 :             :         /** Returns a vector of the log categories in alphabetical order. */
     340                 :             :         std::vector<LogCategory> LogCategoriesList() const;
     341                 :             :         /** Returns a string with the log categories in alphabetical order. */
     342                 :        2826 :         std::string LogCategoriesString() const
     343                 :             :         {
     344   [ +  -  -  +  :      166734 :             return util::Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; });
                   -  + ]
     345                 :             :         };
     346                 :             : 
     347                 :             :         //! Returns a string with all user-selectable log levels.
     348                 :             :         std::string LogLevelsString() const;
     349                 :             : 
     350                 :             :         //! Returns the string representation of a log level.
     351                 :             :         static std::string LogLevelToStr(BCLog::Level level);
     352                 :             : 
     353                 :             :         bool DefaultShrinkDebugFile() const;
     354                 :             :     };
     355                 :             : 
     356                 :             : } // namespace BCLog
     357                 :             : 
     358                 :             : BCLog::Logger& LogInstance();
     359                 :             : 
     360                 :             : /** Return true if log accepts specified category, at the specified level. */
     361                 :    42405958 : static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
     362                 :             : {
     363                 :    42405958 :     return LogInstance().WillLogCategoryLevel(category, level);
     364                 :             : }
     365                 :             : 
     366                 :             : /** Return true if str parses as a log category and set the flag */
     367                 :             : bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str);
     368                 :             : 
     369                 :             : template <typename... Args>
     370                 :     6066223 : inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags flag, BCLog::Level level, bool should_ratelimit, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
     371                 :             : {
     372         [ +  + ]:     6066223 :     if (LogInstance().Enabled()) {
     373         [ +  - ]:     4063469 :         std::string log_msg;
     374                 :             :         try {
     375         [ +  - ]:     4063469 :             log_msg = tfm::format(fmt, args...);
     376         [ -  - ]:           0 :         } catch (tinyformat::format_error& fmterr) {
     377   [ -  -  -  -  :           0 :             log_msg = "Error \"" + std::string{fmterr.what()} + "\" while formatting log message: " + fmt.fmt;
                   -  - ]
     378                 :             :         }
     379   [ +  -  -  +  :     4063469 :         LogInstance().LogPrintStr(log_msg, std::move(source_loc), flag, level, should_ratelimit);
                   +  - ]
     380                 :     4063469 :     }
     381                 :     6066223 : }
     382                 :             : 
     383                 :             : // Allow __func__ to be used in any context without warnings:
     384                 :             : // NOLINTNEXTLINE(bugprone-lambda-function-name)
     385                 :             : #define LogPrintLevel_(category, level, should_ratelimit, ...) LogPrintFormatInternal(SourceLocation{__func__}, category, level, should_ratelimit, __VA_ARGS__)
     386                 :             : 
     387                 :             : // Log unconditionally. Uses basic rate limiting to mitigate disk filling attacks.
     388                 :             : // Be conservative when using functions that unconditionally log to debug.log!
     389                 :             : // It should not be the case that an inbound peer can fill up a user's storage
     390                 :             : // with debug.log entries.
     391                 :             : #define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, /*should_ratelimit=*/true, __VA_ARGS__)
     392                 :             : #define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, /*should_ratelimit=*/true, __VA_ARGS__)
     393                 :             : #define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, /*should_ratelimit=*/true, __VA_ARGS__)
     394                 :             : 
     395                 :             : // Use a macro instead of a function for conditional logging to prevent
     396                 :             : // evaluating arguments when logging for the category is not enabled.
     397                 :             : 
     398                 :             : // Log by prefixing the output with the passed category name and severity level. This logs conditionally if
     399                 :             : // the category is allowed. No rate limiting is applied, because users specifying -debug are assumed to be
     400                 :             : // developers or power users who are aware that -debug may cause excessive disk usage due to logging.
     401                 :             : #define detail_LogIfCategoryAndLevelEnabled(category, level, ...)     \
     402                 :             :     do {                                                              \
     403                 :             :         if (LogAcceptCategory((category), (level))) {                 \
     404                 :             :             bool rate_limit{level >= BCLog::Level::Info};             \
     405                 :             :             Assume(!rate_limit);/*Only called with the levels below*/ \
     406                 :             :             LogPrintLevel_(category, level, rate_limit, __VA_ARGS__); \
     407                 :             :         }                                                             \
     408                 :             :     } while (0)
     409                 :             : 
     410                 :             : // Log conditionally, prefixing the output with the passed category name.
     411                 :             : #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Debug, __VA_ARGS__)
     412                 :             : #define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, BCLog::Level::Trace, __VA_ARGS__)
     413                 :             : 
     414                 :             : #endif // BITCOIN_LOGGING_H
        

Generated by: LCOV version 2.0-1