From: henghonglee Date: Tue, 8 Aug 2023 04:01:11 +0000 (+0800) Subject: Pass Record by value to Logger X-Git-Tag: v0.0.119~35^2~8 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=0cba31fd38615c8608960f87bdd9637b712904a1;p=rust-lightning Pass Record by value to Logger Instead of passing a reference to a Record, pass the Logger an owned Record so that it can be decorated with semantic context. --- diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 57c78e76d..725f83af9 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -728,7 +728,7 @@ mod tests { pub lines: Mutex>, } impl Logger for TrackingLogger { - fn log(&self, record: &Record) { + fn log(&self, record: Record) { *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); } diff --git a/fuzz/src/onion_message.rs b/fuzz/src/onion_message.rs index 13de5005c..54d15324f 100644 --- a/fuzz/src/onion_message.rs +++ b/fuzz/src/onion_message.rs @@ -218,7 +218,7 @@ mod tests { pub lines: Mutex>, } impl Logger for TrackingLogger { - fn log(&self, record: &Record) { + fn log(&self, record: Record) { *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); } diff --git a/fuzz/src/utils/test_logger.rs b/fuzz/src/utils/test_logger.rs index f8c96f99b..5e5817e23 100644 --- a/fuzz/src/utils/test_logger.rs +++ b/fuzz/src/utils/test_logger.rs @@ -56,7 +56,7 @@ impl<'a, Out: Output> Write for LockedWriteAdapter<'a, Out> { } impl Logger for TestLogger { - fn log(&self, record: &Record) { + fn log(&self, record: Record) { write!(LockedWriteAdapter(&self.out), "{:<5} {} [{} : {}] {}\n", record.level.to_string(), self.id, record.module_path, record.line, record.args) .unwrap(); diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index d4f75dd6c..d02f23fdd 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -571,7 +571,7 @@ mod tests { pub struct TestLogger(); impl lightning::util::logger::Logger for TestLogger { - fn log(&self, record: &lightning::util::logger::Record) { + fn log(&self, record: lightning::util::logger::Record) { println!("{:<5} [{} : {}, {}] {}", record.level.to_string(), record.module_path, record.file, record.line, record.args); } } diff --git a/lightning-rapid-gossip-sync/src/lib.rs b/lightning-rapid-gossip-sync/src/lib.rs index 5a61be799..c15eedabb 100644 --- a/lightning-rapid-gossip-sync/src/lib.rs +++ b/lightning-rapid-gossip-sync/src/lib.rs @@ -49,7 +49,7 @@ //! # use lightning::util::logger::{Logger, Record}; //! # struct FakeLogger {} //! # impl Logger for FakeLogger { -//! # fn log(&self, record: &Record) { } +//! # fn log(&self, record: Record) { } //! # } //! # let logger = FakeLogger {}; //! diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 0e68d0914..0e88b1932 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -71,7 +71,7 @@ use crate::prelude::*; /// # use std::sync::Arc; /// # struct FakeLogger; /// # impl Logger for FakeLogger { -/// # fn log(&self, record: &Record) { unimplemented!() } +/// # fn log(&self, record: Record) { unimplemented!() } /// # } /// # struct FakeMessageRouter {} /// # impl MessageRouter for FakeMessageRouter { diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index a2d9f51a3..2d8dd0d67 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -8116,7 +8116,7 @@ pub mod benches { struct DummyLogger {} impl Logger for DummyLogger { - fn log(&self, _record: &Record) {} + fn log(&self, _record: Record) {} } pub fn generate_routes_with_zero_penalty_scorer(bench: &mut Criterion) { diff --git a/lightning/src/routing/scoring.rs b/lightning/src/routing/scoring.rs index 9c907c3f7..de40b295e 100644 --- a/lightning/src/routing/scoring.rs +++ b/lightning/src/routing/scoring.rs @@ -26,7 +26,7 @@ //! # //! # struct FakeLogger {}; //! # impl Logger for FakeLogger { -//! # fn log(&self, record: &Record) { unimplemented!() } +//! # fn log(&self, record: Record) { unimplemented!() } //! # } //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph<&FakeLogger>) { //! # let logger = FakeLogger {}; diff --git a/lightning/src/util/logger.rs b/lightning/src/util/logger.rs index dbca9b785..4534c9dd8 100644 --- a/lightning/src/util/logger.rs +++ b/lightning/src/util/logger.rs @@ -135,10 +135,10 @@ impl<'a> Record<'a> { } } -/// A trait encapsulating the operations required of a logger +/// A trait encapsulating the operations required of a logger. pub trait Logger { - /// Logs the `Record` - fn log(&self, record: &Record); + /// Logs the [`Record`]. + fn log(&self, record: Record); } /// Wrapper for logging a [`PublicKey`] in hex format. diff --git a/lightning/src/util/macro_logger.rs b/lightning/src/util/macro_logger.rs index 4836b4d68..f1d4fe540 100644 --- a/lightning/src/util/macro_logger.rs +++ b/lightning/src/util/macro_logger.rs @@ -159,7 +159,7 @@ macro_rules! log_spendable { #[macro_export] macro_rules! log_internal { ($logger: expr, $lvl:expr, $($arg:tt)+) => ( - $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())) + $logger.log($crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!())) ); } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index e5c635023..2c34bc92c 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -979,7 +979,7 @@ impl TestLogger { } impl Logger for TestLogger { - fn log(&self, record: &Record) { + fn log(&self, record: Record) { *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1; if record.level >= self.level { #[cfg(all(not(ldk_bench), feature = "std"))] {