Coverage Report

Created: 2024-08-21 05:08

/workdir/bitcoin/src/logging.cpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-2022 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/fs.h>
9
#include <util/string.h>
10
#include <util/threadnames.h>
11
#include <util/time.h>
12
13
#include <array>
14
#include <map>
15
#include <optional>
16
17
using util::Join;
18
using util::RemovePrefixView;
19
20
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
21
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
22
23
BCLog::Logger& LogInstance()
24
59.5k
{
25
/**
26
 * NOTE: the logger instances is leaked on exit. This is ugly, but will be
27
 * cleaned up by the OS/libc. Defining a logger as a global object doesn't work
28
 * since the order of destruction of static/global objects is undefined.
29
 * Consider if the logger gets destroyed, and then some later destructor calls
30
 * LogPrintf, maybe indirectly, and you get a core dump at shutdown trying to
31
 * access the logger. When the shutdown sequence is fully audited and tested,
32
 * explicit destruction of these objects can be implemented by changing this
33
 * from a raw pointer to a std::unique_ptr.
34
 * Since the ~Logger() destructor is never called, the Logger class and all
35
 * its subclasses must have implicitly-defined destructors.
36
 *
37
 * This method of initialization was originally introduced in
38
 * ee3374234c60aba2cc4c5cd5cac1c0aefc2d817c.
39
 */
40
59.5k
    static BCLog::Logger* g_logger{new BCLog::Logger()};
41
59.5k
    return *g_logger;
42
59.5k
}
43
44
bool fLogIPs = DEFAULT_LOGIPS;
45
46
static int FileWriteStr(std::string_view str, FILE *fp)
47
0
{
48
0
    return fwrite(str.data(), 1, str.size(), fp);
49
0
}
50
51
bool BCLog::Logger::StartLogging()
52
0
{
53
0
    StdLockGuard scoped_lock(m_cs);
54
55
0
    assert(m_buffering);
56
0
    assert(m_fileout == nullptr);
57
58
0
    if (m_print_to_file) {
  Branch (58:9): [True: 0, False: 0]
59
0
        assert(!m_file_path.empty());
60
0
        m_fileout = fsbridge::fopen(m_file_path, "a");
61
0
        if (!m_fileout) {
  Branch (61:13): [True: 0, False: 0]
62
0
            return false;
63
0
        }
64
65
0
        setbuf(m_fileout, nullptr); // unbuffered
66
67
        // Add newlines to the logfile to distinguish this execution from the
68
        // last one.
69
0
        FileWriteStr("\n\n\n\n\n", m_fileout);
70
0
    }
71
72
    // dump buffered messages from before we opened the log
73
0
    m_buffering = false;
74
0
    if (m_buffer_lines_discarded > 0) {
  Branch (74:9): [True: 0, False: 0]
75
0
        LogPrintStr_(strprintf("Early logging buffer overflowed, %d log lines discarded.\n", m_buffer_lines_discarded), __func__, __FILE__, __LINE__, BCLog::ALL, Level::Info);
76
0
    }
77
0
    while (!m_msgs_before_open.empty()) {
  Branch (77:12): [True: 0, False: 0]
78
0
        const auto& buflog = m_msgs_before_open.front();
79
0
        std::string s{buflog.str};
80
0
        FormatLogStrInPlace(s, buflog.category, buflog.level, buflog.source_file, buflog.source_line, buflog.logging_function, buflog.threadname, buflog.now, buflog.mocktime);
81
0
        m_msgs_before_open.pop_front();
82
83
0
        if (m_print_to_file) FileWriteStr(s, m_fileout);
  Branch (83:13): [True: 0, False: 0]
84
0
        if (m_print_to_console) fwrite(s.data(), 1, s.size(), stdout);
  Branch (84:13): [True: 0, False: 0]
85
0
        for (const auto& cb : m_print_callbacks) {
  Branch (85:29): [True: 0, False: 0]
86
0
            cb(s);
87
0
        }
88
0
    }
89
0
    m_cur_buffer_memusage = 0;
90
0
    if (m_print_to_console) fflush(stdout);
  Branch (90:9): [True: 0, False: 0]
91
92
0
    return true;
93
0
}
94
95
void BCLog::Logger::DisconnectTestLogger()
96
1
{
97
1
    StdLockGuard scoped_lock(m_cs);
98
1
    m_buffering = true;
99
1
    if (m_fileout != nullptr) fclose(m_fileout);
  Branch (99:9): [True: 0, False: 1]
100
1
    m_fileout = nullptr;
101
1
    m_print_callbacks.clear();
102
1
    m_max_buffer_memusage = DEFAULT_MAX_LOG_BUFFER;
103
1
    m_cur_buffer_memusage = 0;
104
1
    m_buffer_lines_discarded = 0;
105
1
    m_msgs_before_open.clear();
106
107
1
}
108
109
void BCLog::Logger::DisableLogging()
110
0
{
111
0
    {
112
0
        StdLockGuard scoped_lock(m_cs);
113
0
        assert(m_buffering);
114
0
        assert(m_print_callbacks.empty());
115
0
    }
116
0
    m_print_to_file = false;
117
0
    m_print_to_console = false;
118
0
    StartLogging();
119
0
}
120
121
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
122
0
{
123
0
    m_categories |= flag;
124
0
}
125
126
bool BCLog::Logger::EnableCategory(std::string_view str)
127
0
{
128
0
    BCLog::LogFlags flag;
129
0
    if (!GetLogCategory(flag, str)) return false;
  Branch (129:9): [True: 0, False: 0]
130
0
    EnableCategory(flag);
131
0
    return true;
132
0
}
133
134
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
135
0
{
136
0
    m_categories &= ~flag;
137
0
}
138
139
bool BCLog::Logger::DisableCategory(std::string_view str)
140
0
{
141
0
    BCLog::LogFlags flag;
142
0
    if (!GetLogCategory(flag, str)) return false;
  Branch (142:9): [True: 0, False: 0]
143
0
    DisableCategory(flag);
144
0
    return true;
145
0
}
146
147
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
148
51.5k
{
149
51.5k
    return (m_categories.load(std::memory_order_relaxed) & category) != 0;
150
51.5k
}
151
152
bool BCLog::Logger::WillLogCategoryLevel(BCLog::LogFlags category, BCLog::Level level) const
153
51.5k
{
154
    // Log messages at Info, Warning and Error level unconditionally, so that
155
    // important troubleshooting information doesn't get lost.
156
51.5k
    if (level >= BCLog::Level::Info) return true;
  Branch (156:9): [True: 0, False: 51.5k]
157
158
51.5k
    if (!WillLogCategory(category)) return false;
  Branch (158:9): [True: 51.5k, False: 0]
159
160
0
    StdLockGuard scoped_lock(m_cs);
161
0
    const auto it{m_category_log_levels.find(category)};
162
0
    return level >= (it == m_category_log_levels.end() ? LogLevel() : it->second);
  Branch (162:22): [True: 0, False: 0]
163
51.5k
}
164
165
bool BCLog::Logger::DefaultShrinkDebugFile() const
166
0
{
167
0
    return m_categories == BCLog::NONE;
168
0
}
169
170
static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
171
    {"net", BCLog::NET},
