Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/bench/bench_bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <bench/bench.h>
#include <common/args.h>
#include <crypto/sha256.h>
#include <logging.h>
#include <tinyformat.h>
#include <util/fs.h>
#include <util/string.h>
Expand Down Expand Up @@ -76,6 +77,7 @@ static std::vector<std::string> parseTestSetupArgs(const ArgsManager& argsman)

int main(int argc, char** argv)
{
BCLog::Logger logger;
ArgsManager argsman;
SetupBenchArgs(argsman);
SHA256AutoDetect();
Expand Down
4 changes: 2 additions & 2 deletions src/bitcoin-chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ int main(int argc, char* argv[])
.always_print_category_levels = true,
};

logging_set_options(logging_options);

Logger logger{std::make_unique<KernelLog>()};
logging_set_options(logger, logging_options);

ContextOptions options{};
ChainParams params{ChainType::MAINNET};
options.SetLogger(logger);
options.SetChainParams(params);

options.SetNotifications(std::make_shared<TestKernelNotifications>());
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <common/system.h>
#include <compat/compat.h>
#include <compat/stdin.h>
#include <logging.h>
#include <policy/feerate.h>
#include <rpc/client.h>
#include <rpc/mining.h>
Expand Down Expand Up @@ -1327,6 +1328,7 @@ static int CommandLineRPC(int argc, char *argv[])

