Skip to content

Commit

Permalink
Helper to print or info log depending on log level
Browse files Browse the repository at this point in the history
  • Loading branch information
sandreae committed Jun 22, 2024
1 parent 6bc1bf7 commit 98848dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
9 changes: 6 additions & 3 deletions aquadoggo/src/http/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::http::api::{
handle_blob_document, handle_blob_view, handle_graphql_playground, handle_graphql_query,
};
use crate::http::context::HttpServiceContext;
use crate::info_or_print;
use crate::manager::{ServiceReadySender, Shutdown};

/// Route to the GraphQL playground
Expand Down Expand Up @@ -74,17 +75,19 @@ pub async fn http_service(
let builder = if let Ok(builder) = axum::Server::try_bind(&http_address) {
builder
} else {
println!("HTTP port {http_port} was already taken, try random port instead ..");
info_or_print(&format!(
"HTTP port {http_port} was already taken, try random port instead .."
));

Check warning on line 80 in aquadoggo/src/http/service.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/http/service.rs#L78-L80

Added lines #L78 - L80 were not covered by tests
axum::Server::try_bind(&SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0))?
};

let builder = builder.serve(build_server(http_context).into_make_service());

let local_address = builder.local_addr();
println!(
info_or_print(&format!(
"Go to http://{}/graphql to use GraphQL playground",
local_address
);
));

builder
.with_graceful_shutdown(async {
Expand Down
9 changes: 9 additions & 0 deletions aquadoggo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod tests;
pub use crate::api::{ConfigFile, LockFile};
pub use crate::config::{AllowList, Configuration};
pub use crate::network::NetworkConfiguration;
use log::{info, log_enabled, Level};
pub use node::Node;

/// Init env_logger before the test suite runs to handle logging outputs.
Expand All @@ -59,3 +60,11 @@ fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
}

fn info_or_print(message: &str) {
if log_enabled!(Level::Info) || log_enabled!(Level::Debug) || log_enabled!(Level::Trace) {
info!("{message}");

Check warning on line 66 in aquadoggo/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/lib.rs#L66

Added line #L66 was not covered by tests
} else {
println!("{message}");
}
}
17 changes: 9 additions & 8 deletions aquadoggo/src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::network::behaviour::{Event, P2pandaBehaviour};
use crate::network::relay::Relay;
use crate::network::utils::{dial_known_peer, is_known_peer_address};
use crate::network::{identity, peers, swarm, utils, ShutdownHandler};
use crate::NetworkConfiguration;
use crate::{info_or_print, NetworkConfiguration};

/// Interval at which we attempt to dial known peers and relays.
const REDIAL_INTERVAL: Duration = Duration::from_secs(20);
Expand All @@ -49,7 +49,7 @@ pub async fn network_service(
let key_pair = identity::to_libp2p_key_pair(&context.key_pair);
let local_peer_id = key_pair.public().to_peer_id();

println!("Peer id: {local_peer_id}");
info_or_print(&format!("Peer id: {local_peer_id}"));

// The swarm can be initiated with or without "relay" capabilities.
let mut swarm = if network_config.relay_mode {
Expand All @@ -70,10 +70,12 @@ pub async fn network_service(
.with(Protocol::from(Ipv4Addr::UNSPECIFIED))
.with(Protocol::Udp(0))
.with(Protocol::QuicV1);
println!(

info_or_print(&format!(

Check warning on line 74 in aquadoggo/src/network/service.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/network/service.rs#L73-L74

Added lines #L73 - L74 were not covered by tests
"QUIC port {} was already taken, try random port instead ..",
network_config.quic_port
);
));

Check warning on line 78 in aquadoggo/src/network/service.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/network/service.rs#L77-L78

Added lines #L77 - L78 were not covered by tests
swarm.listen_on(random_port_addr)?;
}

Expand Down Expand Up @@ -184,7 +186,7 @@ impl EventLoop {
// Show only one QUIC address during the runtime of the node, otherwise
// it might get too spammy
if let Some(address) = utils::to_quic_address(&address) {
println!("Node is listening on 0.0.0.0:{}", address.port());
info_or_print(&format!("Node is listening on 0.0.0.0:{}", address.port()));
self.learned_port = true;
}
}
Expand Down Expand Up @@ -308,9 +310,8 @@ impl EventLoop {
.addresses(vec![peer_circuit_address])
.build();

match self.swarm.dial(opts) {
Ok(_) => debug!("Dialed peer {}", peer_id),
Err(_) => (),
if self.swarm.dial(opts).is_ok() {
debug!("Dialed peer {}", peer_id)
};

Check warning on line 315 in aquadoggo/src/network/service.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/network/service.rs#L302-L315

Added lines #L302 - L315 were not covered by tests
} else {
debug!("Discovered peer from unknown relay node")

Check warning on line 317 in aquadoggo/src/network/service.rs

View check run for this annotation

Codecov / codecov/patch

aquadoggo/src/network/service.rs#L317

Added line #L317 was not covered by tests
Expand Down

0 comments on commit 98848dc

Please sign in to comment.