172
    {"tor", BCLog::TOR},
173
    {"mempool", BCLog::MEMPOOL},
174
    {"http", BCLog::HTTP},
175
    {"bench", BCLog::BENCH},
176
    {"zmq", BCLog::ZMQ},
177
    {"walletdb", BCLog::WALLETDB},
178
    {"rpc", BCLog::RPC},
179
    {"estimatefee", BCLog::ESTIMATEFEE},
180
    {"addrman", BCLog::ADDRMAN},
181
    {"selectcoins", BCLog::SELECTCOINS},
182
    {"reindex", BCLog::REINDEX},
183
    {"cmpctblock", BCLog::CMPCTBLOCK},
184
    {"rand", BCLog::RAND},
185
    {"prune", BCLog::PRUNE},
186
    {"proxy", BCLog::PROXY},
187
    {"mempoolrej", BCLog::MEMPOOLREJ},
188
    {"libevent", BCLog::LIBEVENT},
189
    {"coindb", BCLog::COINDB},
190
    {"qt", BCLog::QT},
191
    {"leveldb", BCLog::LEVELDB},
192
    {"validation", BCLog::VALIDATION},
193
    {"i2p", BCLog::I2P},
194
    {"ipc", BCLog::IPC},
195
#ifdef DEBUG_LOCKCONTENTION
196
    {"lock", BCLog::LOCK},
