Bitcoin Core Fuzz Coverage Report

Coverage Report

Created: 2026-06-01 16:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/zip/work/bitcoin/src/logging.cpp
Line
Count
Source
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
#include <logging.h>
7
#include <memusage.h>
8
#include <util/check.h>
9
#include <util/fs.h>
10
#include <util/string.h>
11
#include <util/threadnames.h>
12
#include <util/time.h>
13
14
#include <array>
15
#include <cstring>
16
#include <map>
17
#include <optional>
18
#include <utility>
19
20
using util::Join;
21
using util::RemovePrefixView;
22
23
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
24
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
25
26
BCLog::Logger& LogInstance()
27
0
{
28
/**
29
 * NOTE: the logger instances is leaked on exit. This is ugly, but will be
30
 * cleaned up by the OS/libc. Defining a logger as a global object doesn't work
31
 * since the order of destruction of static/global objects is undefined.
32
 * Consider if the logger gets destroyed, and then some later destructor calls
33
 * LogInfo, maybe indirectly, and you get a core dump at shutdown trying to
34
 * access the logger. When the shutdown sequence is fully audited and tested,
35
 * explicit destruction of these objects can be implemented by changing this
36
 * from a raw pointer to a std::unique_ptr.
37
 * Since the ~Logger() destructor is never called, the Logger class and all
38
 * its subclasses must have implicitly-defined destructors.
39
 *
40
 * This method of initialization was originally introduced in
41
 * ee3374234c60aba2cc4c5cd5cac1c0aefc2d817c.
42
 */
43
0
    static BCLog::Logger* g_logger{new BCLog::Logger()};
44
0
    return *g_logger;
45
0
}
46
47
bool fLogIPs = DEFAULT_LOGIPS;
48
49
static int FileWriteStr(std::string_view str, FILE *fp)
50
0
{
51
0
    return fwrite(str.data(), 1, str.size(), fp);
52
0
}
53
54
bool BCLog::Logger::StartLogging()
55
0
{
56
0
    STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
57
58
0
    assert(m_buffering);
59
0
    assert(m_fileout == nullptr);
60
61
0
    if (m_print_to_file) {
62
0
        assert(!m_file_path.empty());
63
0
        m_fileout = fsbridge::fopen(m_file_path, "a");
64
0
        if (!m_fileout) {
65
0
            return false;
66
0
        }
67
68
0
        setbuf(m_fileout, nullptr); // unbuffered
69
70
        // Add newlines to the logfile to distinguish this execution from the
71
        // last one.
72
0
        FileWriteStr("\n\n\n\n\n", m_fileout);
73
0
    }
74
75
    // dump buffered messages from before we opened the log
76
0
    m_buffering = false;
77
0
    if (m_buffer_lines_discarded > 0) {
78
0
        LogPrint_({
79
0
            .category = BCLog::ALL,
80
0
            .level = Level::Info,
81
0
            .should_ratelimit = false,
82
0
            .source_loc = SourceLocation{__func__},
83
0
            .message = strprintf("Early logging buffer overflowed, %d log lines discarded.", m_buffer_lines_discarded),
Line
Count
Source
1172
0
#define strprintf tfm::format
84
0
        });
85
0
    }
86
0
    while (!m_msgs_before_open.empty()) {
87
0
        const auto& buflog = m_msgs_before_open.front();
88
0
        std::string s{Format(buflog)};
89
0
        m_msgs_before_open.pop_front();
90
91
0
        if (m_print_to_file) FileWriteStr(s, m_fileout);
92
0
        if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
93
0
        for (const auto& cb : m_print_callbacks) {
94
0
            cb(s);
95
0
        }
96
0
    }
97
0
    m_cur_buffer_memusage = 0;
98
0
    if (m_print_to_console) fflush(stdout);
99
100
0
    return true;
101
0
}
102
103
void BCLog::Logger::DisconnectTestLogger()
104
0
{
105
0
    STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
106
0
    m_buffering = true;
107
0
    if (m_fileout != nullptr) fclose(m_fileout);
108
0
    m_fileout = nullptr;
109
0
    m_print_callbacks.clear();
110
0
    m_max_buffer_memusage = DEFAULT_MAX_LOG_BUFFER;
111
0
    m_cur_buffer_memusage = 0;
112
0
    m_buffer_lines_discarded = 0;
113
0
    m_msgs_before_open.clear();
114
0
}
115
116
void BCLog::Logger::DisableLogging()
117
0
{
118
0
    {
119
0
        STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
120
0
        assert(m_buffering);
121
0
        assert(m_print_callbacks.empty());
122
0
    }
123
0
    m_print_to_file = false;
124
0
    m_print_to_console = false;
125
0
    StartLogging();
126
0
}
127
128
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
129
0
{
130
0
    m_categories |= flag;
131
0
}
132
133
bool BCLog::Logger::EnableCategory(std::string_view str)
134
0
{
135
0
    if (const auto flag{GetLogCategory(str)}) {
136
0
        if (*flag & DEPRECATED){
137
0
            LogWarning("The logging category `%s` is deprecated, can not be enabled, and will be removed in a future version", str);
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
138
            // Deprecated does not mean unsupported, which may prevent startup
139
0
            return true;
140
0
        }
141
0
        EnableCategory(*flag);
142
0
        return true;
143
0
    }
144
0
    return false;
145
0
}
146
147
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
148
0
{
149
0
    m_categories &= ~flag;
150
0
}
151
152
bool BCLog::Logger::DisableCategory(std::string_view str)
153
0
{
154
0
    if (const auto flag{GetLogCategory(str)}) {
155
0
        if (*flag & DEPRECATED){
156
0
            LogWarning("The logging category `%s` is deprecated and will be removed in a future version", str);
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
157
            // Deprecated does not mean unsupported, which may prevent startup
158
0
            return true;
159
0
        }
160
0
        DisableCategory(*flag);
161
0
        return true;
162
0
    }
163
0
    return false;
164
0
}
165
166
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
167
0
{
168
0
    return (m_categories.load(std::memory_order_relaxed) & category) != 0;
169
0
}
170
171
bool BCLog::Logger::WillLogCategoryLevel(BCLog::LogFlags category, BCLog::Level level) const
172
0
{
173
    // Log messages at Info, Warning and Error level unconditionally, so that
174
    // important troubleshooting information doesn't get lost.
175
0
    if (level >= BCLog::Level::Info) return true;
176
177
0
    if (!WillLogCategory(category)) return false;
178
179
0
    STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
180
0
    const auto it{m_category_log_levels.find(category)};
181
0
    return level >= (it == m_category_log_levels.end() ? LogLevel() : it->second);
182
0
}
183
184
bool BCLog::Logger::DefaultShrinkDebugFile() const
185
0
{
186
0
    return m_categories == BCLog::NONE;
187
0
}
188
189
static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
190
    {"net", BCLog::NET},
