Skip to content

Commit 050c44d

Browse files
Squashed 'libbitcoinkernel-sys/bitcoin/' changes from edf99b88e644..20be96ee696b
20be96ee696b kernel: Fix bitcoin-chainstate for windows db5bc2b0968d kernel: Add Purpose section to header documentation e5ddd70c24da kernel: Allowing reducing exports 740f2ef7686e kernel: Add pure kernel bitcoin-chainstate c89b59fc6867 Kernel: Add functions for working with outpoints f82d691fba5c kernel: Add block hash type and block tree utility functions to C header cd70f570ec6d kernel: Add function to read block undo data from disk to C header 301c10f598dc kernel: Add functions to read block from disk to C header 7092fee527a8 kernel: Add function for copying block data to C header 91a51d249f88 kernel: Add functions for the block validation state to C header 8cdd29f3c227 kernel: Add validation interface to C header a4652f7897e2 kernel: Add interrupt function to C header 400048326795 kernel: Add import blocks function to C header d775ba666707 kernel: Add chainstate load options for in-memory dbs in C header d26192373b67 kernel: Add options for reindexing in C header 803e0de7aaa0 kernel: Add block validation to C header 781c7af4e2d0 kernel: Add chainstate loading when instantiating a ChainstateManager f9bf4d9b3136 kernel: Add chainstate manager option for setting worker threads 3270c0615a7e kernel: Add chainstate manager object to C header 0ac5368b3df6 kernel: Add notifications context option to C header b47496af140c kernel: Add chain params context option to C header 92f54852259d kernel: Add kernel library context object 3204110daee4 kernel: Add logging to kernel library C header aba7824242d0 kernel: Introduce initial kernel C header API REVERT: edf99b88e644 kernel: Fix bitcoin-chainstate for windows REVERT: 8fbe027a8a66 kernel: Add Purpose section to header documentation REVERT: 75f44d950566 kernel: Allowing reducing exports REVERT: d3f5df4a38ce kernel: Add pure kernel bitcoin-chainstate REVERT: e5009f3707ee Kernel: Add functions for working with outpoints REVERT: c337b7e609d5 kernel: Add block hash type and block tree utility functions to C header REVERT: 7d3f0ced45f0 kernel: Add function to read block undo data from disk to C header REVERT: 83b7abc0537c kernel: Add functions to read block from disk to C header REVERT: 4c765dddcbec kernel: Add function for copying block data to C header REVERT: ae6570017493 kernel: Add functions for the block validation state to C header REVERT: c2bb6b06af78 kernel: Add validation interface to C header REVERT: 3efd276cedd9 kernel: Add interrupt function to C header REVERT: f0ffab556086 kernel: Add import blocks function to C header REVERT: bdcb7b8b05ab kernel: Add chainstate load options for in-memory dbs in C header REVERT: 6dab7199fe78 kernel: Add options for reindexing in C header REVERT: bee1c1d7363d kernel: Add block validation to C header REVERT: 7e2b237e67d1 kernel: Add chainstate loading when instantiating a ChainstateManager REVERT: e4f1e00203f2 kernel: Add chainstate manager option for setting worker threads REVERT: a7a3138fa2b4 kernel: Add chainstate manager object to C header REVERT: 564e0cf1bf84 kernel: Add notifications context option to C header REVERT: 2ce38f954b86 kernel: Add chain params context option to C header REVERT: 58eae5695813 kernel: Add kernel library context object REVERT: c5f8bd0e0ea2 kernel: Add logging to kernel library C header REVERT: bff9b7f388c7 kernel: Introduce initial kernel C header API git-subtree-dir: libbitcoinkernel-sys/bitcoin git-subtree-split: 20be96ee696ba8c3085da0877156e534a58c39bf
1 parent 3f3e1e7 commit 050c44d

File tree

5 files changed

+68
-73
lines changed

5 files changed

+68
-73
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ int main(int argc, char* argv[])
176176
.always_print_category_levels = true,
177177
};
178178

179-
Logger logger{std::make_unique<KernelLog>(KernelLog{}), logging_options};
179+
logging_set_options(logging_options);
180+
181+
Logger logger{std::make_unique<KernelLog>(KernelLog{})};
180182

181183
ContextOptions options{};
182184
ChainParams params{ChainType::MAINNET};