197
#endif
198
    {"blockstorage", BCLog::BLOCKSTORAGE},
199
    {"txreconciliation", BCLog::TXRECONCILIATION},
200
    {"scan", BCLog::SCAN},
201
    {"txpackages", BCLog::TXPACKAGES},
202
};
203
204
static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
205
    // Swap keys and values from LOG_CATEGORIES_BY_STR.
206
0
    [](const auto& in) {
207
0
        std::unordered_map<BCLog::LogFlags, std::string> out;
208
0
        for (const auto& [k, v] : in) {
  Branch (208:33): [True: 0, False: 0]
209
0
            const bool inserted{out.emplace(v, k).second};
210
0
            assert(inserted);
211
0
        }
212
0
        return out;
213
0
    }(LOG_CATEGORIES_BY_STR)
214
};
215
216
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
217
0
{
218
0
    if (str.empty() || str == "1" || str == "all") {
  Branch (218:9): [True: 0, False: 0]
  Branch (218:24): [True: 0, False: 0]
  Branch (218:38): [True: 0, False: 0]
219
0
        flag = BCLog::ALL;
220
0
        return true;
221
0
    }
222
0
    auto it = LOG_CATEGORIES_BY_STR.find(str);
223
0
    if (it != LOG_CATEGORIES_BY_STR.end()) {
  Branch (223:9): [True: 0, False: 0]
224
0
        flag = it->second;
225
0
        return true;
226
0
    }
227
0
    return false;
228
0
}
229
230
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
231
0
{
232
0
    switch (level) {
  Branch (232:13): [True: 0, False: 0]
233
0
    case BCLog::Level::Trace:
  Branch (233:5): [True: 0, False: 0]
234
0
        return "trace";
235
0
    case BCLog::Level::Debug:
  Branch (235:5): [True: 0, False: 0]
236
0
        return "debug";
237
0
    case BCLog::Level::Info:
  Branch (237:5): [True: 0, False: 0]
238
0
        return "info";
239
0
    case BCLog::Level::Warning:
  Branch (239:5): [True: 0, False: 0]
240
0
        return "warning";
241
0
    case BCLog::Level::Error:
  Branch (241:5): [True: 0, False: 0]
242
0
        return "error";
243
0
    }
244
0
    assert(false);
245
0
}
246
247
static std::string LogCategoryToStr(BCLog::LogFlags category)
248
0
{
249
0
    if (category == BCLog::ALL) {
  Branch (249:9): [True: 0, False: 0]
250
0
        return "all";
251
0
    }
252
0
    auto it = LOG_CATEGORIES_BY_FLAG.find(category);
253
0
    assert(it != LOG_CATEGORIES_BY_FLAG.end());
254
0
    return it->second;
255
0
}
256
257
static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str)
258
0
{
259
0
    if (level_str == "trace") {
  Branch (259:9): [True: 0, False: 0]
260
0
        return BCLog::Level::Trace;
261
0
    } else if (level_str == "debug") {
  Branch (261:16): [True: 0, False: 0]
262
0
        return BCLog::Level::Debug;
263
0
    } else if (level_str == "info") {
  Branch (263:16): [True: 0, False: 0]
264
0
        return BCLog::Level::Info;
265
0
    } else if (level_str == "warning") {
  Branch (265:16): [True: 0, False: 0]
266
0
        return BCLog::Level::Warning;
267
0
    } else if (level_str == "error") {
  Branch (267:16): [True: 0, False: 0]
268
0
        return BCLog::Level::Error;
269
0
    } else {
270
0
        return std::nullopt;
271
0
    }
272
0
}
273
274
std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
275
0
{
276
0
    std::vector<LogCategory> ret;
277
0
    ret.reserve(LOG_CATEGORIES_BY_STR.size());
278
0
    for (const auto& [category, flag] : LOG_CATEGORIES_BY_STR) {
  Branch (278:39): [True: 0, False: 0]
279
0
        ret.push_back(LogCategory{.category = category, .active = WillLogCategory(flag)});
280
0
    }
281
0
    return ret;
282
0
}
283
284
/** Log severity levels that can be selected by the user. */
285
static constexpr std::array<BCLog::Level, 3> LogLevelsList()
286
0
{
287
0
    return {BCLog::Level::Info, BCLog::Level::Debug, BCLog::Level::Trace};
288
0
}
289
290
std::string BCLog::Logger::LogLevelsString() const
291
0
{
292
0
    const auto& levels = LogLevelsList();
293
0
    return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [](BCLog::Level level) { return LogLevelToStr(level); });
