Skip to content

Commit

Permalink
Merge bitcoin#27238: refactor: Split logging utilities from system.h
Browse files Browse the repository at this point in the history
aaced56 refactor: Move error() from util/system.h to logging.h (Ben Woosley)
e7333b4 refactor: Extract util/exception from util/system (Ben Woosley)

Pull request description:

  This pull request is part of the `libbitcoinkernel` project bitcoin#24303 https://github.com/bitcoin/bitcoin/projects/18 and more specifically its "Step 2: Decouple most non-consensus code from libbitcoinkernel". These commits were originally authored by empact and are taken from their parent PR bitcoin#25152.

  #### Context

  There is an ongoing effort to decouple the `ArgsManager` used for command line parsing user-provided arguments from the libbitcoinkernel library (bitcoin#25290, bitcoin#25487, bitcoin#25527, bitcoin#25862, bitcoin#26177, and bitcoin#27125). The `ArgsManager` is defined in `system.h`.

  #### Changes

  Next to providing better code organization, this PR removes some reliance of the tree of libbitcoinkernel header includes on `system.h` (and thus the `ArgsManager` definition) by moving some logging functions out of the `system.*` files.

  Further commits splitting more functionality out of `system.h` are still in bitcoin#25152 and will be submitted in separate PRs once this PR has been processed.

ACKs for top commit:
  MarcoFalke:
    re-ACK aaced56 🐍

Tree-SHA512: cb39f4cb7a77e7dc1887b1cbf340d53decab8880fc00878a2f12dc627fe67245b4aafd4cc31a9eab0fad1e5bb5d0eb4cdb8d501323ca200fa6ab7b201ae34aea
  • Loading branch information
fanquake committed Mar 14, 2023
2 parents f50fb17 + aaced56 commit b175bdb
Show file tree
Hide file tree
Showing 26 changed files with 87 additions and 42 deletions.
1 change: 1 addition & 0 deletions ci/test/06_script_b.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ if [ "${RUN_TIDY}" = "true" ]; then
" src/util/bytevectorhash.cpp"\
" src/util/check.cpp"\
" src/util/error.cpp"\
" src/util/exception.cpp"\
" src/util/getuniquepath.cpp"\
" src/util/hasher.cpp"\
" src/util/message.cpp"\
Expand Down
3 changes: 3 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ BITCOIN_CORE_H = \
util/check.h \
util/epochguard.h \
util/error.h \
util/exception.h \
util/fastrange.h \
util/fees.h \
util/getuniquepath.h \
Expand Down Expand Up @@ -699,6 +700,7 @@ libbitcoin_util_a_SOURCES = \
util/bytevectorhash.cpp \
util/check.cpp \
util/error.cpp \
util/exception.cpp \
util/fees.cpp \
util/getuniquepath.cpp \
util/hasher.cpp \
Expand Down Expand Up @@ -942,6 +944,7 @@ libbitcoinkernel_la_SOURCES = \
txmempool.cpp \
uint256.cpp \
util/check.cpp \
util/exception.cpp \
util/getuniquepath.cpp \
util/hasher.cpp \
util/moneystr.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/addrdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <cstdint>
#include <fs.h>
#include <hash.h>
#include <logging.h>
#include <logging/timer.h>
#include <netbase.h>
#include <netgroup.h>
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <rpc/request.h>
#include <tinyformat.h>
#include <univalue.h>
#include <util/exception.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <util/translation.h>
Expand Down
3 changes: 2 additions & 1 deletion src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
#include <consensus/amount.h>
#include <consensus/consensus.h>
#include <core_io.h>
#include <key_io.h>
#include <fs.h>
#include <key_io.h>
#include <policy/policy.h>
#include <primitives/transaction.h>
#include <script/script.h>
#include <script/sign.h>
#include <script/signingprovider.h>
#include <univalue.h>
#include <util/exception.h>
#include <util/moneystr.h>
#include <util/rbf.h>
#include <util/strencodings.h>
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <compat/compat.h>
#include <core_io.h>
#include <streams.h>
#include <util/exception.h>
#include <util/system.h>
#include <util/translation.h>
#include <version.h>
Expand Down
1 change: 1 addition & 0 deletions src/bitcoin-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <logging.h>
#include <pubkey.h>
#include <tinyformat.h>
#include <util/exception.h>
#include <util/system.h>
#include <util/translation.h>
#include <wallet/wallettool.h>
Expand Down
1 change: 1 addition & 0 deletions src/bitcoind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <noui.h>
#include <shutdown.h>
#include <util/check.h>
#include <util/exception.h>
#include <util/strencodings.h>
#include <util/syscall_sandbox.h>
#include <util/syserror.h>
Expand Down
1 change: 1 addition & 0 deletions src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <index/base.h>
#include <interfaces/chain.h>
#include <kernel/chain.h>
#include <logging.h>
#include <node/blockstorage.h>
#include <node/context.h>
#include <node/database_args.h>
Expand Down
1 change: 1 addition & 0 deletions src/index/coinstatsindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <crypto/muhash.h>
#include <index/coinstatsindex.h>
#include <kernel/coinstats.h>
#include <logging.h>
#include <node/blockstorage.h>
#include <serialize.h>
#include <txdb.h>
Expand Down
1 change: 1 addition & 0 deletions src/index/txindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <index/txindex.h>

#include <index/disktxpos.h>
#include <logging.h>
#include <node/blockstorage.h>
#include <util/system.h>
#include <validation.h>
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/coinstats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <coins.h>
#include <crypto/muhash.h>
#include <hash.h>
#include <logging.h>
#include <node/blockstorage.h>
#include <primitives/transaction.h>
#include <script/script.h>
Expand All @@ -19,7 +20,6 @@
#include <uint256.h>
#include <util/check.h>
#include <util/overflow.h>
#include <util/system.h>
#include <validation.h>
#include <version.h>

Expand Down
7 changes: 7 additions & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,11 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
} \
} while (0)