src/kernel/bitcoinkernel.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,20 @@ struct LoggingConnection {
230230
void* m_user_data;
231231
std::function<void(void* user_data)> m_deleter;
232232

233-
LoggingConnection(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback, const btck_LoggingOptions options)
233+
LoggingConnection(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)
234234
{
235235
LOCK(cs_main);
236236

237-
LogInstance().m_log_timestamps = options.log_timestamps;
238-
LogInstance().m_log_time_micros = options.log_time_micros;
239-
LogInstance().m_log_threadnames = options.log_threadnames;
240-
LogInstance().m_log_sourcelocations = options.log_sourcelocations;
241-
LogInstance().m_always_print_category_level = options.always_print_category_levels;
242-
243237
auto connection{LogInstance().PushBackCallback([callback, user_data](const std::string& str) { callback(user_data, str.c_str(), str.length()); })};
244238

245-
try {
246-
// Only start logging if we just added the connection.
247-
if (LogInstance().NumConnections() == 1 && !LogInstance().StartLogging()) {
248-
LogError("Logger start failed.");
249-
LogInstance().DeleteCallback(connection);
239+
// Only start logging if we just added the connection.
240+
if (LogInstance().NumConnections() == 1 && !LogInstance().StartLogging()) {
241+
LogError("Logger start failed.");
242+
LogInstance().DeleteCallback(connection);
243+
if (user_data && user_data_destroy_callback) {
250244
user_data_destroy_callback(user_data);
251245
}
252-
} catch (std::exception& e) {
253-
LogError("Logger start failed: %s", e.what());
254-
LogInstance().DeleteCallback(connection);
255-
user_data_destroy_callback(user_data);
256-
throw;
246+
throw std::runtime_error("Failed to start logging");
257247
}
258248

259249
m_connection = std::make_unique<std::list<std::function<void(const std::string&)>>::iterator>(connection);
@@ -266,19 +256,20 @@ struct LoggingConnection {
266256
~LoggingConnection()
267257
{
268258
LOCK(cs_main);
259+
LogDebug(BCLog::KERNEL, "Logger disconnecting.");
260+
261+
// Switch back to buffering by calling DisconnectTestLogger if the
262+
// connection that we are about to remove is the last one.
263+
if (LogInstance().NumConnections() == 1) {
264+
LogInstance().DisconnectTestLogger();
265+
} else {
266+
LogInstance().DeleteCallback(*m_connection);
267+
}
269268

270-
LogDebug(BCLog::KERNEL, "Logger disconnected.");
271-
LogInstance().DeleteCallback(*m_connection);
272269
m_connection.reset();
273270
if (m_user_data && m_deleter) {
274271
m_deleter(m_user_data);
275272
}
276-
277-
// Switch back to buffering by calling DisconnectTestLogger if the
278-
// connection that was just removed was the last one.
279-
if (!LogInstance().Enabled()) {
280-
LogInstance().DisconnectTestLogger();
281-
}
282273
}
283274
};
284275

@@ -713,6 +704,16 @@ void btck_txid_destroy(btck_Txid* txid)
713704
delete txid;
714705
}
715706

707+
void btck_logging_set_options(const btck_LoggingOptions options)
708+
{
709+
LOCK(cs_main);
710+
LogInstance().m_log_timestamps = options.log_timestamps;
711+
LogInstance().m_log_time_micros = options.log_time_micros;
712+
LogInstance().m_log_threadnames = options.log_threadnames;
713+
LogInstance().m_log_sourcelocations = options.log_sourcelocations;
714+
LogInstance().m_always_print_category_level = options.always_print_category_levels;
715+
}
716+
716717
void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level)
717718
{
718719
LOCK(cs_main);
@@ -738,12 +739,13 @@ void btck_logging_disable()
738739
LogInstance().DisableLogging();
739740
}
740741

741-
btck_LoggingConnection* btck_logging_connection_create(btck_LogCallback callback,
742-
void* user_data,
743-
btck_DestroyCallback user_data_destroy_callback,
744-
const btck_LoggingOptions options)
742+
btck_LoggingConnection* btck_logging_connection_create(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)
745743
{
746-
return btck_LoggingConnection::create(callback, user_data, user_data_destroy_callback, options);
744+
try {
745+
return btck_LoggingConnection::create(callback, user_data, user_data_destroy_callback);
746+
} catch (const std::exception&) {
747+
return nullptr;
748+
}
747749
}
748750

749751
void btck_logging_connection_destroy(btck_LoggingConnection* connection)

src/kernel/bitcoinkernel.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ BITCOINKERNEL_API btck_Transaction* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transa
483483

484484
/*
485485
* @brief Serializes the transaction through the passed in callback to bytes.
486-
* This is consensus serialization that is also used for the p2p network.
486+
* This is consensus serialization that is also used for the P2P network.
487487
*
488488
* @param[in] transaction Non-null.
489489
* @param[in] writer Non-null, callback to a write bytes function.
@@ -689,20 +689,33 @@ BITCOINKERNEL_API void btck_transaction_output_destroy(btck_TransactionOutput* t
689689
* buffered internally anymore once this is called and the buffer is cleared.
690690
* This function should only be called once and is not thread or re-entry safe.
691691
* Log messages will be buffered until this function is called, or a logging
692-
* connection is created.
692+
* connection is created. This must not be called while a logging connection
693+
* already exists.
693694
*/
694695
BITCOINKERNEL_API void btck_logging_disable();
695696

697+
/**
698+
* @brief Set some options for the global internal logger. This changes global
699+
* settings and will override settings for all existing @ref
700+
* btck_LoggingConnection instances.
701+
*
702+
* @param[in] options Sets formatting options of the log messages.
703+
*/
704+
BITCOINKERNEL_API void btck_logging_set_options(const btck_LoggingOptions options);
705+
696706
/**
697707
* @brief Set the log level of the global internal logger. This does not
698708
* enable the selected categories. Use @ref btck_logging_enable_category to
699709
* start logging from a specific, or all categories. This changes a global
700710
* setting and will override settings for all existing
701711
* @ref btck_LoggingConnection instances.
702712
*
703-
* @param[in] category If btck_LOG_ALL is chosen, all messages at the specified level
704-
* will be logged. Otherwise only messages from the specified category
705-
* will be logged at the specified level and above.
713+
* @param[in] category If btck_LogCategory_ALL is chosen, sets both the global fallback log level
714+
* used by all categories that don't have a specific level set, and also
715+
* sets the log level for messages logged with the btck_LogCategory_ALL category itself.
716+
* For any other category, sets a category-specific log level that overrides
717+
* the global fallback for that category only.
718+
706719
* @param[in] level Log level at which the log category is set.
707720
*/
708721
BITCOINKERNEL_API void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level);
@@ -736,14 +749,12 @@ BITCOINKERNEL_API void btck_logging_disable_category(btck_LogCategory category);
736749
* is also defined it is assumed that ownership of the user_data is passed
737750
* to the created logging connection.
738751
* @param[in] user_data_destroy_callback Nullable, function for freeing the user data.
739-
* @param[in] options Sets formatting options of the log messages.
740-
* @return A new kernel logging connection, or null on error.
752+
* @return A new kernel logging connection.
741753
*/
742754
BITCOINKERNEL_API btck_LoggingConnection* BITCOINKERNEL_WARN_UNUSED_RESULT btck_logging_connection_create(
743755
btck_LogCallback log_callback,
744756
void* user_data,
745-
btck_DestroyCallback user_data_destroy_callback,
746-
const btck_LoggingOptions options) BITCOINKERNEL_ARG_NONNULL(1);
757+
btck_DestroyCallback user_data_destroy_callback) BITCOINKERNEL_ARG_NONNULL(1);
747758

