Skip to content

Commit 68681ee

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 0d7a3ee commit 68681ee

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/task_runner.h>
@@ -38,6 +38,7 @@
3838
#include <validationinterface.h>
3939

4040
#include <array>
41+
#include <atomic>
4142
#include <cassert>
4243
#include <cstddef>
4344
#include <cstring>
@@ -53,6 +54,26 @@
5354
#include <utility>
5455
#include <vector>
5556

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

@@ -202,11 +223,6 @@ constexpr auto LOG_CATEGORIES = [] {
202223
return a;
203224
}();
204225

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

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

772773
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)