Skip to content

Commit 6736566

Browse files
author
Antoine Riard
committed
Logging interface
Implement error, warn, info, debug and trace macros, internally calling an instance of Logger, and passing it to every main structures Issue #54
1 parent fb4be8a commit 6736566

File tree

7 files changed

+591
-12
lines changed

7 files changed

+591
-12
lines changed

src/chain/chaininterface.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use bitcoin::blockdata::block::{Block, BlockHeader};
22
use bitcoin::blockdata::transaction::Transaction;
33
use bitcoin::blockdata::script::Script;
44
use bitcoin::util::hash::Sha256dHash;
5-
use std::sync::{Mutex,Weak,MutexGuard};
5+
use util::logger::Logger;
6+
use std::sync::{Mutex,Weak,MutexGuard,Arc};
67
use std::sync::atomic::{AtomicUsize, Ordering};
78

89
/// An interface to request notification of certain scripts as they appear the
@@ -70,7 +71,8 @@ pub trait FeeEstimator: Sync + Send {
7071
pub struct ChainWatchInterfaceUtil {
7172
watched: Mutex<(Vec<Script>, Vec<(Sha256dHash, u32)>, bool)>, //TODO: Something clever to optimize this
7273
listeners: Mutex<Vec<Weak<ChainListener>>>,
73-
reentered: AtomicUsize
74+
reentered: AtomicUsize,
75+
logger: Arc<Logger>,
7476
}
7577

7678
/// Register listener
@@ -100,11 +102,12 @@ impl ChainWatchInterface for ChainWatchInterfaceUtil {
100102
}
101103

102104
impl ChainWatchInterfaceUtil {
103-
pub fn new() -> ChainWatchInterfaceUtil {
105+
pub fn new(logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
104106
ChainWatchInterfaceUtil {
105107
watched: Mutex::new((Vec::new(), Vec::new(), false)),
106108
listeners: Mutex::new(Vec::new()),
107-
reentered: AtomicUsize::new(1)
109+
reentered: AtomicUsize::new(1),
110+
logger: logger,
108111
}
109112
}
110113

src/ln/channel.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
2323
use chain::transaction::OutPoint;
2424
use util::{transaction_utils,rng};
2525
use util::sha2::Sha256;
26+
use util::logger::Logger;
2627

2728
use std::default::Default;
2829
use std::{cmp,mem};
2930
use std::time::Instant;
31+
use std::sync::{Arc};
3032

3133
pub struct ChannelKeys {
3234
pub funding_key: SecretKey,
@@ -302,6 +304,8 @@ pub struct Channel {
302304
their_shutdown_scriptpubkey: Option<Script>,
303305

304306
channel_monitor: ChannelMonitor,
307+
308+
logger: Arc<Logger>,
305309
}
306310

307311
const OUR_MAX_HTLCS: u16 = 5; //TODO
@@ -360,7 +364,7 @@ impl Channel {
360364
// Constructors:
361365

362366
/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`
363-
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64) -> Channel {
367+
pub fn new_outbound(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, channel_value_satoshis: u64, announce_publicly: bool, user_id: u64, logger: Arc<Logger>) -> Channel {
364368
if channel_value_satoshis >= MAX_FUNDING_SATOSHIS {
365369
panic!("funding value > 2^24");
366370
}
@@ -428,6 +432,8 @@ impl Channel {
428432
their_shutdown_scriptpubkey: None,
429433

430434
channel_monitor: channel_monitor,
435+
436+
logger,
431437
}
432438
}
433439

@@ -445,7 +451,7 @@ impl Channel {
445451
/// Assumes chain_hash has already been checked and corresponds with what we expect!
446452
/// Generally prefers to take the DisconnectPeer action on failure, as a notice to the sender
447453
/// that we're rejecting the new channel.
448-
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, announce_publicly: bool) -> Result<Channel, HandleError> {
454+
pub fn new_from_req(fee_estimator: &FeeEstimator, chan_keys: ChannelKeys, their_node_id: PublicKey, msg: &msgs::OpenChannel, user_id: u64, announce_publicly: bool, logger: Arc<Logger>) -> Result<Channel, HandleError> {
449455
// Check sanity of message fields:
450456
if msg.funding_satoshis >= MAX_FUNDING_SATOSHIS {
451457
return Err(HandleError{err: "funding value > 2^24", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
@@ -541,6 +547,8 @@ impl Channel {
541547
their_shutdown_scriptpubkey: None,
542548

543549
channel_monitor: channel_monitor,
550+
551+
logger,
544552
};
545553

546554
let obscure_factor = chan.get_commitment_transaction_number_obscure_factor();

src/ln/channelmanager.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use ln::msgs;
1919
use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable};
2020
use util::{byte_utils, events, internal_traits, rng};
2121
use util::sha2::Sha256;
22+
use util::logger::Logger;
2223

2324
use crypto;
2425
use crypto::mac::{Mac,MacResult};
@@ -159,6 +160,8 @@ pub struct ChannelManager {
159160
our_network_key: SecretKey,
160161

161162
pending_events: Mutex<Vec<events::Event>>,
163+
164+
logger: Arc<Logger>,
162165
}
163166

164167
const CLTV_EXPIRY_DELTA: u16 = 6 * 24 * 2; //TODO?
@@ -204,7 +207,7 @@ impl ChannelManager {
204207
/// fee_proportional_millionths is an optional fee to charge any payments routed through us.
205208
/// Non-proportional fees are fixed according to our risk using the provided fee estimator.
206209
/// panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
207-
pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
210+
pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>, tx_broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
208211
let secp_ctx = Secp256k1::new();
209212

210213
let res = Arc::new(ChannelManager {
@@ -229,6 +232,8 @@ impl ChannelManager {
229232
our_network_key,
230233

231234
pending_events: Mutex::new(Vec::new()),
235+
236+
logger,
232237
});
233238
let weak_res = Arc::downgrade(&res);
234239
res.chain_monitor.register_listener(weak_res);
@@ -263,7 +268,7 @@ impl ChannelManager {
263268
}
264269
};
265270

266-
let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, self.announce_channels_publicly, user_id);
271+
let channel = Channel::new_outbound(&*self.fee_estimator, chan_keys, their_network_key, channel_value_satoshis, self.announce_channels_publicly, user_id, Arc::clone(&self.logger));
267272
let res = channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator)?;
268273
let mut channel_state = self.channel_state.lock().unwrap();
269274
match channel_state.by_id.insert(channel.channel_id(), channel) {
@@ -1185,7 +1190,7 @@ impl ChannelMessageHandler for ChannelManager {
11851190
}
11861191
};
11871192

1188-
let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, self.announce_channels_publicly)?;
1193+
let channel = Channel::new_from_req(&*self.fee_estimator, chan_keys, their_node_id.clone(), msg, 0, self.announce_channels_publicly, Arc::clone(&self.logger))?;
11891194
let accept_msg = channel.get_accept_channel()?;
11901195
channel_state.by_id.insert(channel.channel_id(), channel);
11911196
Ok(accept_msg)

src/ln/peer_handler.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use ln::msgs::{MsgEncodable,MsgDecodable};
55
use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
66
use util::byte_utils;
77
use util::events::{EventsProvider,Event};
8+
use util::logger::Logger;
89

910
use std::collections::{HashMap,LinkedList};
1011
use std::sync::{Arc, Mutex};
@@ -96,6 +97,7 @@ pub struct PeerManager<Descriptor: SocketDescriptor> {
9697
pending_events: Mutex<Vec<Event>>,
9798
our_node_secret: SecretKey,
9899
initial_syncs_sent: AtomicUsize,
100+
logger: Arc<Logger>,
99101
}
100102

101103

@@ -117,13 +119,14 @@ const INITIAL_SYNCS_TO_SEND: usize = 5;
117119
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
118120
/// PeerIds may repeat, but only after disconnect_event() has been called.
119121
impl<Descriptor: SocketDescriptor> PeerManager<Descriptor> {
120-
pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey) -> PeerManager<Descriptor> {
122+
pub fn new(message_handler: MessageHandler, our_node_secret: SecretKey, logger: Arc<Logger>) -> PeerManager<Descriptor> {
121123
PeerManager {
122124
message_handler: message_handler,
123125
peers: Mutex::new(PeerHolder { peers: HashMap::new(), node_id_to_descriptor: HashMap::new() }),
124126
pending_events: Mutex::new(Vec::new()),
125127
our_node_secret: our_node_secret,
126128
initial_syncs_sent: AtomicUsize::new(0),
129+
logger,
127130
}
128131
}
129132

src/ln/router.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use bitcoin::util::hash::Sha256dHash;
66
use ln::channelmanager;
77
use ln::msgs::{ErrorAction,HandleError,RoutingMessageHandler,MsgEncodable,NetAddress,GlobalFeatures};
88
use ln::msgs;
9+
use util::logger::Logger;
910

1011
use std::cmp;
11-
use std::sync::RwLock;
12+
use std::sync::{RwLock,Arc};
1213
use std::collections::{HashMap,BinaryHeap};
1314
use std::collections::hash_map::Entry;
1415

@@ -105,6 +106,7 @@ pub struct RouteHint {
105106
pub struct Router {
106107
secp_ctx: Secp256k1,
107108
network_map: RwLock<NetworkMap>,
109+
logger: Arc<Logger>,
108110
}
109111

110112
macro_rules! secp_verify_sig {
@@ -325,7 +327,7 @@ struct DummyDirectionalChannelInfo {
325327
}
326328

327329
impl Router {
328-
pub fn new(our_pubkey: PublicKey) -> Router {
330+
pub fn new(our_pubkey: PublicKey, logger: Arc<Logger>) -> Router {
329331
let mut nodes = HashMap::new();
330332
nodes.insert(our_pubkey.clone(), NodeInfo {
331333
channels: Vec::new(),
@@ -344,6 +346,7 @@ impl Router {
344346
our_node_id: our_pubkey,
345347
nodes: nodes,
346348
}),
349+
logger,
347350
}
348351
}
349352

0 commit comments

Comments
 (0)