template <typename... Args>
bool error(const char* fmt, const Args&... args)
{
LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
return false;
}

#endif // BITCOIN_LOGGING_H
3 changes: 2 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
#include <compat/compat.h>
#include <consensus/consensus.h>
#include <crypto/sha256.h>
#include <node/eviction.h>
#include <fs.h>
#include <i2p.h>
#include <logging.h>
#include <net_permissions.h>
#include <netaddress.h>
#include <netbase.h>
#include <node/eviction.h>
#include <node/interface_ui.h>
#include <protocol.h>
#include <random.h>
Expand Down
2 changes: 1 addition & 1 deletion src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include <netbase.h>

#include <compat/compat.h>
#include <logging.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/sock.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/system.h>
#include <util/time.h>

#include <atomic>
Expand Down
1 change: 1 addition & 0 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <flatfile.h>
#include <fs.h>
#include <hash.h>
#include <logging.h>
#include <pow.h>
#include <reverse_iterator.h>
#include <shutdown.h>
Expand Down
1 change: 1 addition & 0 deletions src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <qt/utilitydialog.h>
#include <qt/winshutdownmonitor.h>
#include <uint256.h>
#include <util/exception.h>
#include <util/string.h>
#include <util/system.h>
#include <util/threadnames.h>
Expand Down
1 change: 1 addition & 0 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <protocol.h>
#include <script/script.h>
#include <script/standard.h>
#include <util/exception.h>
#include <util/system.h>
#include <util/time.h>

Expand Down
2 changes: 1 addition & 1 deletion src/qt/initexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <qt/initexecutor.h>

#include <interfaces/node.h>
#include <util/system.h>
#include <util/exception.h>
#include <util/threadnames.h>

#include <exception>
Expand Down
2 changes: 1 addition & 1 deletion src/script/signingprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <script/signingprovider.h>
#include <script/standard.h>

#include <util/system.h>
#include <logging.h>

const SigningProvider& DUMMY_SIGNING_PROVIDER = SigningProvider();

Expand Down
2 changes: 1 addition & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <txdb.h>

#include <chain.h>
#include <logging.h>
#include <pow.h>
#include <random.h>
#include <shutdown.h>
#include <uint256.h>
#include <util/system.h>
#include <util/translation.h>
#include <util/vector.h>

Expand Down
41 changes: 41 additions & 0 deletions src/util/exception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <util/exception.h>

#include <logging.h>
#include <tinyformat.h>

#include <exception>
#include <iostream>
#include <string>
#include <typeinfo>

#ifdef WIN32
#include <windows.h>
#endif // WIN32

static std::string FormatException(const std::exception* pex, std::string_view thread_name)
{
#ifdef WIN32
char pszModule[MAX_PATH] = "";
GetModuleFileNameA(nullptr, pszModule, sizeof(pszModule));
#else
const char* pszModule = "bitcoin";
#endif
if (pex)
return strprintf(
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, thread_name);
else
return strprintf(
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, thread_name);
}

void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name)
{
std::string message = FormatException(pex, thread_name);
LogPrintf("\n\n************************\n%s\n", message);
tfm::format(std::cerr, "\n\n************************\n%s\n", message);
}
14 changes: 14 additions & 0 deletions src/util/exception.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2023 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_UTIL_EXCEPTION_H
#define BITCOIN_UTIL_EXCEPTION_H

#include <exception>
#include <string_view>

void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name);

#endif // BITCOIN_UTIL_EXCEPTION_H
23 changes: 0 additions & 23 deletions src/util/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,29 +792,6 @@ std::string HelpMessageOpt(const std::string &option, const std::string &message
std::string("\n\n");
}

static std::string FormatException(const std::exception* pex, std::string_view thread_name)
{
#ifdef WIN32
char pszModule[MAX_PATH] = "";
GetModuleFileNameA(nullptr, pszModule, sizeof(pszModule));
#else
const char* pszModule = "bitcoin";
#endif
if (pex)
return strprintf(
"EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, thread_name);
else
return strprintf(
"UNKNOWN EXCEPTION \n%s in %s \n", pszModule, thread_name);
}

void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name)
{
std::string message = FormatException(pex, thread_name);
LogPrintf("\n\n************************\n%s\n", message);
tfm::format(std::cerr, "\n\n************************\n%s\n", message);
}

fs::path GetDefaultDataDir()
{
// Windows: C:\Users\Username\AppData\Roaming\Bitcoin
Expand Down
11 changes: 0 additions & 11 deletions src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@
#include <fs.h>
#include <logging.h>
#include <sync.h>
#include <tinyformat.h>
#include <util/settings.h>
#include <util/time.h>

#include <any>
#include <exception>
#include <map>
#include <optional>
#include <set>
Expand All @@ -45,15 +43,6 @@ extern const char * const BITCOIN_SETTINGS_FILENAME;
void SetupEnvironment();
bool SetupNetworking();

template<typename... Args>
bool error(const char* fmt, const Args&... args)
{
LogPrintf("ERROR: %s\n", tfm::format(fmt, args...));
return false;
}

void PrintExceptionContinue(const std::exception* pex, std::string_view thread_name);

/**
* Ensure file contents are fully committed to disk, using a platform-specific
* feature analogous to fsync().
Expand Down
2 changes: 1 addition & 1 deletion src/util/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <util/thread.h>

#include <logging.h>
#include <util/system.h>
#include <util/exception.h>
#include <util/threadnames.h>

#include <exception>
Expand Down

0 comments on commit b175bdb

Please sign in to comment.