191
    {"tor", BCLog::TOR},
192
    {"mempool", BCLog::MEMPOOL},
193
    {"http", BCLog::HTTP},
194
    {"bench", BCLog::BENCH},
195
    {"zmq", BCLog::ZMQ},
196
    {"walletdb", BCLog::WALLETDB},
197
    {"rpc", BCLog::RPC},
198
    {"estimatefee", BCLog::ESTIMATEFEE},
199
    {"addrman", BCLog::ADDRMAN},
200
    {"selectcoins", BCLog::SELECTCOINS},
201
    {"reindex", BCLog::REINDEX},
202
    {"cmpctblock", BCLog::CMPCTBLOCK},
203
    {"rand", BCLog::RAND},
204
    {"prune", BCLog::PRUNE},
205
    {"proxy", BCLog::PROXY},
206
    {"mempoolrej", BCLog::MEMPOOLREJ},
207
    {"libevent", BCLog::LIBEVENT},
208
    {"coindb", BCLog::COINDB},
209
    {"qt", BCLog::QT},
210
    {"leveldb", BCLog::LEVELDB},
211
    {"validation", BCLog::VALIDATION},
212
    {"i2p", BCLog::I2P},
213
    {"ipc", BCLog::IPC},
214
#ifdef DEBUG_LOCKCONTENTION
215
    {"lock", BCLog::LOCK},
216
#endif
217
    {"blockstorage", BCLog::BLOCKSTORAGE},
218
    {"txreconciliation", BCLog::TXRECONCILIATION},
219
    {"scan", BCLog::SCAN},
220
    {"txpackages", BCLog::TXPACKAGES},
221
    {"kernel", BCLog::KERNEL},
