From 493dd26b75d0e35422b3230be3c72a0f96eb5e8a Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 3 Jul 2024 14:19:20 -0400 Subject: [PATCH] kernel: Drop global Logger instance Change LogInstance() function to no longer allocate (and leak) a BCLog::Logger instance. Instead allow kernel applications to initialize their own logging instances that can be returned by LogInstance(). The LogInstance() function is not actually used for the majority of logging in kernel code. Most kernel log output goes directly to BCLog::Logger instances specified when kernel objects like ChainstateManager and CTxMemPool are constructed, which allows applications to create multiple Logger instances and control which log output is sent to them. The only kernel code that does rely on LogInstance() is certain low level code in the util library, like the RNG seeder, that is not passed a specific instance and needs to rely on the global LogInstance() function. Other code outside the kernel library uses LogInstance() heavily, and may continue to do so. bitcoind and test code now just create a log instance before the first LogInstance() call, which it returns, so all behavior is the same as before. --- src/bench/bench_bitcoin.cpp | 2 ++ src/bitcoin-cli.cpp | 2 ++ src/bitcoin-tx.cpp | 2 ++ src/bitcoin-util.cpp | 2 ++ src/bitcoin-wallet.cpp | 2 ++ src/bitcoind.cpp | 4 ++++ src/logging.cpp | 27 ++++++++++++++++++++------- src/logging.h | 3 +++ src/qt/bitcoin.cpp | 3 +++ src/qt/test/test_main.cpp | 2 ++ src/test/blockfilter_tests.cpp | 2 +- src/test/fuzz/block_index.cpp | 4 ++-- src/test/fuzz/fuzz.cpp | 4 ++++ src/test/fuzz/mini_miner.cpp | 4 ++-- src/test/fuzz/script_sigcache.cpp | 4 ++-- src/test/reverselock_tests.cpp | 2 +- src/test/scheduler_tests.cpp | 3 ++- src/test/sync_tests.cpp | 2 +- src/test/util/setup_common.cpp | 4 +++- src/test/util/setup_common.h | 2 +- 20 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp index 88afe68a1a992..ce9ae69ef1d99 100644 --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -76,6 +77,7 @@ static std::vector parseTestSetupArgs(const ArgsManager& argsman) int main(int argc, char** argv) { + BCLog::Logger logger; ArgsManager argsman; SetupBenchArgs(argsman); SHA256AutoDetect(); diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 5c5965245bc6a..17b703ad5dfd7 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -1331,6 +1332,7 @@ MAIN_FUNCTION common::WinCmdLineArgs winArgs; std::tie(argc, argv) = winArgs.get(); #endif + BCLog::Logger logger; SetupEnvironment(); if (!SetupNetworking()) { tfm::format(std::cerr, "Error: Initializing networking failed\n"); diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp index a627c24e86839..ef3a310d4b733 100644 --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include