Skip to content

Commit dca56e0

Browse files
committed
kernel: implement levels-based logging C API
Because logging is now structured, bitcoinkernel users can implement their own filtering logic using the btck_LogEntry fields. Simplify the interface by replacing the category/levels setters with a single global btck_logging_set_min_level function. Implements a kernel-specific g_dispatcher with the levels-based filtering, so kernel's dependency on logging.cpp is now removed entirely.
1 parent 81dfedc commit dca56e0

File tree

6 files changed

+39
-71
lines changed

6 files changed

+39
-71
lines changed

contrib/valgrind.supp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
fun:_Znwm
4949
fun:_Z11LogInstancev
5050
}
51+
{
52+
Suppress g_kernel_logger still reachable memory warning
53+
Memcheck:Leak
54+
match-leak-kinds: reachable
55+
fun:_Znwm
56+
fun:_ZL15g_kernel_loggerv
57+
}
5158
{
5259
Suppress BCLog::Logger::StartLogging() still reachable memory warning
5360
Memcheck:Leak

src/kernel/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ add_library(bitcoinkernel
3030
../deploymentstatus.cpp
3131
../flatfile.cpp
3232
../hash.cpp
33-
../logging.cpp
3433
../node/blockstorage.cpp
3534
../node/chainstate.cpp
3635
../node/utxo_snapshot.cpp

src/kernel/bitcoinkernel.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <kernel/context.h>
1717
#include <kernel/notifications_interface.h>
1818
#include <kernel/warning.h>
19-
#include <logging.h>
2019
#include <node/blockstorage.h>
2120
#include <node/chainstate.h>
2221
#include <primitives/block.h>
@@ -29,6 +28,7 @@
2928
#include <uint256.h>
3029
#include <undo.h>
3130
#include <util/fs.h>
31+
#include <util/log.h>
3232
#include <util/result.h>
3333
#include <util/signalinterrupt.h>
3434
#include <util/string.h>
@@ -39,6 +39,7 @@
3939
#include <validationinterface.h>
4040

4141
#include <array>
42+
#include <atomic>
4243
#include <cassert>
4344
#include <cstddef>
4445
#include <cstring>
@@ -54,6 +55,26 @@
5455
#include <utility>
5556
#include <vector>
5657

58+
struct KernelLogger {
59+
std::atomic<util::log::Level> min_level{util::log::Level::Info};
60+
util::log::Dispatcher dispatcher{[this](util::log::Level level, uint64_t) {
61+
return level >= min_level.load(std::memory_order_relaxed);
62+
}};
63+
};
64+
65+
// Kernel logging state. Intentionally leaked to avoid use-after-destroy if logging occurs during
66+
// static destruction.
67+
static KernelLogger& g_kernel_logger()
68+
{
69+
static KernelLogger* p{new KernelLogger{}};
70+
return *p;
71+
}
72+
73+
util::log::Dispatcher& util::log::g_dispatcher()
74+
{
75+
return g_kernel_logger().dispatcher;
76+
}
77+
5778
using kernel::ChainstateRole;
5879
using util::ImmediateTaskRunner;
5980

@@ -203,11 +224,6 @@ constexpr auto LOG_CATEGORIES = [] {
203224
return a;
204225
}();
205226

206-
constexpr BCLog::LogFlags get_bclog_flag(btck_LogCategory category)
207-
{
208-
return LOG_CATEGORIES[category].bclog;
209-
}
210-
211227
btck_LogCategory get_btck_category(BCLog::LogFlags flag)
212228
{
213229
for (size_t i = 0; i < LOG_CATEGORIES.size(); ++i) {
@@ -750,24 +766,9 @@ void btck_txid_destroy(btck_Txid* txid)
750766
delete txid;
751767
}
752768

753-
void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level)
754-
{
755-
LOCK(cs_main);
756-
if (category == btck_LogCategory_ALL) {
757-
LogInstance().SetLogLevel(get_bclog_level(level));
758-
}
759-
760-
LogInstance().AddCategoryLogLevel(get_bclog_flag(category), get_bclog_level(level));
761-
}
762-
763-
void btck_logging_enable_category(btck_LogCategory category)
764-
{
765-
LogInstance().EnableCategory(get_bclog_flag(category));
766-
}
767-
768-
void btck_logging_disable_category(btck_LogCategory category)
769+
void btck_logging_set_min_level(btck_LogLevel level)
769770
{
770-
LogInstance().DisableCategory(get_bclog_flag(category));
771+
g_kernel_logger().min_level.store(get_bclog_level(level));
771772
}
772773

773774
btck_LoggingConnection* btck_logging_connection_create(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)

src/kernel/bitcoinkernel.h

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -764,39 +764,15 @@ BITCOINKERNEL_API void btck_transaction_output_destroy(btck_TransactionOutput* t
764764
///@{
765765

766766
/**
767-
* @brief Set the log level of the global internal logger. This does not
768-
* enable the selected categories. Use @ref btck_logging_enable_category to
769-
* start logging from a specific, or all categories. This changes a global
770-
* setting and will override settings for all existing
771-
* @ref btck_LoggingConnection instances.
767+
* @brief Set the minimum log level. Messages below this level are discarded
768+
* before formatting to avoid overhead.
772769
*
773-
* @param[in] category If btck_LogCategory_ALL is chosen, sets both the global fallback log level
774-
* used by all categories that don't have a specific level set, and also
775-
* sets the log level for messages logged with the btck_LogCategory_ALL category itself.
776-
* For any other category, sets a category-specific log level that overrides
777-
* the global fallback for that category only.
778-
779-
* @param[in] level Log level at which the log category is set.
780-
*/
781-
BITCOINKERNEL_API void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level);
782-
783-
/**
784-
* @brief Enable a specific log category for the global internal logger. This
785-
* changes a global setting and will override settings for all existing @ref
786-
* btck_LoggingConnection instances.
787-
*
788-
* @param[in] category If btck_LogCategory_ALL is chosen, all categories will be enabled.
789-
*/
790-
BITCOINKERNEL_API void btck_logging_enable_category(btck_LogCategory category);
791-
792-
/**
793-
* @brief Disable a specific log category for the global internal logger. This
794-
* changes a global setting and will override settings for all existing @ref
770+
* This changes a global setting and will affect all existing @ref
795771
* btck_LoggingConnection instances.
796772
*
797-
* @param[in] category If btck_LogCategory_ALL is chosen, all categories will be disabled.
773+
* @param[in] level Minimum log level. Messages below this level are not logged.
798774
*/
799-
BITCOINKERNEL_API void btck_logging_disable_category(btck_LogCategory category);
775+
BITCOINKERNEL_API void btck_logging_set_min_level(btck_LogLevel level);
800776

801777
/**
802778
* @brief Start logging messages through the provided callback.

src/kernel/bitcoinkernel_wrapper.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -810,16 +810,6 @@ class Block : public Handle<btck_Block, btck_block_copy, btck_block_destroy>
810810
}
811811
};
812812

813-
inline void logging_set_level_category(LogCategory category, LogLevel level)
814-
{
815-
btck_logging_set_level_category(static_cast<btck_LogCategory>(category), static_cast<btck_LogLevel>(level));
816-
}
817-
818-
inline void logging_enable_category(LogCategory category)
819-
{
820-
btck_logging_enable_category(static_cast<btck_LogCategory>(category));
821-
}
822-
823813
class LogEntry
824814
{
825815
private:
@@ -838,9 +828,9 @@ class LogEntry
838828
LogCategory Category() const { return static_cast<LogCategory>(m_entry->category); }
839829
};
840830

841-
inline void logging_disable_category(LogCategory category)
831+
inline void logging_set_min_level(LogLevel level)
842832
{
843-
btck_logging_disable_category(static_cast<btck_LogCategory>(category));
833+
btck_logging_set_min_level(static_cast<btck_LogLevel>(level));
844834
}
845835

846836
inline std::string_view log_level_get_name(LogLevel level)

src/test/kernel/test_kernel.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,15 +615,10 @@ BOOST_AUTO_TEST_CASE(btck_script_verify_tests)
615615

616616
BOOST_AUTO_TEST_CASE(logging_tests)
617617
{
618-
logging_set_level_category(LogCategory::BENCH, LogLevel::TRACE_LEVEL);
619-
logging_disable_category(LogCategory::BENCH);
620-
logging_enable_category(LogCategory::VALIDATION);
621-
logging_disable_category(LogCategory::VALIDATION);
618+
logging_set_min_level(LogLevel::TRACE_LEVEL);
622619

623620
// Check that connecting, connecting another, and then disconnecting and connecting a logger again works.
624621
{
625-
logging_set_level_category(LogCategory::KERNEL, LogLevel::TRACE_LEVEL);
626-
logging_enable_category(LogCategory::KERNEL);
627622
Logger logger{std::make_unique<TestLog>()};
628623
Logger logger_2{std::make_unique<TestLog>()};
629624
}

0 commit comments

Comments
 (0)