Skip to content

Commit

Permalink
fix log level issues
Browse files Browse the repository at this point in the history
  • Loading branch information
erhant committed Oct 8, 2024
1 parent 1abbc36 commit 7da3a09
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 52 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
37 changes: 37 additions & 0 deletions compute/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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",
);
}
}
48 changes: 48 additions & 0 deletions compute/src/utils/misc.rs
Original file line number Diff line number Diff line change
@@ -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
})
}
50 changes: 2 additions & 48 deletions compute/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

0 comments on commit 7da3a09

Please sign in to comment.