bitcoind_watcher/
lib.rs

1// Helpful source: https://towardsdatascience.com/understand-your-comp&uter-system-using-logs-98139d0b5de1
2extern crate logwatcher;
3use bitcoind_log_parser;
4use bitcoind_log_parser::LogMessage;
5use logwatcher::LogWatcher;
6use logwatcher::LogWatcherAction;
7
8pub use bitcoind_log_parser::LogLine;
9
10type Callback = Box<dyn Fn(LogLine) + 'static>;
11
12pub struct Config {
13    log_path: String,
14}
15pub struct BitcoindWatcher {
16    config: Config,
17    transaction_added_to_mempool_callback: Option<Callback>,
18    new_proof_of_work_valid_block: Option<Callback>,
19    new_outbound_peer_connected: Option<Callback>,
20}
21
22impl BitcoindWatcher {
23    pub fn run(&self) {
24        let transaction_added_to_mempool_callback_ref =
25            self.transaction_added_to_mempool_callback.as_ref();
26        let new_proof_of_work_valid_block_ref = self.new_proof_of_work_valid_block.as_ref();
27        let new_outbound_peer_connected_ref = self.new_outbound_peer_connected.as_ref();
28
29        let file_to_watch = &self.config.log_path;
30        let mut log_watcher = LogWatcher::register(file_to_watch.to_string()).unwrap();
31        log_watcher.watch(&mut move |line: String| {
32            let log_line: LogLine = bitcoind_log_parser::parse(&line).unwrap();
33            match &log_line.message {
34                LogMessage::NewOutboundPeerConnected(_) => match new_outbound_peer_connected_ref {
35                    Some(ref callback) => callback(log_line),
36                    None => println!("no callback"),
37                },
38                LogMessage::TransactionAddedToMempool(tatmp) => {
39                    match transaction_added_to_mempool_callback_ref {
40                        Some(ref callback) => callback(log_line),
41                        None => (),
42                    }
43                    //println!("{:#?}", &log_line);
44                    //println!("{:#?}", tatmp.txid);
45                }
46                LogMessage::NewPoWValidBlock(npowvbm) => {
47                    match transaction_added_to_mempool_callback_ref {
48                        Some(ref callback) => callback(log_line),
49                        None => (),
50                    }
51                    //println!("{:#?}", &log_line);
52                }
53                LogMessage::Unknown { raw: _raw } => {
54                    //println!("{:#?}", &log_line);
55                }
56                _ => {
57                    //println!("{}", line)
58                }
59            }
60            LogWatcherAction::None
61        });
62    }
63    pub fn new(log_path: &str) -> Self {
64        let config = Config {
65            log_path: log_path.to_string(),
66        };
67        BitcoindWatcher {
68            config,
69            transaction_added_to_mempool_callback: None,
70            new_proof_of_work_valid_block: None,
71            new_outbound_peer_connected: None,
72        }
73    }
74    pub fn on_transaction_added_to_mempool(mut self, callback: Callback) -> Self {
75        self.transaction_added_to_mempool_callback = Some(callback);
76        self
77    }
78    pub fn on_new_proof_of_work_valid_block(mut self, callback: Callback) -> Self {
79        self.new_proof_of_work_valid_block = Some(callback);
80        self
81    }
82    pub fn on_new_outbound_peer_connected(mut self, callback: Callback) -> Self {
83        self.new_outbound_peer_connected = Some(callback);
84        self
85    }
86}