222
    {"privatebroadcast", BCLog::PRIVBROADCAST},
223
};
224
225
static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
226
    // Swap keys and values from LOG_CATEGORIES_BY_STR.
227
0
    [](const auto& in) {
228
0
        std::unordered_map<BCLog::LogFlags, std::string> out;
229
0
        for (const auto& [k, v] : in) {
230
0
            const bool inserted{out.emplace(v, k).second};
231
0
            assert(inserted);
232
0
        }
233
0
        return out;
234
0
    }(LOG_CATEGORIES_BY_STR)
235
};
236
237
std::optional<BCLog::LogFlags> BCLog::Logger::GetLogCategory(std::string_view str)
238
0
{
239
0
    if (str.empty() || str == "1" || str == "all") {
240
0
        return BCLog::ALL;
241
0
    }
242
0
    auto it = LOG_CATEGORIES_BY_STR.find(str);
243
0
    if (it != LOG_CATEGORIES_BY_STR.end()) {
244
0
        return it->second;
245
0
    }
246
0
    return std::nullopt;
247
0
}
248
249
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
250
0
{
251
0
    switch (level) {
252
0
    case BCLog::Level::Trace:
253
0
        return "trace";
254
0
    case BCLog::Level::Debug:
255
0
        return "debug";
256
0
    case BCLog::Level::Info:
257
0
        return "info";
258
0
    case BCLog::Level::Warning:
259
0
        return "warning";
260
0
    case BCLog::Level::Error:
261
0
        return "error";
262
0
    }
263
0
    assert(false);
264
0
}
265
266
static std::string LogCategoryToStr(BCLog::LogFlags category)
267
0
{
268
0
    if (category == BCLog::ALL) {
269
0
        return "all";
270
0
    }
271
0
    auto it = LOG_CATEGORIES_BY_FLAG.find(category);
272
0
    assert(it != LOG_CATEGORIES_BY_FLAG.end());
273
0
    return it->second;
274
0
}
275
276
static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str)
277
0
{
278
0
    if (level_str == "trace") {
279
0
        return BCLog::Level::Trace;
280
0
    } else if (level_str == "debug") {
281
0
        return BCLog::Level::Debug;
282
0
    } else if (level_str == "info") {
283
0
        return BCLog::Level::Info;
284
0
    } else if (level_str == "warning") {
285
0
        return BCLog::Level::Warning;
286
0
    } else if (level_str == "error") {
287
0
        return BCLog::Level::Error;
288
0
    } else {
289
0
        return std::nullopt;
290
0
    }
291
0
}
292
293
std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
294
0
{
295
0
    std::vector<LogCategory> ret;
296
0
    ret.reserve(LOG_CATEGORIES_BY_STR.size());
297
0
    for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) {
298
0
        ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
299
0
    }
300
0
    return ret;
301
0
}
302
303
/** Log severity levels that can be selected by the user. */
304
static constexpr std::array<BCLog::Level, 3> LogLevelsList()
305
0
{
306
0
    return {BCLog::Level::Info, BCLog::Level::Debug, BCLog::Level::Trace};
307
0
}
308
309
std::string BCLog::Logger::LogLevelsString() const
310
0
{
311
0
    const auto& levels = LogLevelsList();
312
0
    return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [](BCLog::Level level) { return LogLevelToStr(level); });
313
0
}
314
315
std::string BCLog::Logger::LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
316
0
{
317
0
    std::string strStamped;
318
319
0
    if (!m_log_timestamps)
320
0
        return strStamped;
321
322
0
    const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
323
0
    strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
324
0
    if (m_log_time_micros && !strStamped.empty()) {
325
0
        strStamped.pop_back();
326
0
        strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
Line
Count
Source
1172
0
#define strprintf tfm::format
327
0
    }
328
0
    if (mocktime > 0s) {
329
0
        strStamped += " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) + ")";
330
0
    }
331
0
    strStamped += ' ';
332
333
0
    return strStamped;
