diff --git a/Cargo.lock b/Cargo.lock --- a/Cargo.lock +++ b/Cargo.lock @@ -213,10 +213,19 @@ dependencies = [ "abc-rust-lint", "chronik-bridge", + "chronik-util", "cxx", "cxx-build", ] +[[package]] +name = "chronik-util" +version = "0.1.0" +dependencies = [ + "abc-rust-lint", + "chronik-bridge", +] + [[package]] name = "clang-sys" version = "1.3.3" diff --git a/Cargo.toml b/Cargo.toml --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ "chronik/chronik-bridge", "chronik/chronik-db", "chronik/chronik-lib", + "chronik/chronik-util", ] diff --git a/chronik/chronik-bridge/src/ffi.rs b/chronik/chronik-bridge/src/ffi.rs --- a/chronik/chronik-bridge/src/ffi.rs +++ b/chronik/chronik-bridge/src/ffi.rs @@ -13,6 +13,20 @@ include!("chronik-cpp/chronik_bridge.h"); /// Print the message to bitcoind's logs. - fn log_println(msg: &str); + fn log_print( + logging_function: &str, + source_file: &str, + source_line: u32, + msg: &str, + ); + + /// Print the message to bitcoind's logs under the BCLog::Chronik + /// category. + fn log_print_chronik( + logging_function: &str, + source_file: &str, + source_line: u32, + msg: &str, + ); } } diff --git a/chronik/chronik-cpp/chronik_bridge.h b/chronik/chronik-cpp/chronik_bridge.h --- a/chronik/chronik-cpp/chronik_bridge.h +++ b/chronik/chronik-cpp/chronik_bridge.h @@ -10,7 +10,12 @@ namespace chronik_bridge { -void log_println(rust::Str msg); +void log_print(const rust::Str logging_function, const rust::Str source_file, + const uint32_t source_line, const rust::Str msg); + +void log_print_chronik(const rust::Str logging_function, + const rust::Str source_file, const uint32_t source_line, + const rust::Str msg); } // namespace chronik_bridge diff --git a/chronik/chronik-cpp/chronik_bridge.cpp b/chronik/chronik-cpp/chronik_bridge.cpp --- a/chronik/chronik-cpp/chronik_bridge.cpp +++ b/chronik/chronik-cpp/chronik_bridge.cpp @@ -7,8 +7,18 @@ namespace chronik_bridge { -void log_println(rust::Str msg) { - LogPrintf("%s\n", std::string(msg)); +void log_print(const rust::Str logging_function, const rust::Str source_file, + const uint32_t source_line, const rust::Str msg) { + LogInstance().LogPrintStr(std::string(msg), std::string(logging_function), + std::string(source_file), source_line); +} + +void log_print_chronik(const rust::Str logging_function, + const rust::Str source_file, const uint32_t source_line, + const rust::Str msg) { + if (LogInstance().WillLogCategory(BCLog::CHRONIK)) { + log_print(logging_function, source_file, source_line, msg); + } } } // namespace chronik_bridge diff --git a/chronik/chronik-lib/Cargo.toml b/chronik/chronik-lib/Cargo.toml --- a/chronik/chronik-lib/Cargo.toml +++ b/chronik/chronik-lib/Cargo.toml @@ -14,6 +14,7 @@ [dependencies] abc-rust-lint = { path = "../abc-rust-lint" } chronik-bridge = { path = "../chronik-bridge" } +chronik-util = { path = "../chronik-util" } # Bridge to C++ cxx = "1.0" diff --git a/chronik/chronik-lib/src/bridge.rs b/chronik/chronik-lib/src/bridge.rs --- a/chronik/chronik-lib/src/bridge.rs +++ b/chronik/chronik-lib/src/bridge.rs @@ -4,7 +4,13 @@ //! Rust side of the bridge; these structs and functions are exposed to C++. +use chronik_util::{log, log_chronik}; + /// Setup the Chronik bridge. Currently only logs to bitcoind. pub fn setup_bridge() { - chronik_bridge::ffi::log_println("Starting Chronik..."); + log!("Starting Chronik...\n"); + log_chronik!( + "Note: Chronik is not implemented yet. These logs are just for \ + testing.\n" + ); } diff --git a/chronik/chronik-lib/Cargo.toml b/chronik/chronik-util/Cargo.toml copy from chronik/chronik-lib/Cargo.toml copy to chronik/chronik-util/Cargo.toml --- a/chronik/chronik-lib/Cargo.toml +++ b/chronik/chronik-util/Cargo.toml @@ -1,23 +1,12 @@ # Copyright (c) 2022 The Bitcoin developers [package] -name = "chronik-lib" +name = "chronik-util" version = "0.1.0" edition = "2021" rust-version = "1.61.0" license = "MIT" -[lib] -# Compile to C-ABI -crate-type = ["staticlib"] - [dependencies] abc-rust-lint = { path = "../abc-rust-lint" } chronik-bridge = { path = "../chronik-bridge" } - -# Bridge to C++ -cxx = "1.0" - -[build-dependencies] -# Build scripts for the cxx crate -cxx-build = "1.0" diff --git a/chronik/chronik-util/src/lib.rs b/chronik/chronik-util/src/lib.rs new file mode 100644 --- /dev/null +++ b/chronik/chronik-util/src/lib.rs @@ -0,0 +1,11 @@ +// Copyright (c) 2022 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +//! Utilities for Chronik, such as logging. + +abc_rust_lint::lint! { + mod log; + + pub use crate::log::*; +} diff --git a/chronik/chronik-util/src/log.rs b/chronik/chronik-util/src/log.rs new file mode 100644 --- /dev/null +++ b/chronik/chronik-util/src/log.rs @@ -0,0 +1,44 @@ +// Copyright (c) 2022 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +//! Macros for logging messages to bitcoind + +pub use chronik_bridge::ffi::{log_print, log_print_chronik}; + +/// Logs the message to bitcoind's logging system: +/// +/// ```ignore +/// let world = "world"; +/// log!("Hello {}!\n", world); +/// ``` +#[macro_export] +macro_rules! log { + ($($arg:tt)*) => { + chronik_util::log_print( + "", + file!(), + line!(), + &format!($($arg)*), + ); + }; +} + +/// Logs the message to bitcoind's logging system under the BCLog::Chronik +/// category: +/// +/// ```ignore +/// let world = "world"; +/// log_chronik!("Hello {}!\n", world); +/// ``` +#[macro_export] +macro_rules! log_chronik { + ($($arg:tt)*) => { + chronik_util::log_print_chronik( + "", + file!(), + line!(), + &format!($($arg)*), + ); + }; +} diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -60,6 +60,7 @@ VALIDATION = (1 << 21), AVALANCHE = (1 << 22), I2P = (1 << 23), + CHRONIK = (1 << 24), ALL = ~uint32_t(0), }; diff --git a/src/logging.cpp b/src/logging.cpp --- a/src/logging.cpp +++ b/src/logging.cpp @@ -124,6 +124,7 @@ {BCLog::VALIDATION, "validation"}, {BCLog::AVALANCHE, "avalanche"}, {BCLog::I2P, "i2p"}, + {BCLog::CHRONIK, "chronik"}, {BCLog::ALL, "1"}, {BCLog::ALL, "all"}, };