Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_UTIL_LOG_H
6 : : #define BITCOIN_UTIL_LOG_H
7 : :
8 : : // This header works in tandem with `logging/categories.h`
9 : : // to expose the complete logging interface.
10 : : #include <logging/categories.h> // IWYU pragma: export
11 : : #include <tinyformat.h>
12 : : #include <util/check.h>
13 : : #include <util/threadnames.h>
14 : : #include <util/time.h>
15 : :
16 : : #include <atomic>
17 : : #include <cstdint>
18 : : #include <source_location>
19 : : #include <string>
20 : : #include <string_view>
21 : :
22 : : /// Like std::source_location, but allowing to override the function name.
23 : : class SourceLocation
24 : : {
25 : : public:
26 : : /// The func argument must be constructed from the C++11 __func__ macro.
27 : : /// Ref: https://en.cppreference.com/w/cpp/language/function.html#func
28 : : /// Non-static string literals are not supported.
29 : 371212 : explicit SourceLocation(
30 : : const char* func,
31 : : std::source_location loc = std::source_location::current())
32 [ + - ][ + - : 371218 : : m_func{func}, m_loc{loc} {}
+ - + - +
- ][ - - +
- - - - -
+ - + - +
- + - + -
+ - + - +
- + - ][ +
- - - + -
+ - - - -
- + - - -
- - - - +
- + - - -
- - - - +
- - - + -
- - - - -
- - - - -
+ - + - -
- - - - -
+ - - - +
- - - - -
- - - - -
- ][ - - -
- - - + -
+ - - - -
- - - - -
- - - - -
- - - - -
+ - ][ + -
+ - + - +
- - - - -
+ - + - -
- + - + -
- - - - -
- + - + -
+ - - - ]
[ + - - -
- - - - -
- + - - -
- - + - +
- - - - -
- - + - -
- - - + -
- - - - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - ][ -
- + - + -
+ - - - -
- - - - -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
[ + - - -
- - - - -
- - - + -
- - + - +
- - - + -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
- - - - -
- - - - -
+ - + - +
- - - - -
- - + - -
- - - + -
- - + - +
- - - - -
- - + - -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - -
- - - ][ +
- + - + -
# # ][ - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- + - - -
- - + - +
- - - - -
+ - - - -
- - - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - - - -
- + - ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- + - + -
- - + - -
- - - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - - -
+ - - - -
- - - + -
- - ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - - -
- - - - -
- - - - -
+ - + - +
- + - - -
- - - - +
- - - - -
+ - - - -
- + - - -
- - - - -
- - - - -
- - - - +
- + - ]
[ + - + - ]
[ + - + -
+ - + - +
- + - ][ +
- + - + -
+ - + - +
- + - + -
+ - + + ]
[ + - + -
+ - + - +
- + - + -
+ - ][ - -
- - + - +
- + - - -
+ - - - -
- + - - -
+ - + - +
- + - + -
+ - + - -
- + - + -
+ - + - +
- + - - -
- - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - +
- - - + -
- - - - -
- + - - -
- - - - -
- - - + -
+ - - - -
- - - + -
+ - + - +
- - - - -
+ - + - +
- - - - -
- - + - -
- - - - -
+ - - - +
- + - + -
+ - + - -
- - - - -
+ - + - +
- + - + -
+ - + - -
- + - - -
- - ][ - -
- - + - +
- - - - -
- - - - -
- + - + -
# # # # ]
[ + - + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
33 : :
34 [ + - ]: 370458 : std::string_view file_name() const { return m_loc.file_name(); }
35 [ + - + - : 371065 : std::uint_least32_t line() const { return m_loc.line(); }
+ - + - +
- + - +
+ ][ + - ]
36 [ + - + - : 370112 : std::string_view function_name_short() const { return m_func; }
+ - ][ + - ]
37 : :
38 : : private:
39 : : std::string_view m_func;
40 : : std::source_location m_loc;
41 : : };
42 : :
43 : : namespace util::log {
44 : : /** Opaque to util::log; interpreted by consumers (e.g., BCLog::LogFlags). */
45 : : using Category = uint64_t;
46 : :
47 : : //! Structure and constant for tagging not to rate limit.
48 : : struct NoRateLimitTag {
49 : : explicit NoRateLimitTag() = default;
50 : : };
51 : : inline constexpr NoRateLimitTag NO_RATE_LIMIT{};
52 : :
53 : : enum class Level {
54 : : Trace = 0, // High-volume or detailed logging for development/debugging
55 : : Debug, // Reasonably noisy logging, but still usable in production
56 : : Info, // Default
57 : : Warning,
58 : : Error,
59 : : };
60 : :
61 [ - - ]: 1114234 : struct Entry {
62 : : Category category;
63 : : Level level;
64 : : bool should_ratelimit{false}; //!< Hint for consumers if this entry should be ratelimited
65 : : SystemClock::time_point timestamp{SystemClock::now()};
66 : : std::chrono::seconds mocktime{GetMockTime()};
67 : : std::string thread_name{util::ThreadGetInternalName()};
68 : : SourceLocation source_loc;
69 : : std::string message;
70 : : };
71 : :
72 : : /// Return whether messages with specified category should be debug logged.
73 : : /// Applications using the logging library need to provide this.
74 : : bool ShouldDebugLog(Category category);
75 : :
76 : : /// Return whether messages with specified category should be trace logged.
77 : : /// Applications using the logging library need to provide this.
78 : : bool ShouldTraceLog(Category category);
79 : :
80 : : /** Send message to be logged. Applications using the logging library need to provide this. */
81 : : void Log(Entry entry);
82 : :
83 : : template <typename... Args>
84 [ + - ]: 371219 : inline void LogPrintFormatInternal_(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, bool should_ratelimit, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
85 : : {
86 [ + - ]: 371219 : std::string log_msg;
87 : : try {
88 [ + - ]: 371219 : log_msg = tfm::format(fmt, args...);
89 [ - - ]: 0 : } catch (tinyformat::format_error& fmterr) {
90 [ - - - - : 0 : log_msg = "Error \"" + std::string{fmterr.what()} + "\" while formatting log message: " + fmt.fmt;
- - ]
91 : : }
92 [ + - + - ]: 1113657 : util::log::Log(util::log::Entry{
93 : : .category = flag,
94 : : .level = level,
95 : : .should_ratelimit = should_ratelimit,
96 : : .source_loc = std::move(source_loc),
97 : : .message = std::move(log_msg)});
98 [ + - ]: 1113657 : }
99 : :
100 : : template <typename... Args>
101 : 11766 : inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
102 : : {
103 [ + - ][ + - : 11759 : return LogPrintFormatInternal_(std::move(source_loc), flag, level, /*should_ratelimit=*/true, fmt, args...);
+ - + - ]
[ + - + -
- - - - -
- - - + -
+ - - - -
- + - - -
- - - - +
- + - - -
- - ][ - -
- - - - -
- # # # #
# # # # ]
[ - - - -
- - + - -
- + - - -
+ - # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - -
- - - + -
- - - - -
- + - - -
+ - - - ]
[ + - + -
+ - + - +
- - - - -
+ - + - ]
[ - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - - -
- - - - -
- - - + -
+ - + - -
- - - - -
+ - - - -
- + - - -
+ - + - -
- - - - -
+ - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - - - ]
[ - - - -
- - - - -
- + - + -
- - + - -
- - - - -
- - - - -
- - - -
- ][ + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
[ + - + - ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - ][ + -
+ - + - +
- + - +
- ][ - - +
- + - + -
- - + - -
- - - + -
- - + - +
- + - + -
+ - + - +
- - - + -
+ - + - +
- + - + -
- - - - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- + - - -
+ - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
+ - - - +
- - - - -
- - + - +
- + - + -
- - - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
[ + - - -
+ - + - -
- - - + -
- - - - -
- + - + -
- - - - -
- + - - -
+ - - - -
- - - - -
- - + - -
- - - - -
+ - - - +
- - - - -
- - - - ]
[ + - + -
- - - - +
- + - -
- ][ - - -
- + - + -
- - - - -
- - - - -
- - - - -
- ]
104 : : }
105 : :
106 : : template <typename... Args>
107 : 359453 : inline void LogPrintFormatInternal(SourceLocation&& source_loc, BCLog::LogFlags flag, util::log::Level level, util::log::NoRateLimitTag, util::ConstevalFormatString<sizeof...(Args)> fmt, const Args&... args)
108 : : {
109 [ + - ]: 359453 : return LogPrintFormatInternal_(std::move(source_loc), flag, level, /*should_ratelimit=*/false, fmt, args...);
[ + - + - ]
[ - - + -
+ - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - + -
- - ][ - -
- - + - +
- - - - -
- - - - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ][ + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - + - +
- + - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
+ - + - +
- - - - -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - - -
- - - - -
- - - - -
- - - - -
- - - ][ -
- - - - -
+ - + - +
- + - - -
- - - - +
- - - - -
+ - - - -
- + - - -
- - + - ]
[ + - + -
+ - ][ + -
+ - + - +
- - - + -
+ - + - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- ][ - - -
- - - - -
- - - - +
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - - - +
- + - + -
+ - + - +
- + - + -
+ - ][ + -
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
- - - - #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - +
- + - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - +
- ]
110 : : }
111 : : } // namespace util::log
112 : :
113 : : namespace BCLog {
114 : : //! Alias for compatibility. Prefer util::log::Level over BCLog::Level in new code.
115 : : using Level = util::log::Level;
116 : : } // namespace BCLog
117 : :
118 : : // Allow __func__ to be used in any context without warnings:
119 : : // NOLINTNEXTLINE(bugprone-lambda-function-name)
120 : : #define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
121 : :
122 : : // Log unconditionally. Uses basic rate limiting to mitigate disk filling attacks.
123 : : // Be conservative when using functions that unconditionally log to debug.log!
124 : : // It should not be the case that an inbound peer can fill up a user's storage
125 : : // with debug.log entries.
126 : : #define LogInfo(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Info, __VA_ARGS__)
127 : : #define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
128 : : #define LogError(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Error, __VA_ARGS__)
129 : :
130 : : // Use a macro instead of a function for conditional logging to prevent
131 : : // evaluating arguments when logging for the category is not enabled.
132 : :
133 : : // Log by prefixing the output with the passed category name and severity level. This logs conditionally if
134 : : // the category is allowed. No rate limiting is applied, because users specifying -debug are assumed to be
135 : : // developers or power users who are aware that -debug may cause excessive disk usage due to logging.
136 : : #define detail_LogIfCategoryAndLevelEnabled(category, shouldlog, level, ...) \
137 : : do { \
138 : : if (shouldlog(category)) { \
139 : : detail_LogWithSrcLoc((category), (level), util::log::NO_RATE_LIMIT, __VA_ARGS__); \
140 : : } \
141 : : } while (0)
142 : :
143 : : // Log conditionally, prefixing the output with the passed category name.
144 : : #define LogDebug(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldDebugLog, util::log::Level::Debug, __VA_ARGS__)
145 : : #define LogTrace(category, ...) detail_LogIfCategoryAndLevelEnabled(category, util::log::ShouldTraceLog, util::log::Level::Trace, __VA_ARGS__)
146 : :
147 : : // Log unconditionally the first time this statement is hit, then conditionally afterwards
148 : : #define LogWarnThenDebug(category, ...) \
149 : : do { \
150 : : static std::atomic<bool> _warned{false}; \
151 : : if (!_warned.exchange(true)) { \
152 : : LogWarning(__VA_ARGS__); \
153 : : } else { \
154 : : LogDebug(category, __VA_ARGS__); \
155 : : } \
156 : : } while (0)
157 : :
158 : : #endif // BITCOIN_UTIL_LOG_H
|