334
0
}
335
336
namespace BCLog {
337
    /** Belts and suspenders: make sure outgoing log messages don't contain
338
     * potentially suspicious characters, such as terminal control codes.
339
     *
340
     * This escapes control characters except newline ('\n') in C syntax.
341
     * It escapes instead of removes them to still allow for troubleshooting
342
     * issues where they accidentally end up in strings.
343
     */
344
0
    std::string LogEscapeMessage(std::string_view str) {
345
0
        std::string ret;
346
0
        for (char ch_in : str) {
347
0
            uint8_t ch = (uint8_t)ch_in;
348
0
            if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
349
0
                ret += ch_in;
350
0
            } else {
351
0
                ret += strprintf("\\x%02x", ch);
Line
Count
Source
1172
0
#define strprintf tfm::format
352
0
            }
353
0
        }
354
0
        return ret;
355
0
    }
356
} // namespace BCLog
357
358
std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level level) const
359
0
{
360
0
    if (category == LogFlags::NONE) category = LogFlags::ALL;
361
362
0
    const bool has_category{m_always_print_category_level || category != LogFlags::ALL};
363
364
    // If there is no category, Info is implied
365
0
    if (!has_category && level == Level::Info) return {};
366
367
0
    std::string s{"["};
368
0
    if (has_category) {
369
0
        s += LogCategoryToStr(category);
370
0
    }
371
372
0
    if (m_always_print_category_level || !has_category || level != Level::Debug) {
373
        // If there is a category, Debug is implied, so don't add the level
374
375
        // Only add separator if we have a category
376
0
        if (has_category) s += ":";
377
0
        s += Logger::LogLevelToStr(level);
378
0
    }
379
380
0
    s += "] ";
381
0
    return s;
382
0
}
383
384
static size_t MemUsage(const util::log::Entry& log)
385
0
{
386
0
    return memusage::DynamicUsage(log.message) +
387
0
           memusage::DynamicUsage(log.thread_name) +
388
0
           memusage::MallocUsage(sizeof(memusage::list_node<util::log::Entry>));
389
0
}
390
391
BCLog::LogRateLimiter::LogRateLimiter(uint64_t max_bytes, std::chrono::seconds reset_window)
392
0
    : m_max_bytes{max_bytes}, m_reset_window{reset_window} {}
393
394
std::shared_ptr<BCLog::LogRateLimiter> BCLog::LogRateLimiter::Create(
395
    SchedulerFunction&& scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window)
396
0
{
397
0
    auto limiter{std::shared_ptr<LogRateLimiter>(new LogRateLimiter(max_bytes, reset_window))};
398
0
    std::weak_ptr<LogRateLimiter> weak_limiter{limiter};
399
0
    auto reset = [weak_limiter] {
400
0
        if (auto shared_limiter{weak_limiter.lock()}) shared_limiter->Reset();
401
0
    };
402
0
    scheduler_func(reset, limiter->m_reset_window);
403
0
    return limiter;
404
0
}
405
406
BCLog::LogRateLimiter::Status BCLog::LogRateLimiter::Consume(
407
    const SourceLocation& source_loc,
408
    const std::string& str)