MAIN_FUNCTION
{
BCLog::Logger logger;
SetupEnvironment();
if (!SetupNetworking()) {
tfm::format(std::cerr, "Error: Initializing networking failed\n");
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <consensus/consensus.h>
#include <core_io.h>
#include <key_io.h>
#include <logging.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <script/script.h>
Expand Down Expand Up @@ -857,6 +858,7 @@ static int CommandLineRawTx(int argc, char* argv[])

MAIN_FUNCTION
{
BCLog::Logger logger;
SetupEnvironment();

try {
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoin-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <common/system.h>
#include <compat/compat.h>
#include <core_io.h>
#include <logging.h>
#include <streams.h>
#include <util/exception.h>
#include <util/strencodings.h>
Expand Down Expand Up @@ -152,6 +153,7 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
MAIN_FUNCTION
{
ArgsManager& args = gArgs;
BCLog::Logger logger;
SetupEnvironment();

try {
Expand Down
2 changes: 2 additions & 0 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ MAIN_FUNCTION
{
ArgsManager& args = gArgs;

BCLog::Logger logger;

int exit_status;
std::unique_ptr<interfaces::Init> init = interfaces::MakeWalletInit(argc, argv, exit_status);
if (!init) {
Expand Down
4 changes: 4 additions & 0 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <init.h>
#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <logging.h>
#include <kernel/context.h>
#include <node/context.h>
#include <node/interface_ui.h>
Expand Down Expand Up @@ -259,6 +260,9 @@ static bool AppInit(NodeContext& node)

MAIN_FUNCTION
{
// Intentionally leaked! See BCLog::g_logger description for rationale.
new BCLog::Logger;

NodeContext node;
int exit_status;
std::unique_ptr<interfaces::Init> init = interfaces::MakeNodeInit(node, argc, argv, exit_status);
Expand Down
63 changes: 33 additions & 30 deletions src/dbwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,25 @@ bool DestroyDB(const std::string& path_str)

/** Handle database error by throwing dbwrapper_error exception.
*/
static void HandleError(const leveldb::Status& status)
static void HandleError(const BCLog::Context& log, const leveldb::Status& status)
{
if (status.ok())
return;
const std::string errmsg = "Fatal LevelDB error: " + status.ToString();
LogError("%s", errmsg);
LogInfo("You can use -debug=leveldb to get more complete diagnostic messages");
LogError(log, "%s", errmsg);
LogInfo(log, "You can use -debug=leveldb to get more complete diagnostic messages");
throw dbwrapper_error(errmsg);
}

class CBitcoinLevelDBLogger : public leveldb::Logger {
private:
const BCLog::Context& m_log;
public:
CBitcoinLevelDBLogger(const BCLog::Context& log) : m_log{log} {}
// This code is adapted from posix_logger.h, which is why it is using vsprintf.
// Please do not do this in normal code
void Logv(const char * format, va_list ap) override {
if (!LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug)) {
if (!LogEnabled(m_log, BCLog::Level::Debug)) {
return;
}
char buffer[500];
Expand Down Expand Up @@ -101,7 +104,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {

assert(p <= limit);
base[std::min(bufsize - 1, (int)(p - base))] = '\0';
LogDebug(BCLog::LEVELDB, "%s\n", util::RemoveSuffixView(base, "\n"));
LogDebug(m_log, "%s\n", util::RemoveSuffixView(base, "\n"));
if (base != buffer) {
delete[] base;
}
Expand All @@ -110,7 +113,7 @@ class CBitcoinLevelDBLogger : public leveldb::Logger {
}
};

static void SetMaxOpenFiles(leveldb::Options *options) {
static void SetMaxOpenFiles(const BCLog::Context& log, leveldb::Options *options) {
// On most platforms the default setting of max_open_files (which is 1000)
// is optimal. On Windows using a large file count is OK because the handles
// do not interfere with select() loops. On 64-bit Unix hosts this value is
Expand All @@ -131,25 +134,25 @@ static void SetMaxOpenFiles(leveldb::Options *options) {
options->max_open_files = 64;
}
#endif
LogDebug(BCLog::LEVELDB, "LevelDB using max_open_files=%d (default=%d)\n",
LogDebug(log, "LevelDB using max_open_files=%d (default=%d)\n",
options->max_open_files, default_open_files);
}

static leveldb::Options GetOptions(size_t nCacheSize)
static leveldb::Options GetOptions(const BCLog::Context& log, size_t nCacheSize)
{
leveldb::Options options;
options.block_cache = leveldb::NewLRUCache(nCacheSize / 2);
options.write_buffer_size = nCacheSize / 4; // up to two write buffers may be held in memory simultaneously
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
options.compression = leveldb::kNoCompression;
options.info_log = new CBitcoinLevelDBLogger();
options.info_log = new CBitcoinLevelDBLogger(log);
if (leveldb::kMajorVersion > 1 || (leveldb::kMajorVersion == 1 && leveldb::kMinorVersion >= 16)) {
// LevelDB versions before 1.16 consider short writes to be corruption. Only trigger error
// on corruption in later versions.
options.paranoid_checks = true;
}
options.max_file_size = std::max(options.max_file_size, DBWRAPPER_MAX_FILE_SIZE);
SetMaxOpenFiles(&options);
SetMaxOpenFiles(log, &options);
return options;
}

Expand Down Expand Up @@ -213,40 +216,40 @@ struct LevelDBContext {
leveldb::DB* pdb;
};

CDBWrapper::CDBWrapper(const DBParams& params)
: m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
CDBWrapper::CDBWrapper(BCLog::Logger& logger, const DBParams& params)
: m_log{logger, BCLog::LEVELDB}, m_db_context{std::make_unique<LevelDBContext>()}, m_name{fs::PathToString(params.path.stem())}, m_path{params.path}, m_is_memory{params.memory_only}
{
DBContext().penv = nullptr;
DBContext().readoptions.verify_checksums = true;
DBContext().iteroptions.verify_checksums = true;
DBContext().iteroptions.fill_cache = false;
DBContext().syncoptions.sync = true;
DBContext().options = GetOptions(params.cache_bytes);
DBContext().options = GetOptions(m_log, params.cache_bytes);
DBContext().options.create_if_missing = true;
if (params.memory_only) {
DBContext().penv = leveldb::NewMemEnv(leveldb::Env::Default());
DBContext().options.env = DBContext().penv;
} else {
if (params.wipe_data) {
LogInfo("Wiping LevelDB in %s", fs::PathToString(params.path));
LogInfo(m_log, "Wiping LevelDB in %s", fs::PathToString(params.path));
leveldb::Status result = leveldb::DestroyDB(fs::PathToString(params.path), DBContext().options);
HandleError(result);
HandleError(m_log, result);
}
TryCreateDirectories(params.path);
LogInfo("Opening LevelDB in %s", fs::PathToString(params.path));
LogInfo(m_log, "Opening LevelDB in %s", fs::PathToString(params.path));
}
// PathToString() return value is safe to pass to leveldb open function,
// because on POSIX leveldb passes the byte string directly to ::open(), and
// on Windows it converts from UTF-8 to UTF-16 before calling ::CreateFileW
// (see env_posix.cc and env_windows.cc).
leveldb::Status status = leveldb::DB::Open(DBContext().options, fs::PathToString(params.path), &DBContext().pdb);
HandleError(status);
LogInfo("Opened LevelDB successfully");
HandleError(m_log, status);
LogInfo(m_log, "Opened LevelDB successfully");

if (params.options.force_compact) {
LogInfo("Starting database compaction of %s", fs::PathToString(params.path));
LogInfo(m_log, "Starting database compaction of %s", fs::PathToString(params.path));
DBContext().pdb->CompactRange(nullptr, nullptr);
LogInfo("Finished database compaction of %s", fs::PathToString(params.path));
LogInfo(m_log, "Finished database compaction of %s", fs::PathToString(params.path));
}

if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
Expand All @@ -255,9 +258,9 @@ CDBWrapper::CDBWrapper(const DBParams& params)
assert(!m_obfuscation); // Make sure the key is written without obfuscation.
Write(OBFUSCATION_KEY, obfuscation);
m_obfuscation = obfuscation;
LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
LogInfo(m_log, "Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
}
LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
LogInfo(m_log, "Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
}

CDBWrapper::~CDBWrapper()
Expand All @@ -276,16 +279,16 @@ CDBWrapper::~CDBWrapper()

void CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
{
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB, BCLog::Level::Debug);
const bool log_memory{LogEnabled(m_log, BCLog::Level::Debug)};
double mem_before = 0;
if (log_memory) {
mem_before = DynamicMemoryUsage() / 1024.0 / 1024;
}
leveldb::Status status = DBContext().pdb->Write(fSync ? DBContext().syncoptions : DBContext().writeoptions, &batch.m_impl_batch->batch);
HandleError(status);
HandleError(m_log, status);
if (log_memory) {
double mem_after = DynamicMemoryUsage() / 1024.0 / 1024;
LogDebug(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
LogDebug(m_log, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
m_name, mem_before, mem_after);
}
}
Expand All @@ -295,7 +298,7 @@ size_t CDBWrapper::DynamicMemoryUsage() const
std::string memory;
std::optional<size_t> parsed;
if (!DBContext().pdb->GetProperty("leveldb.approximate-memory-usage", &memory) || !(parsed = ToIntegral<size_t>(memory))) {
LogDebug(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
LogDebug(m_log, "Failed to get approximate-memory-usage property\n");
return 0;
}
return parsed.value();
Expand All @@ -309,8 +312,8 @@ std::optional<std::string> CDBWrapper::ReadImpl(std::span<const std::byte> key)
if (!status.ok()) {
if (status.IsNotFound())
return std::nullopt;
LogError("LevelDB read failure: %s", status.ToString());
HandleError(status);
LogError(m_log, "LevelDB read failure: %s", status.ToString());
HandleError(m_log, status);
}
return strValue;
}
Expand All @@ -324,8 +327,8 @@ bool CDBWrapper::ExistsImpl(std::span<const std::byte> key) const
if (!status.ok()) {
if (status.IsNotFound())
return false;
LogError("LevelDB read failure: %s", status.ToString());
HandleError(status);
LogError(m_log, "LevelDB read failure: %s", status.ToString());
HandleError(m_log, status);
}
return true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/dbwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BITCOIN_DBWRAPPER_H

#include <attributes.h>
#include <logging.h>
#include <serialize.h>
#include <span.h>
#include <streams.h>
Expand Down Expand Up @@ -179,6 +180,9 @@ class CDBWrapper
{
friend const Obfuscation& dbwrapper_private::GetObfuscation(const CDBWrapper&);
private:
//! log object
BCLog::Context m_log;

//! holds all leveldb-specific fields of this class
std::unique_ptr<LevelDBContext> m_db_context;

Expand All @@ -203,7 +207,7 @@ class CDBWrapper
auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); }

public:
CDBWrapper(const DBParams& params);
CDBWrapper(BCLog::Logger& logger, const DBParams& params);
~CDBWrapper();

CDBWrapper(const CDBWrapper&) = delete;
Expand Down
2 changes: 1 addition & 1 deletion src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CBlockLocator GetLocator(interfaces::Chain& chain, const uint256& block_hash)
}

BaseIndex::DB::DB(const fs::path& path, size_t n_cache_size, bool f_memory, bool f_wipe, bool f_obfuscate) :
CDBWrapper{DBParams{
CDBWrapper{LogInstance(), DBParams{
.path = path,
.cache_bytes = n_cache_size,
.memory_only = f_memory,
Expand Down
5 changes: 3 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
if (!blockman_result) {
return InitError(util::ErrorString(blockman_result));
}
CTxMemPool::Options mempool_opts{};
CTxMemPool::Options mempool_opts{.logger = &LogInstance()};
auto mempool_result{ApplyArgsManOptions(args, chainparams, mempool_opts)};
if (!mempool_result) {
return InitError(util::ErrorString(mempool_result));
Expand Down Expand Up @@ -1288,6 +1288,7 @@ static ChainstateLoadResult InitAndLoadChainstate(

CTxMemPool::Options mempool_opts{
.check_ratio = chainparams.DefaultConsistencyChecks() ? 1 : 0,
.logger = &LogInstance(),
.signals = node.validation_signals.get(),
};
Assert(ApplyArgsManOptions(args, chainparams, mempool_opts)); // no error can happen, already checked in AppInitParameterInteraction
Expand Down Expand Up @@ -1325,7 +1326,7 @@ static ChainstateLoadResult InitAndLoadChainstate(
// The coinsdb is opened at a later point on LoadChainstate.
Assert(!node.chainman); // Was reset above
try {
node.chainman = std::make_unique<ChainstateManager>(*Assert(node.shutdown_signal), chainman_opts, blockman_opts);
node.chainman = std::make_unique<ChainstateManager>(LogInstance(), *Assert(node.shutdown_signal), chainman_opts, blockman_opts);
} catch (dbwrapper_error& e) {
LogError("%s", e.what());
return {ChainstateLoadStatus::FAILURE, _("Error opening block database")};
Expand Down
Loading
Loading