Skip to content

Commit

Permalink
feat: [#639] Tracker Checker. Setup logging
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Feb 1, 2024
1 parent bbfca2c commit 6b74c66
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/console/clients/checker/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;

use anyhow::{Context, Result};
use clap::Parser;
use log::{debug, LevelFilter};

use super::config::Configuration;
use super::console::Console;
Expand All @@ -39,6 +40,8 @@ struct Args {
///
/// Will return an error if the configuration was not provided.
pub async fn run() -> Result<Vec<CheckResult>> {
setup_logging(LevelFilter::Info);

let args = Args::parse();

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

fn setup_logging(level: LevelFilter) {
if let Err(_err) = fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(

Check warning on line 62 in src/console/clients/checker/app.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/app.rs#L59-L62

Added lines #L59 - L62 were not covered by tests
"{} [{}][{}] {}",
chrono::Local::now().format("%+"),
record.target(),
record.level(),

Check warning on line 66 in src/console/clients/checker/app.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/app.rs#L64-L66

Added lines #L64 - L66 were not covered by tests
message
));
})
.level(level)
.chain(std::io::stdout())
.apply()

Check warning on line 72 in src/console/clients/checker/app.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/app.rs#L68-L72

Added lines #L68 - L72 were not covered by tests
{
panic!("Failed to initialize logging.")
}

Check warning on line 75 in src/console/clients/checker/app.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/app.rs#L74-L75

Added lines #L74 - L75 were not covered by tests

debug!("logging initialized.");
}

Check warning on line 78 in src/console/clients/checker/app.rs

View check run for this annotation

Codecov / codecov/patch

src/console/clients/checker/app.rs#L77-L78

Added lines #L77 - L78 were not covered by tests

fn setup_config(args: Args) -> Result<Configuration> {
match (args.config_path, args.config_content) {
(Some(config_path), _) => load_config_from_file(&config_path),
Expand Down
10 changes: 10 additions & 0 deletions src/console/clients/checker/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ impl Service {
self.console.println("UDP trackers ...");

for udp_tracker in &self.config.udp_trackers {
debug!("UDP tracker: {:?}", udp_tracker);

let colored_tracker_url = udp_tracker.to_string().yellow();

let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);

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

debug!("Bind and connect");

let Ok(bound_to) = client.bind_and_connect(ASSIGNED_BY_OS, udp_tracker).await else {
check_results.push(Err(CheckError::UdpError {
socket_addr: *udp_tracker,
Expand All @@ -73,6 +77,8 @@ impl Service {
break;
};

debug!("Send connection request");

let Ok(connection_id) = client.send_connection_request(transaction_id).await else {
check_results.push(Err(CheckError::UdpError {
socket_addr: *udp_tracker,
Expand All @@ -87,6 +93,8 @@ impl Service {

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

debug!("Send announce request");

if (client
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
.await)
Expand All @@ -104,6 +112,8 @@ impl Service {
.println(&format!("{} - Announce at {} is failing", "✗".red(), colored_tracker_url));
}

debug!("Send scrape request");

let info_hashes = vec![InfoHash(hex!("9c38422213e30bff212b30c360d26f9a02136422"))]; // # DevSkim: ignore DS173237

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

0 comments on commit 6b74c66

Please sign in to comment.