Skip to content

Commit 7da3a09

Browse files
committed
fix log level issues
1 parent 1abbc36 commit 7da3a09

File tree

5 files changed

+91
-52
lines changed

5 files changed

+91
-52
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ default-members = ["compute"]
77

88
[workspace.package]
99
edition = "2021"
10-
version = "0.2.11"
10+
version = "0.2.12"
1111
license = "Apache-2.0"
1212
readme = "README.md"
1313

compute/src/main.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ use tokio_util::sync::CancellationToken;
77
#[tokio::main]
88
async fn main() -> Result<()> {
99
let dotenv_result = dotenvy::dotenv();
10+
// TODO: remove me later when the launcher is fixed
11+
amend_log_levels();
12+
1013
env_logger::builder()
1114
.format_timestamp(Some(env_logger::TimestampPrecision::Millis))
1215
.init();
16+
println!("LOG_LEVEL: {}", env::var("RUST_LOG").unwrap());
1317
if let Err(e) = dotenv_result {
1418
log::warn!("Could not load .env file: {}", e);
1519
}
@@ -142,3 +146,36 @@ async fn wait_for_termination(cancellation: CancellationToken) -> Result<()> {
142146
cancellation.cancel();
143147
Ok(())
144148
}
149+
150+
/// Very CRUDE fix due to launcher log level bug
151+
///
152+
/// TODO: remove me later when the launcher is fixed
153+
pub fn amend_log_levels() {
154+
if let Ok(rust_log) = std::env::var("RUST_LOG") {
155+
let log_level = if rust_log.contains("dkn_compute=info") {
156+
"info"
157+
} else if rust_log.contains("dkn_compute=debug") {
158+
"debug"
159+
} else if rust_log.contains("dkn_compute=trace") {
160+
"trace"
161+
} else {
162+
return;
163+
};
164+
165+
// check if it contains other log levels
166+
let mut new_rust_log = rust_log.clone();
167+
if !rust_log.contains("dkn_p2p") {
168+
new_rust_log = format!("{},{}={}", new_rust_log, "dkn_p2p", log_level);
169+
}
170+
if !rust_log.contains("dkn_workflows") {
171+
new_rust_log = format!("{},{}={}", new_rust_log, "dkn_workflows", log_level);
172+
}
173+
std::env::set_var("RUST_LOG", new_rust_log);
174+
} else {
175+
// TODO: use env_logger default function instead of this
176+
std::env::set_var(
177+
"RUST_LOG",
178+
"none,dkn_compute=info,dkn_p2p=info,dkn_workflows=info",
179+
);
180+
}
181+
}

compute/src/utils/misc.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use dkn_p2p::libp2p::{multiaddr::Protocol, Multiaddr};
2+
use port_check::is_port_reachable;
3+
use std::{
4+
net::{Ipv4Addr, SocketAddrV4},
5+
time::{Duration, SystemTime},
6+
};
7+
8+
/// Returns the current time in nanoseconds since the Unix epoch.
9+
///
10+
/// If a `SystemTimeError` occurs, will return 0 just to keep things running.
11+
#[inline]
12+
pub fn get_current_time_nanos() -> u128 {
13+
SystemTime::now()
14+
.duration_since(SystemTime::UNIX_EPOCH)
15+
.unwrap_or_else(|e| {
16+
log::error!("Error getting current time: {}", e);
17+
Duration::new(0, 0)
18+
})
19+
.as_nanos()
20+
}
21+
22+
/// Checks if a given address is already in use locally.
23+
/// This is mostly used to see if the P2P address is already in use.
24+
///
25+
/// Simply tries to connect with TCP to the given address.
26+
#[inline]
27+
pub fn address_in_use(addr: &Multiaddr) -> bool {
28+
addr.iter()
29+
// find the port within our multiaddr
30+
.find_map(|p| {
31+
if let Protocol::Tcp(port) = p {
32+
Some(port)
33+
} else {
34+
None
35+
}
36+
37+
// }
38+
})
39+
// check if its reachable or not
40+
.map(|port| is_port_reachable(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port)))
41+
.unwrap_or_else(|| {
42+
log::error!(
43+
"Could not find any TCP port in the given address: {:?}",
44+
addr
45+
);
46+
false
47+
})
48+
}

compute/src/utils/mod.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,5 @@ pub use message::DKNMessage;
77
mod available_nodes;
88
pub use available_nodes::AvailableNodes;
99

10-
use dkn_p2p::libp2p::{multiaddr::Protocol, Multiaddr};
11-
use port_check::is_port_reachable;
12-
use std::{
13-
net::{Ipv4Addr, SocketAddrV4},
14-
time::{Duration, SystemTime},
15-
};
16-
17-
/// Returns the current time in nanoseconds since the Unix epoch.
18-
///
19-
/// If a `SystemTimeError` occurs, will return 0 just to keep things running.
20-
#[inline]
21-
pub fn get_current_time_nanos() -> u128 {
22-
SystemTime::now()
23-
.duration_since(SystemTime::UNIX_EPOCH)
24-
.unwrap_or_else(|e| {
25-
log::error!("Error getting current time: {}", e);
26-
Duration::new(0, 0)
27-
})
28-
.as_nanos()
29-
}
30-
31-
/// Checks if a given address is already in use locally.
32-
/// This is mostly used to see if the P2P address is already in use.
33-
///
34-
/// Simply tries to connect with TCP to the given address.
35-
#[inline]
36-
pub fn address_in_use(addr: &Multiaddr) -> bool {
37-
addr.iter()
38-
// find the port within our multiaddr
39-
.find_map(|p| {
40-
if let Protocol::Tcp(port) = p {
41-
Some(port)
42-
} else {
43-
None
44-
}
45-
46-
// }
47-
})
48-
// check if its reachable or not
49-
.map(|port| is_port_reachable(SocketAddrV4::new(Ipv4Addr::LOCALHOST, port)))
50-
.unwrap_or_else(|| {
51-
log::error!(
52-
"Could not find any TCP port in the given address: {:?}",
53-
addr
54-
);
55-
false
56-
})
57-
}
10+
mod misc;
11+
pub use misc::*;

0 commit comments

Comments
 (0)