logging.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifndef BITCOIN_LOGGING_H
  6. #define BITCOIN_LOGGING_H
  7. #include <fs.h>
  8. #include <tinyformat.h>
  9. #include <atomic>
  10. #include <cstdint>
  11. #include <list>
  12. #include <mutex>
  13. #include <string>
  14. #include <vector>
  15. static const bool DEFAULT_LOGTIMEMICROS = false;
  16. static const bool DEFAULT_LOGIPS = false;
  17. static const bool DEFAULT_LOGTIMESTAMPS = true;
  18. extern const char * const DEFAULT_DEBUGLOGFILE;
  19. extern bool fLogIPs;
  20. struct CLogCategoryActive
  21. {
  22. std::string category;
  23. bool active;
  24. };
  25. namespace BCLog {
  26. enum LogFlags : uint32_t {
  27. NONE = 0,
  28. NET = (1 << 0),
  29. TOR = (1 << 1),
  30. MEMPOOL = (1 << 2),
  31. HTTP = (1 << 3),
  32. BENCH = (1 << 4),
  33. ZMQ = (1 << 5),
  34. DB = (1 << 6),
  35. RPC = (1 << 7),
  36. ESTIMATEFEE = (1 << 8),
  37. ADDRMAN = (1 << 9),
  38. SELECTCOINS = (1 << 10),
  39. REINDEX = (1 << 11),
  40. CMPCTBLOCK = (1 << 12),
  41. RAND = (1 << 13),
  42. PRUNE = (1 << 14),
  43. PROXY = (1 << 15),
  44. MEMPOOLREJ = (1 << 16),
  45. LIBEVENT = (1 << 17),
  46. COINDB = (1 << 18),
  47. QT = (1 << 19),
  48. LEVELDB = (1 << 20),
  49. ALL = ~(uint32_t)0,
  50. };
  51. class Logger
  52. {
  53. private:
  54. FILE* m_fileout = nullptr;
  55. std::mutex m_file_mutex;
  56. std::list<std::string> m_msgs_before_open;
  57. /**
  58. * m_started_new_line is a state variable that will suppress printing of
  59. * the timestamp when multiple calls are made that don't end in a
  60. * newline.
  61. */
  62. std::atomic_bool m_started_new_line{true};
  63. /** Log categories bitfield. */
  64. std::atomic<uint32_t> m_categories{0};
  65. std::string LogTimestampStr(const std::string& str);
  66. public:
  67. bool m_print_to_console = false;
  68. bool m_print_to_file = false;
  69. bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
  70. bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
  71. fs::path m_file_path;
  72. std::atomic<bool> m_reopen_file{false};
  73. /** Send a string to the log output */
  74. void LogPrintStr(const std::string &str);
  75. /** Returns whether logs will be written to any output */
  76. bool Enabled() const { return m_print_to_console || m_print_to_file; }
  77. bool OpenDebugLog();
  78. void ShrinkDebugFile();
  79. uint32_t GetCategoryMask() const { return m_categories.load(); }
  80. void EnableCategory(LogFlags flag);
  81. bool EnableCategory(const std::string& str);
  82. void DisableCategory(LogFlags flag);
  83. bool DisableCategory(const std::string& str);
  84. bool WillLogCategory(LogFlags category) const;
  85. bool DefaultShrinkDebugFile() const;
  86. };
  87. } // namespace BCLog
  88. extern BCLog::Logger* const g_logger;
  89. /** Return true if log accepts specified category */
  90. static inline bool LogAcceptCategory(BCLog::LogFlags category)
  91. {
  92. return g_logger->WillLogCategory(category);
  93. }
  94. /** Returns a string with the log categories. */
  95. std::string ListLogCategories();
  96. /** Returns a vector of the active log categories. */
  97. std::vector<CLogCategoryActive> ListActiveLogCategories();
  98. /** Return true if str parses as a log category and set the flag */
  99. bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
  100. /** Get format string from VA_ARGS for error reporting */
  101. template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
  102. static inline void MarkUsed() {}
  103. template<typename T, typename... Args> static inline void MarkUsed(const T& t, const Args&... args)
  104. {
  105. (void)t;
  106. MarkUsed(args...);
  107. }
  108. // Be conservative when using LogPrintf/error or other things which
  109. // unconditionally log to debug.log! It should not be the case that an inbound
  110. // peer can fill up a user's disk with debug.log entries.
  111. #ifdef USE_COVERAGE
  112. #define LogPrintf(...) do { MarkUsed(__VA_ARGS__); } while(0)
  113. #define LogPrint(category, ...) do { MarkUsed(__VA_ARGS__); } while(0)
  114. #else
  115. #define LogPrintf(...) do { \
  116. if (g_logger->Enabled()) { \
  117. std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
  118. try { \
  119. _log_msg_ = tfm::format(__VA_ARGS__); \
  120. } catch (tinyformat::format_error &fmterr) { \
  121. /* Original format string will have newline so don't add one here */ \
  122. _log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
  123. } \
  124. g_logger->LogPrintStr(_log_msg_); \
  125. } \
  126. } while(0)
  127. #define LogPrint(category, ...) do { \
  128. if (LogAcceptCategory((category))) { \
  129. LogPrintf(__VA_ARGS__); \
  130. } \
  131. } while(0)
  132. #endif
  133. #endif // BITCOIN_LOGGING_H