Skip to content

Commit 6b74c66

Browse files
committed
feat: [torrust#639] Tracker Checker. Setup logging
1 parent bbfca2c commit 6b74c66

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/console/clients/checker/app.rs

+24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::sync::Arc;
1717

1818
use anyhow::{Context, Result};
1919
use clap::Parser;
20+
use log::{debug, LevelFilter};
2021

2122
use super::config::Configuration;
2223
use super::console::Console;
@@ -39,6 +40,8 @@ struct Args {
3940
///
4041
/// Will return an error if the configuration was not provided.
4142
pub async fn run() -> Result<Vec<CheckResult>> {
43+
setup_logging(LevelFilter::Info);
44+
4245
let args = Args::parse();
4346

4447
let config = setup_config(args)?;
@@ -53,6 +56,27 @@ pub async fn run() -> Result<Vec<CheckResult>> {
5356
Ok(service.run_checks().await)
5457
}
5558

59+
fn setup_logging(level: LevelFilter) {
60+
if let Err(_err) = fern::Dispatch::new()
61+
.format(|out, message, record| {
62+
out.finish(format_args!(
63+
"{} [{}][{}] {}",
64+
chrono::Local::now().format("%+"),
65+
record.target(),
66+
record.level(),
67+
message
68+
));
69+
})
70+
.level(level)
71+
.chain(std::io::stdout())
72+
.apply()
73+
{
74+
panic!("Failed to initialize logging.")
75+
}
76+
77+
debug!("logging initialized.");
78+
}
79+
5680
fn setup_config(args: Args) -> Result<Configuration> {
5781
match (args.config_path, args.config_content) {
5882
(Some(config_path), _) => load_config_from_file(&config_path),

src/console/clients/checker/service.rs

+10
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,16 @@ impl Service {
5858
self.console.println("UDP trackers ...");
5959

6060
for udp_tracker in &self.config.udp_trackers {
61+
debug!("UDP tracker: {:?}", udp_tracker);
62+
6163
let colored_tracker_url = udp_tracker.to_string().yellow();
6264

6365
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);
6466

6567
let mut client = checker::Client::default();
6668

69+
debug!("Bind and connect");
70+
6771
let Ok(bound_to) = client.bind_and_connect(ASSIGNED_BY_OS, udp_tracker).await else {
6872
check_results.push(Err(CheckError::UdpError {
6973
socket_addr: *udp_tracker,
@@ -73,6 +77,8 @@ impl Service {
7377
break;
7478
};
7579

80+
debug!("Send connection request");
81+
7682
let Ok(connection_id) = client.send_connection_request(transaction_id).await else {
7783
check_results.push(Err(CheckError::UdpError {
7884
socket_addr: *udp_tracker,
@@ -87,6 +93,8 @@ impl Service {
8793

8894
let info_hash = InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422")); // # DevSkim: ignore DS173237
8995

96+
debug!("Send announce request");
97+
9098
if (client
9199
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
92100
.await)
@@ -104,6 +112,8 @@ impl Service {
104112
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
105113
}
106114

115+
debug!("Send scrape request");
116+
107117
let info_hashes = vec![InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422"))]; // # DevSkim: ignore DS173237
108118

109119
if (client.send_scrape_request(connection_id, transaction_id, info_hashes).await).is_ok() {

0 commit comments

Comments
 (0)