From 7da3a09372572b50901607cf3b2577eeb14ae4cd Mon Sep 17 00:00:00 2001 From: erhant Date: Tue, 8 Oct 2024 16:48:25 +0300 Subject: [PATCH] fix log level issues --- Cargo.lock | 6 ++--- Cargo.toml | 2 +- compute/src/main.rs | 37 +++++++++++++++++++++++++++++ compute/src/utils/misc.rs | 48 +++++++++++++++++++++++++++++++++++++ compute/src/utils/mod.rs | 50 ++------------------------------------- 5 files changed, 91 insertions(+), 52 deletions(-) create mode 100644 compute/src/utils/misc.rs diff --git a/Cargo.lock b/Cargo.lock index 402170e..aacbb86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -980,7 +980,7 @@ dependencies = [ [[package]] name = "dkn-compute" -version = "0.2.11" +version = "0.2.12" dependencies = [ "async-trait", "base64 0.22.1", @@ -1014,7 +1014,7 @@ dependencies = [ [[package]] name = "dkn-p2p" -version = "0.2.11" +version = "0.2.12" dependencies = [ "env_logger 0.11.5", "eyre", @@ -1026,7 +1026,7 @@ dependencies = [ [[package]] name = "dkn-workflows" -version = "0.2.11" +version = "0.2.12" dependencies = [ "async-trait", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index c6bb77a..d697194 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ default-members = ["compute"] [workspace.package] edition = "2021" -version = "0.2.11" +version = "0.2.12" license = "Apache-2.0" readme = "README.md" diff --git a/compute/src/main.rs b/compute/src/main.rs index f61e4dd..c00c34c 100644 --- a/compute/src/main.rs +++ b/compute/src/main.rs @@ -7,9 +7,13 @@ use tokio_util::sync::CancellationToken; #[tokio::main] async fn main() -> Result<()> { let dotenv_result = dotenvy::dotenv(); + // TODO: remove me later when the launcher is fixed + amend_log_levels(); + env_logger::builder() .format_timestamp(Some(env_logger::TimestampPrecision::Millis)) .init(); + println!("LOG_LEVEL: {}", env::var("RUST_LOG").unwrap()); if let Err(e) = dotenv_result { log::warn!("Could not load .env file: {}", e); } @@ -142,3 +146,36 @@ async fn wait_for_termination(cancellation: CancellationToken) -> Result<()> { cancellation.cancel(); Ok(()) } + +/// Very CRUDE fix due to launcher log level bug +/// +/// TODO: remove me later when the launcher is fixed +pub fn amend_log_levels() { + if let Ok(rust_log) = std::env::var("RUST_LOG") { + let log_level = if rust_log.contains("dkn_compute=info") { + "info" + } else if rust_log.contains("dkn_compute=debug") { + "debug" + } else if rust_log.contains("dkn_compute=trace") { + "trace" + } else { + return; + }; + + // check if it contains other log levels + let mut new_rust_log = rust_log.clone(); + if !rust_log.contains("dkn_p2p") { + new_rust_log = format!("{},{}={}", new_rust_log, "dkn_p2p", log_level); + } + if !rust_log.contains("dkn_workflows") { + new_rust_log = format!("{},{}={}", new_rust_log, "dkn_workflows", log_level); + } + std::env::set_var("RUST_LOG", new_rust_log); + } else { + // TODO: use env_logger default function instead of this + std::env::set_var( + "RUST_LOG", + "none,dkn_compute=info,dkn_p2p=info,dkn_workflows=info", + ); + } +} diff --git a/compute/src/utils/misc.rs b/compute/src/utils/misc.rs new file mode 100644 index 0000000..1c5fde7 --- /dev/null +++ b/compute/src/utils/misc.rs @@ -0,0 +1,48 @@ +use dkn_p2p::libp2p::{multiaddr::Protocol, Multiaddr}; +use port_check::is_port_reachable; +use std::{ + net::{Ipv4Addr, SocketAddrV4}, + time::{Duration, SystemTime}, +}; + +/// Returns the current time in nanoseconds since the Unix epoch. +/// +/// If a `SystemTimeError` occurs, will return 0 just to keep things running. +#[inline] +pub fn get_current_time_nanos() -> u128 { + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_else(|e| { + log::error!("Error getting current time: {}", e); + Duration::new(0, 0) + }) + .as_nanos() +} + +/// Checks if a given address is already in use locally. +/// This is mostly used to see if the P2P address is already in use. +/// +/// Simply tries to connect with TCP to the given address. +#[inline] +pub fn address_in_use(addr: &Multiaddr) -> bool { + addr.iter() + // find the port within our multiaddr + .find_map(|p| { + if let Protocol::Tcp(port) = p { + Some(port) + } else { + None + } + + // } + }) + // check if its reachable or not + .map(|port| is_port_reachable(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))) + .unwrap_or_else(|| { + log::error!( + "Could not find any TCP port in the given address: {:?}", + addr + ); + false + }) +} diff --git a/compute/src/utils/mod.rs b/compute/src/utils/mod.rs index 8721f91..5002b14 100644 --- a/compute/src/utils/mod.rs +++ b/compute/src/utils/mod.rs @@ -7,51 +7,5 @@ pub use message::DKNMessage; mod available_nodes; pub use available_nodes::AvailableNodes; -use dkn_p2p::libp2p::{multiaddr::Protocol, Multiaddr}; -use port_check::is_port_reachable; -use std::{ - net::{Ipv4Addr, SocketAddrV4}, - time::{Duration, SystemTime}, -}; - -/// Returns the current time in nanoseconds since the Unix epoch. -/// -/// If a `SystemTimeError` occurs, will return 0 just to keep things running. -#[inline] -pub fn get_current_time_nanos() -> u128 { - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap_or_else(|e| { - log::error!("Error getting current time: {}", e); - Duration::new(0, 0) - }) - .as_nanos() -} - -/// Checks if a given address is already in use locally. -/// This is mostly used to see if the P2P address is already in use. -/// -/// Simply tries to connect with TCP to the given address. -#[inline] -pub fn address_in_use(addr: &Multiaddr) -> bool { - addr.iter() - // find the port within our multiaddr - .find_map(|p| { - if let Protocol::Tcp(port) = p { - Some(port) - } else { - None - } - - // } - }) - // check if its reachable or not - .map(|port| is_port_reachable(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port))) - .unwrap_or_else(|| { - log::error!( - "Could not find any TCP port in the given address: {:?}", - addr - ); - false - }) -} +mod misc; +pub use misc::*;