294
0
}
295
296
std::string BCLog::Logger::LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
297
0
{
298
0
    std::string strStamped;
299
300
0
    if (!m_log_timestamps)
  Branch (300:9): [True: 0, False: 0]
301
0
        return strStamped;
302
303
0
    const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
304
0
    strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
305
0
    if (m_log_time_micros && !strStamped.empty()) {
  Branch (305:9): [True: 0, False: 0]
  Branch (305:30): [True: 0, False: 0]
306
0
        strStamped.pop_back();
307
0
        strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
308
0
    }
309
0
    if (mocktime > 0s) {
  Branch (309:9): [True: 0, False: 0]
310
0
        strStamped += " (mocktime: " + FormatISO8601DateTime(count_seconds(mocktime)) + ")";
311
0
    }
312
0
    strStamped += ' ';
313
314
0
    return strStamped;
315
0
}
316
317
namespace BCLog {
318
    /** Belts and suspenders: make sure outgoing log messages don't contain
319
     * potentially suspicious characters, such as terminal control codes.
320
     *
321
     * This escapes control characters except newline ('\n') in C syntax.
322
     * It escapes instead of removes them to still allow for troubleshooting
323
     * issues where they accidentally end up in strings.
324
     */
325
0
    std::string LogEscapeMessage(std::string_view str) {
326
0
        std::string ret;
327
0
        for (char ch_in : str) {
  Branch (327:25): [True: 0, False: 0]
328
0
            uint8_t ch = (uint8_t)ch_in;
329
0
            if ((ch >= 32 || ch == '\n') && ch != '\x7f') {
  Branch (329:18): [True: 0, False: 0]
  Branch (329:30): [True: 0, False: 0]
  Branch (329:45): [True: 0, False: 0]
330
0
                ret += ch_in;
331
0
            } else {
332
0
                ret += strprintf("\\x%02x", ch);
333
0
            }
334
0
        }
335
0
        return ret;
336
0
    }
337
} // namespace BCLog
338
339
std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level level) const
340
0
{
341
0
    if (category == LogFlags::NONE) category = LogFlags::ALL;
  Branch (341:9): [True: 0, False: 0]
342
343
0
    const bool has_category{m_always_print_category_level || category != LogFlags::ALL};
  Branch (343:29): [True: 0, False: 0]
  Branch (343:62): [True: 0, False: 0]
344
345
    // If there is no category, Info is implied
346
0
    if (!has_category && level == Level::Info) return {};
  Branch (346:9): [True: 0, False: 0]
  Branch (346:26): [True: 0, False: 0]
347
348
0
    std::string s{"["};
349
0
    if (has_category) {
  Branch (349:9): [True: 0, False: 0]
350
0
        s += LogCategoryToStr(category);
351
0
    }
352
353
0
    if (m_always_print_category_level || !has_category || level != Level::Debug) {
  Branch (353:9): [True: 0, False: 0]
  Branch (353:42): [True: 0, False: 0]
  Branch (353:59): [True: 0, False: 0]
354
        // If there is a category, Debug is implied, so don't add the level
355
356
        // Only add separator if we have a category
357
0
        if (has_category) s += ":";
  Branch (357:13): [True: 0, False: 0]
358
0
        s += Logger::LogLevelToStr(level);
359
0
    }
360
361
0
    s += "] ";
362
0
    return s;
363
0
}
364
365
static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog)
366
0
{
367
0
    return buflog.str.size() + buflog.logging_function.size() + buflog.source_file.size() + buflog.threadname.size() + memusage::MallocUsage(sizeof(memusage::list_node<BCLog::Logger::BufferedLog>));
368
0
}
369
370
void BCLog::Logger::FormatLogStrInPlace(std::string& str, BCLog::LogFlags category, BCLog::Level level, std::string_view source_file, int source_line, std::string_view logging_function, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
371
0
{
372
0
    str.insert(0, GetLogPrefix(category, level));
373
374
0
    if (m_log_sourcelocations) {
  Branch (374:9): [True: 0, False: 0]
375
0
        str.insert(0, strprintf("[%s:%d] [%s] ", RemovePrefixView(source_file, "./"), source_line, logging_function));
376
0
    }
377
378
0
    if (m_log_threadnames) {
  Branch (378:9): [True: 0, False: 0]
379
0
        str.insert(0, strprintf("[%s] ", (threadname.empty() ? "unknown" : threadname)));
  Branch (379:43): [True: 0, False: 0]
380
0
    }
381
382
0
    str.insert(0, LogTimestampStr(now, mocktime));
383
0
}
384
385
void BCLog::Logger::LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
386
0
{
387
0
    StdLockGuard scoped_lock(m_cs);
388
0
    return LogPrintStr_(str, logging_function, source_file, source_line, category, level);
389
0
}
390
391
void BCLog::Logger::LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
392
0
{
393
0
    std::string str_prefixed = LogEscapeMessage(str);
394
395
0
    const bool starts_new_line = m_started_new_line;
396
0
    m_started_new_line = !str.empty() && str[str.size()-1] == '\n';
  Branch (396:26): [True: 0, False: 0]
  Branch (396:42): [True: 0, False: 0]
397
398
0
    if (m_buffering) {
  Branch (398:9): [True: 0, False: 0]
399
0
        if (!starts_new_line) {
  Branch (399:13): [True: 0, False: 0]
400
0
            if (!m_msgs_before_open.empty()) {
  Branch (400:17): [True: 0, False: 0]
401
0
                m_msgs_before_open.back().str += str_prefixed;
402
0
                m_cur_buffer_memusage += str_prefixed.size();
403
0
                return;
404
0
            } else {
405
                // unlikely edge case; add a marker that something was trimmed
406
0
                str_prefixed.insert(0, "[...] ");
407
0
            }
408
0
        }
409
410
0
        {
411
0
            BufferedLog buf{
412
0
                .now=SystemClock::now(),
413
0
                .mocktime=GetMockTime(),
414
0
                .str=str_prefixed,
415
0
                .logging_function=std::string(logging_function),
416
0
                .source_file=std::string(source_file),
417
0
                .threadname=util::ThreadGetInternalName(),
418
0
                .source_line=source_line,
419
0
                .category=category,
420
0
                .level=level,
421
0
            };
422
0
            m_cur_buffer_memusage += MemUsage(buf);
423
0
            m_msgs_before_open.push_back(std::move(buf));
424
0
        }
425
426
0
        while (m_cur_buffer_memusage > m_max_buffer_memusage) {
  Branch (426:16): [True: 0, False: 0]
427
0
            if (m_msgs_before_open.empty()) {
  Branch (427:17): [True: 0, False: 0]
428
0
                m_cur_buffer_memusage = 0;
429
0
                break;
430
0
            }
431
0
            m_cur_buffer_memusage -= MemUsage(m_msgs_before_open.front());
432
0
            m_msgs_before_open.pop_front();
433
0
            ++m_buffer_lines_discarded;
434
0
        }
435
436
0
        return;
437
0
    }
438
439
0
    if (starts_new_line) {
  Branch (439:9): [True: 0, False: 0]
440
0
        FormatLogStrInPlace(str_prefixed, category, level, source_file, source_line, logging_function, util::ThreadGetInternalName(), SystemClock::now(), GetMockTime());
441
0
    }
442
443
0
    if (m_print_to_console) {
  Branch (443:9): [True: 0, False: 0]
444
        // print to console
445
0
        fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout);
446
0
        fflush(stdout);
447
0
    }
448
0
    for (const auto& cb : m_print_callbacks) {
  Branch (448:25): [True: 0, False: 0]
449
0
        cb(str_prefixed);
450
0
    }
451
0
    if (m_print_to_file) {
  Branch (451:9): [True: 0, False: 0]
452
0
        assert(m_fileout != nullptr);
453
454
        // reopen the log file, if requested
455
0
        if (m_reopen_file) {
  Branch (455:13): [True: 0, False: 0]
456
0
            m_reopen_file = false;
457
0
            FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
458
0
            if (new_fileout) {
  Branch (458:17): [True: 0, False: 0]
459
0
                setbuf(new_fileout, nullptr); // unbuffered
460
0
                fclose(m_fileout);
461
0
                m_fileout = new_fileout;
462
0
            }
463
0
        }
464
0
        FileWriteStr(str_prefixed, m_fileout);
465
0
    }
466
0
}
467
468
void BCLog::Logger::ShrinkDebugFile()
469
0
{
470
    // Amount of debug.log to save at end when shrinking (must fit in memory)
471
0
    constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
472
473
0
    assert(!m_file_path.empty());
474
475
    // Scroll debug.log if it's getting too big
476
0
    FILE* file = fsbridge::fopen(m_file_path, "r");
477
478
    // Special files (e.g. device nodes) may not have a size.
479
0
    size_t log_size = 0;
480
0
    try {
481
0
        log_size = fs::file_size(m_file_path);
482
0
    } catch (const fs::filesystem_error&) {}
483
484
    // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
485
    // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
486
0
    if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
  Branch (486:9): [True: 0, False: 0]
  Branch (486:17): [True: 0, False: 0]
487
0
    {
488
        // Restart the file with some of the end
489
0
        std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
490
0
        if (fseek(file, -((long)vch.size()), SEEK_END)) {
  Branch (490:13): [True: 0, False: 0]
491
0
            LogPrintf("Failed to shrink debug log file: fseek(...) failed\n");
492
0
            fclose(file);
493
0
            return;
494
0
        }
495
0
        int nBytes = fread(vch.data(), 1, vch.size(), file);
496
0
        fclose(file);
497
498
0
        file = fsbridge::fopen(m_file_path, "w");
499
0
        if (file)
  Branch (499:13): [True: 0, False: 0]
500
0
        {
501
0
            fwrite(vch.data(), 1, nBytes, file);
502
0
            fclose(file);
503
0
        }
504
0
    }
505
0
    else if (file != nullptr)
  Branch (505:14): [True: 0, False: 0]
506
0
        fclose(file);
507
0
}
508
509
bool BCLog::Logger::SetLogLevel(std::string_view level_str)
510
0
{
511
0
    const auto level = GetLogLevel(level_str);
512
0
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
  Branch (512:9): [True: 0, False: 0]
  Branch (512:31): [True: 0, False: 0]
513
0
    m_log_level = level.value();
514
0
    return true;
515
0
}
516
517
bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
518
0
{
519
0
    BCLog::LogFlags flag;
520
0
    if (!GetLogCategory(flag, category_str)) return false;
  Branch (520:9): [True: 0, False: 0]
521
522
0
    const auto level = GetLogLevel(level_str);
523
0
    if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
  Branch (523:9): [True: 0, False: 0]
  Branch (523:31): [True: 0, False: 0]
524
525
0
    StdLockGuard scoped_lock(m_cs);
526
0
    m_category_log_levels[flag] = level.value();
527
0
    return true;
528
0
}