748759
/**
749760
* Stop logging and destroy the logging connection.
@@ -1141,7 +1152,7 @@ BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_ge
11411152

11421153
/*
11431154
* @brief Serializes the block through the passed in callback to bytes.
1144-
* This is consensus serialization that is also used for the p2p network.
1155+
* This is consensus serialization that is also used for the P2P network.
11451156
*
11461157
* @param[in] block Non-null.
11471158
* @param[in] writer Non-null, callback to a write bytes function.

src/kernel/bitcoinkernel_wrapper.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,11 @@ inline void logging_disable()
741741
btck_logging_disable();
742742
}
743743

744+
inline void logging_set_options(const btck_LoggingOptions& logging_options)
745+
{
746+
btck_logging_set_options(logging_options);
747+
}
748+
744749
inline void logging_set_level_category(LogCategory category, LogLevel level)
745750
{
746751
btck_logging_set_level_category(static_cast<btck_LogCategory>(category), static_cast<btck_LogLevel>(level));
@@ -765,12 +770,11 @@ template <Log T>
765770
class Logger : UniqueHandle<btck_LoggingConnection, btck_logging_connection_destroy>
766771
{
767772
public:
768-
Logger(std::unique_ptr<T> log, const btck_LoggingOptions& logging_options)
773+
Logger(std::unique_ptr<T> log)
769774
: UniqueHandle{btck_logging_connection_create(
770775
+[](void* user_data, const char* message, size_t message_len) { static_cast<T*>(user_data)->LogMessage({message, message_len}); },
771776
log.release(),
772-
+[](void* user_data) { delete static_cast<T*>(user_data); },
773-
logging_options)}
777+
+[](void* user_data) { delete static_cast<T*>(user_data); })}
774778
{
775779
}
776780
};

src/test/kernel/test_kernel.cpp

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ BOOST_AUTO_TEST_CASE(logging_tests)
537537
.always_print_category_levels = true,
538538
};
539539

540+
logging_set_options(logging_options);
540541
logging_set_level_category(LogCategory::BENCH, LogLevel::TRACE_LEVEL);
541542
logging_disable_category(LogCategory::BENCH);
542543
logging_enable_category(LogCategory::VALIDATION);
@@ -546,10 +547,10 @@ BOOST_AUTO_TEST_CASE(logging_tests)
546547
{
547548
logging_set_level_category(LogCategory::KERNEL, LogLevel::TRACE_LEVEL);
548549
logging_enable_category(LogCategory::KERNEL);
549-
Logger logger{std::make_unique<TestLog>(TestLog{}), logging_options};
550-
Logger logger_2{std::make_unique<TestLog>(TestLog{}), logging_options};
550+
Logger logger{std::make_unique<TestLog>()};
551+
Logger logger_2{std::make_unique<TestLog>()};
551552
}
552-
Logger logger{std::make_unique<TestLog>(TestLog{}), logging_options};
553+
Logger logger{std::make_unique<TestLog>()};
553554
}
554555

555556
BOOST_AUTO_TEST_CASE(btck_context_tests)
@@ -600,14 +601,7 @@ Context create_context(std::shared_ptr<TestKernelNotifications> notifications, C
600601

601602
BOOST_AUTO_TEST_CASE(btck_chainman_tests)
602603
{
603-
btck_LoggingOptions logging_options = {
604-
.log_timestamps = true,
605-
.log_time_micros = true,
606-
.log_threadnames = false,
607-
.log_sourcelocations = false,
608-
.always_print_category_levels = true,
609-
};
610-
Logger logger{std::make_unique<TestLog>(TestLog{}), logging_options};
604+
Logger logger{std::make_unique<TestLog>(TestLog{})};
611605
auto test_directory{TestDirectory{"chainman_test_bitcoin_kernel"}};
612606

613607
{ // test with default context
@@ -665,15 +659,6 @@ std::unique_ptr<ChainMan> create_chainman(TestDirectory& test_directory,
665659

666660
void chainman_reindex_test(TestDirectory& test_directory)
667661
{
668-
btck_LoggingOptions logging_options = {
669-
.log_timestamps = true,
670-
.log_time_micros = true,
671-
.log_threadnames = false,
672-
.log_sourcelocations = false,
673-
.always_print_category_levels = true,
674-
};
675-
Logger logger{std::make_unique<TestLog>(TestLog{}), logging_options};
676-
677662
auto mainnet_test_directory{TestDirectory{"mainnet_test_bitcoin_kernel"}};
678663

679664
auto notifications{std::make_shared<TestKernelNotifications>()};
@@ -797,15 +782,6 @@ void chainman_mainnet_validation_test(TestDirectory& test_directory)
797782

798783
BOOST_AUTO_TEST_CASE(btck_chainman_mainnet_tests)
799784
{
800-
btck_LoggingOptions logging_options = {
801-
.log_timestamps = true,
802-
.log_time_micros = true,
803-
.log_threadnames = false,
804-
.log_sourcelocations = false,
805-
.always_print_category_levels = true,
806-
};
807-
Logger logger{std::make_unique<TestLog>(TestLog{}), logging_options};
808-
809785
auto test_directory{TestDirectory{"mainnet_test_bitcoin_kernel"}};
810786
chainman_mainnet_validation_test(test_directory);
811787
chainman_reindex_test(test_directory);

0 commit comments

Comments
 (0)