409
0
{
410
0
    STDLOCK(m_mutex);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
411
0
    auto& stats{m_source_locations.try_emplace(source_loc, m_max_bytes).first->second};
412
0
    Status status{stats.m_dropped_bytes > 0 ? Status::STILL_SUPPRESSED : Status::UNSUPPRESSED};
413
414
0
    if (!stats.Consume(str.size()) && status == Status::UNSUPPRESSED) {
415
0
        status = Status::NEWLY_SUPPRESSED;
416
0
        m_suppression_active = true;
417
0
    }
418
419
0
    return status;
420
0
}
421
422
std::string BCLog::Logger::Format(const util::log::Entry& entry) const
423
0
{
424
0
    std::string result{LogTimestampStr(entry.timestamp, entry.mocktime)};
425
426
0
    if (m_log_threadnames) {
427
0
        result += strprintf("[%s] ", (entry.thread_name.empty() ? "unknown" : entry.thread_name));
Line
Count
Source
1172
0
#define strprintf tfm::format
428
0
    }
429
430
0
    if (m_log_sourcelocations) {
431
0
        result += strprintf("[%s:%d] [%s] ", RemovePrefixView(entry.source_loc.file_name(), "./"), entry.source_loc.line(), entry.source_loc.function_name_short());
Line
Count
Source
1172
0
#define strprintf tfm::format
432
0
    }
433
434
0
    result += GetLogPrefix(static_cast<LogFlags>(entry.category), entry.level);
435
0
    result += LogEscapeMessage(entry.message);
436
437
0
    if (!result.ends_with('\n')) result += '\n';
438
0
    return result;
439
0
}
440
441
void BCLog::Logger::LogPrint(util::log::Entry entry)
442
0
{
443
0
    STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
444
0
    return LogPrint_(std::move(entry));
445
0
}
446
447
// NOLINTNEXTLINE(misc-no-recursion)
448
void BCLog::Logger::LogPrint_(util::log::Entry entry)
449
0
{
450
0
    if (m_buffering) {
451
0
        {
452
0
            m_cur_buffer_memusage += MemUsage(entry);
453
0
            m_msgs_before_open.push_back(std::move(entry));
454
0
        }
455
456
0
        while (m_cur_buffer_memusage > m_max_buffer_memusage) {
457
0
            if (m_msgs_before_open.empty()) {
458
0
                m_cur_buffer_memusage = 0;
459
0
                break;
460
0
            }
461
0
            m_cur_buffer_memusage -= MemUsage(m_msgs_before_open.front());
462
0
            m_msgs_before_open.pop_front();
463
0
            ++m_buffer_lines_discarded;
464
0
        }
465
466
0
        return;
467
0
    }
468
469
0
    std::string str_prefixed{Format(entry)};
470
0
    bool ratelimit{false};
471
0
    if (entry.should_ratelimit && m_limiter) {
472
0
        auto status{m_limiter->Consume(entry.source_loc, str_prefixed)};
473
0
        if (status == LogRateLimiter::Status::NEWLY_SUPPRESSED) {
474
            // NOLINTNEXTLINE(misc-no-recursion)
475
0
            LogPrint_({
476
0
                .category = LogFlags::ALL,
477
0
                .level = Level::Warning,
478
0
                .should_ratelimit = false, // with should_ratelimit=false, this cannot lead to infinite recursion
479
0
                .source_loc = SourceLocation{__func__},
480
0
                .message = strprintf(
Line
Count
Source
1172
0
#define strprintf tfm::format
481
0
                    "Excessive logging detected from %s:%d (%s): >%d bytes logged during "
482
0
                    "the last time window of %is. Suppressing logging to disk from this "
483
0
                    "source location until time window resets. Console logging "
484
0
                    "unaffected. Last log entry.",
485
0
                    entry.source_loc.file_name(), entry.source_loc.line(), entry.source_loc.function_name_short(),
486
0
                    m_limiter->m_max_bytes,
487
0
                    Ticks<std::chrono::seconds>(m_limiter->m_reset_window)),
488
0
            });
489
0
        } else if (status == LogRateLimiter::Status::STILL_SUPPRESSED) {
490
0
            ratelimit = true;
491
0
        }
492
0
    }
493
494
    // To avoid confusion caused by dropped log messages when debugging an issue,
495
    // we prefix log lines with "[*]" when there are any suppressed source locations.
496
0
    if (m_limiter && m_limiter->SuppressionsActive()) {
497
0
        str_prefixed.insert(0, "[*] ");
498
0
    }
499
500
0
    if (m_print_to_console) {
501
        // print to console
502
0
        fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
503
0
        fflush(stdout);
504
0
    }
505
0
    for (const auto& cb : m_print_callbacks) {
506
0
        cb(str_prefixed);
507
0
    }
508
0
    if (m_print_to_file && !ratelimit) {
509
0
        assert(m_fileout != nullptr);
510
511
        // reopen the log file, if requested
512
0
        if (m_reopen_file) {
513
0
            m_reopen_file = false;
514
0
            FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
515
0
            if (new_fileout) {
516
0
                setbuf(new_fileout, nullptr); // unbuffered
517
0
                fclose(m_fileout);
518
0
                m_fileout = new_fileout;
519
0
            }
520
0
        }
521
0
        FileWriteStr(str_prefixed, m_fileout);
522
0
    }
523
0
}
524
525
void BCLog::Logger::ShrinkDebugFile()
526
0
{
527
0
    STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
528
529
    // Amount of debug.log to save at end when shrinking (must fit in memory)
530
0
    constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
531
532
0
    assert(!m_file_path.empty());
533
534
    // Scroll debug.log if it's getting too big
535
0
    FILE* file = fsbridge::fopen(m_file_path, "r");
536
537
    // Special files (e.g. device nodes) may not have a size.
538
0
    size_t log_size = 0;
539
0
    try {
540
0
        log_size = fs::file_size(m_file_path);
541
0
    } catch (const fs::filesystem_error&) {}
542
543
    // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
544
    // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
545
0
    if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
546
0
    {
547
        // Restart the file with some of the end
548
0
        std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
549
0
        if (fseek(file, -((long)vch.size()), SEEK_END)) {
550
            // LogWarning, except with m_cs held
551
0
            LogPrint_({
552
0
                .category = BCLog::ALL,
553
0
                .level = Level::Warning,
554
0
                .should_ratelimit = true,
555
0
                .source_loc = SourceLocation{__func__},
556
0
                .message = "Failed to shrink debug log file: fseek(...) failed",
557
0
            });
558
0
            fclose(file);
559
0
            return;
560
0
        }
561
0
        int nBytes = fread(vch.data(), 1, vch.size(), file);
562
0
        fclose(file);
563
564
0
        file = fsbridge::fopen(m_file_path, "w");
565
0
        if (file)
566
0
        {
567
0
            fwrite(vch.data(), 1, nBytes, file);
568
0
            fclose(file);
569
0
        }
570
0
    }
571
0
    else if (file != nullptr)
572
0
        fclose(file);
573
0
}
574
575
void BCLog::LogRateLimiter::Reset()
576
0
{
577
0
    decltype(m_source_locations) source_locations;
578
0
    {
579
0
        STDLOCK(m_mutex);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
580
0
        source_locations.swap(m_source_locations);
581
0
        m_suppression_active = false;
582
0
    }
583
0
    for (const auto& [source_loc, stats] : source_locations) {
584
0
        if (stats.m_dropped_bytes == 0) continue;
585
0
        LogWarning(util::log::NO_RATE_LIMIT,
Line
Count
Source
126
0
#define LogWarning(...) detail_LogWithSrcLoc(BCLog::LogFlags::ALL, util::log::Level::Warning, __VA_ARGS__)
Line
Count
Source
119
0
#define detail_LogWithSrcLoc(category, level, ...) util::log::LogPrintFormatInternal(SourceLocation{__func__}, category, level, __VA_ARGS__)
586
0
            "Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.",
587
0
            source_loc.file_name(), source_loc.line(), source_loc.function_name_short(),
588
0
            stats.m_dropped_bytes, Ticks<std::chrono::seconds>(m_reset_window));
589
0
    }
590
0
}
591
592
bool BCLog::LogRateLimiter::Stats::Consume(uint64_t bytes)
593
0
{
594
0
    if (bytes > m_available_bytes) {
595
0
        m_dropped_bytes += bytes;
596
0
        m_available_bytes = 0;
597
0
        return false;
598
0
    }
599
600
0
    m_available_bytes -= bytes;
601
0
    return true;
602
0
}
603
604
bool BCLog::Logger::SetLogLevel(std::string_view level_str)
605
0
{
606
0
    const auto level = GetLogLevel(level_str);
607
0
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
608
0
    m_log_level = level.value();
609
0
    return true;
610
0
}
611
612
bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
613
0
{
614
0
    const auto flag{GetLogCategory(category_str)};
615
0
    if (!flag) return false;
616
617
0
    const auto level = GetLogLevel(level_str);
618
0
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
619
620
0
    STDLOCK(m_cs);
Line
Count
Source
41
0
#define STDLOCK(cs) StdMutex::Guard UNIQUE_NAME(criticalblock){StdMutex::CheckNotHeld(cs)}
Line
Count
Source
11
0
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
Line
Count
Source
9
0
#define PASTE2(x, y) PASTE(x, y)
Line
Count
Source
8
0
#define PASTE(x, y) x ## y
621
0
    m_category_log_levels[*flag] = level.value();
622
0
    return true;
623
0
}
624
625
bool util::log::ShouldDebugLog(Category category)
626
0
{
627
0
    return LogInstance().WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), util::log::Level::Debug);
628
0
}
629
630
bool util::log::ShouldTraceLog(Category category)
631
0
{
632
0
    return LogInstance().WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), util::log::Level::Trace);
633
0
}
634
635
void util::log::Log(util::log::Entry entry)
636
0
{
637
0
    BCLog::Logger& logger{LogInstance()};
638
0
    if (logger.Enabled()) {
639
0
        logger.LogPrint(std::move(entry));
640
0
    }
641
0
}