kernel: Add logging to kernel library C header

Exposing logging in the kernel library allows users to follow
operations. Users of the C header can use
`kernel_logging_connection_create(...)` to pass a callback function to
Bitcoin Core's internal logger. Additionally the level and category can
be globally configured.

By default, the logger buffers messages until
`kernel_loggin_connection_create(...)` is called. If the user does not
want any logging messages, it is recommended that
`kernel_disable_logging()` is called, which permanently disables the
logging and any buffering of messages.

Co-authored-by: stringintech <stringintech@gmail.com>
This commit is contained in:
TheCharlatan
2024-05-29 16:13:45 +02:00
parent 2cf136dec4
commit 28d679bad9
6 changed files with 440 additions and 0 deletions

View File

@@ -12,8 +12,10 @@
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <ranges>
#include <span>
#include <string_view>
#include <vector>
using namespace btck;
@@ -49,6 +51,15 @@ void check_equal(std::span<const std::byte> _actual, std::span<const std::byte>
expected.begin(), expected.end());
}
class TestLog
{
public:
void LogMessage(std::string_view message)
{
std::cout << "kernel: " << message;
}
};
void run_verify_test(
const ScriptPubkey& spent_script_pubkey,
const Transaction& spending_tx,
@@ -344,3 +355,29 @@ BOOST_AUTO_TEST_CASE(btck_script_verify_tests)
/*input_index*/ 0,
/*is_taproot*/ true);
}
BOOST_AUTO_TEST_CASE(logging_tests)
{
btck_LoggingOptions logging_options = {
.log_timestamps = true,
.log_time_micros = true,
.log_threadnames = false,
.log_sourcelocations = false,
.always_print_category_levels = true,
};
logging_set_options(logging_options);
logging_set_level_category(LogCategory::BENCH, LogLevel::TRACE_LEVEL);
logging_disable_category(LogCategory::BENCH);
logging_enable_category(LogCategory::VALIDATION);
logging_disable_category(LogCategory::VALIDATION);
// Check that connecting, connecting another, and then disconnecting and connecting a logger again works.
{
logging_set_level_category(LogCategory::KERNEL, LogLevel::TRACE_LEVEL);
logging_enable_category(LogCategory::KERNEL);
Logger logger{std::make_unique<TestLog>()};
Logger logger_2{std::make_unique<TestLog>()};
}
Logger logger{std::make_unique<TestLog>()};
}