Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global async logging #232

Merged
merged 6 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions forest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ libp2p = "0.15.0"
futures = "0.3.1"
clap = "2.33.0"
log = "0.4.8"
slog = "2.5.2"
slog-async = "2.3.0"
slog-term = "2.4.2"
async-log = "2.0.0"
async-std = { version = "1.4.0", features = ["attributes"] }
serde = { version = "1.0", features = ["derive"] }
pretty_env_logger = "0.3"
3 changes: 1 addition & 2 deletions forest/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ mod config;
pub use config::Config;

use clap::{App, Arg};
use slog::Logger;
use std::io;
use utils::{read_file_to_string, read_toml};

pub(super) fn cli(_log: &Logger) -> Result<Config, io::Error> {
pub(super) fn cli() -> Result<Config, io::Error> {
let app = App::new("Forest")
.version("0.0.1")
.author("ChainSafe Systems <info@chainsafe.io>")
Expand Down
15 changes: 0 additions & 15 deletions forest/src/log/mod.rs

This file was deleted.

11 changes: 11 additions & 0 deletions forest/src/logger/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

pub(crate) fn setup_logger() {
let logger = pretty_env_logger::formatted_timed_builder()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, thoughts on changing the timestamp format

2020-02-18T19:35:07.045

to something like

2020-02-18 19:35:07

Nit, but the timestamp is a lot to look at right now lol. Can change in another PR or something, this may just be me

.filter(None, log::LevelFilter::Trace)
.build();
async_log::Logger::wrap(logger, || 0)
.start(log::LevelFilter::Trace)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info should probably be default?

Suggested change
.start(log::LevelFilter::Trace)
.start(log::LevelFilter::Info)

.unwrap();
}
23 changes: 10 additions & 13 deletions forest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,42 @@
// SPDX-License-Identifier: Apache-2.0, MIT

mod cli;
mod log;

mod logger;
use self::cli::cli;
use async_std::task;
use forest_libp2p::{get_keypair, Libp2pService};
use libp2p::identity::{ed25519, Keypair};
use slog::{info, trace};
use log::{info, trace};
use utils::write_to_file;

#[async_std::main]
async fn main() {
let log = log::setup_logging();
info!(log, "Starting Forest");
logger::setup_logger();
info!("Starting Forest");

// Capture CLI inputs
let config = cli(&log).expect("CLI error");

let logger = log.clone();
let config = cli().expect("CLI error");

let net_keypair = match get_keypair(&log, &"/.forest/libp2p/keypair") {
let net_keypair = match get_keypair(&"/.forest/libp2p/keypair") {
Some(kp) => kp,
None => {
// Keypair not found, generate and save generated keypair
let gen_keypair = ed25519::Keypair::generate();
// Save Ed25519 keypair to file
// TODO rename old file to keypair.old(?)
if let Err(e) = write_to_file(&gen_keypair.encode(), &"/.forest/libp2p/", "keypair") {
info!(log, "Could not write keystore to disk!");
trace!(log, "Error {:?}", e);
info!("Could not write keystore to disk!");
trace!("Error {:?}", e);
};
Keypair::Ed25519(gen_keypair)
}
};

let lp2p_service = Libp2pService::new(logger, &config.network, net_keypair);
let lp2p_service = Libp2pService::new(&config.network, net_keypair);

task::block_on(async move {
lp2p_service.run().await;
});

info!(log, "Forest finish shutdown");
info!("Forest finish shutdown");
}
1 change: 0 additions & 1 deletion node/forest_libp2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ libp2p = "0.15.0"
futures = "0.3.1"
futures-util = "0.3.1"
log = "0.4.8"
slog = "2.5.2"
async-std = { version = "1.4.0", features = ["unstable"] }
serde = { version = "1.0", features = ["derive"] }
35 changes: 11 additions & 24 deletions node/forest_libp2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use libp2p::ping::{
};
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess};
use libp2p::NetworkBehaviour;
use slog::{debug, Logger};
use log::debug;
use std::{task::Context, task::Poll};

#[derive(NetworkBehaviour)]
Expand All @@ -25,8 +25,6 @@ pub struct ForestBehaviour<TSubstream: AsyncRead + AsyncWrite + Unpin + Send + '
pub identify: Identify<TSubstream>,
#[behaviour(ignore)]
events: Vec<ForestBehaviourEvent>,
#[behaviour(ignore)]
log: Logger,
}

#[derive(Debug)]
Expand Down Expand Up @@ -82,29 +80,19 @@ impl<TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static>
match event.result {
Result::Ok(PingSuccess::Ping { rtt }) => {
debug!(
self.log,
"PingSuccess::Ping rtt to {} is {} ms",
event.peer.to_base58(),
rtt.as_millis()
);
}
Result::Ok(PingSuccess::Pong) => {
debug!(
self.log,
"PingSuccess::Pong from {}",
event.peer.to_base58()
);
debug!("PingSuccess::Pong from {}", event.peer.to_base58());
}
Result::Err(PingFailure::Timeout) => {
debug!(self.log, "PingFailure::Timeout {}", event.peer.to_base58());
debug!("PingFailure::Timeout {}", event.peer.to_base58());
}
Result::Err(PingFailure::Other { error }) => {
debug!(
self.log,
"PingFailure::Other {}: {}",
event.peer.to_base58(),
error
);
debug!("PingFailure::Other {}: {}", event.peer.to_base58(), error);
}
}
}
Expand All @@ -120,12 +108,12 @@ impl<TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static>
info,
observed_addr,
} => {
debug!(self.log, "Identified Peer {:?}", peer_id);
debug!(self.log, "protocol_version {:}?", info.protocol_version);
debug!(self.log, "agent_version {:?}", info.agent_version);
debug!(self.log, "listening_ addresses {:?}", info.listen_addrs);
debug!(self.log, "observed_address {:?}", observed_addr);
debug!(self.log, "protocols {:?}", info.protocols);
debug!("Identified Peer {:?}", peer_id);
debug!("protocol_version {:}?", info.protocol_version);
debug!("agent_version {:?}", info.agent_version);
debug!("listening_ addresses {:?}", info.listen_addrs);
debug!("observed_address {:?}", observed_addr);
debug!("protocols {:?}", info.protocols);
}
IdentifyEvent::Sent { .. } => (),
IdentifyEvent::Error { .. } => (),
Expand All @@ -147,15 +135,14 @@ impl<TSubstream: AsyncRead + AsyncWrite + Send + Unpin + 'static> ForestBehaviou
}

impl<TSubstream: AsyncRead + AsyncWrite + Unpin + Send + 'static> ForestBehaviour<TSubstream> {
pub fn new(log: Logger, local_key: &Keypair) -> Self {
pub fn new(local_key: &Keypair) -> Self {
let local_peer_id = local_key.public().into_peer_id();
let gossipsub_config = GossipsubConfig::default();
ForestBehaviour {
gossipsub: Gossipsub::new(local_peer_id, gossipsub_config),
mdns: Mdns::new().expect("Could not start mDNS"),
ping: Ping::default(),
identify: Identify::new("forest/libp2p".into(), "0.0.1".into(), local_key.public()),
log,
events: vec![],
}
}
Expand Down
31 changes: 14 additions & 17 deletions node/forest_libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use libp2p::{
identity::{ed25519, Keypair},
mplex, secio, yamux, PeerId, Swarm, Transport,
};
use slog::{debug, error, info, trace, Logger};
use log::{debug, error, info, trace};
use std::io::{Error, ErrorKind};
use std::time::Duration;
use utils::{get_home_dir, read_file_to_vec};
Expand Down Expand Up @@ -47,31 +47,29 @@ pub struct Libp2pService {

pubsub_receiver_out: Receiver<NetworkEvent>,
pubsub_sender_out: Sender<NetworkEvent>,

log: Logger,
}

impl Libp2pService {
/// Constructs a Libp2pService
pub fn new(log: Logger, config: &Libp2pConfig, net_keypair: Keypair) -> Self {
pub fn new(config: &Libp2pConfig, net_keypair: Keypair) -> Self {
let peer_id = PeerId::from(net_keypair.public());

info!(log, "Local peer id: {:?}", peer_id);
info!("Local peer id: {:?}", peer_id);

let transport = build_transport(net_keypair.clone());

let mut swarm = {
let be = ForestBehaviour::new(log.clone(), &net_keypair);
let be = ForestBehaviour::new(&net_keypair);
Swarm::new(transport, be, peer_id)
};

for node in config.bootstrap_peers.clone() {
match node.parse() {
Ok(to_dial) => match Swarm::dial_addr(&mut swarm, to_dial) {
Ok(_) => debug!(log, "Dialed {:?}", node),
Err(e) => debug!(log, "Dial {:?} failed: {:?}", node, e),
Ok(_) => debug!("Dialed {:?}", node),
Err(e) => debug!("Dial {:?} failed: {:?}", node, e),
},
Err(err) => error!(log, "Failed to parse address to dial: {:?}", err),
Err(err) => error!("Failed to parse address to dial: {:?}", err),
}
}

Expand All @@ -96,7 +94,6 @@ impl Libp2pService {
pubsub_sender_in,
pubsub_receiver_out,
pubsub_sender_out,
log,
}
}

Expand All @@ -117,7 +114,7 @@ impl Libp2pService {
topics,
message,
} => {
info!(self.log, "Got a Gossip Message from {:?}", source);
info!( "Got a Gossip Message from {:?}", source);
self.pubsub_sender_out.send(NetworkEvent::PubsubMessage {
source,
topics,
Expand Down Expand Up @@ -168,22 +165,22 @@ pub fn build_transport(local_key: Keypair) -> Boxed<(PeerId, StreamMuxerBox), Er
}

/// Fetch keypair from disk, returning none if it cannot be decoded
pub fn get_keypair(log: &Logger, path: &str) -> Option<Keypair> {
pub fn get_keypair(path: &str) -> Option<Keypair> {
let path_to_keystore = get_home_dir() + path;
match read_file_to_vec(&path_to_keystore) {
Err(e) => {
info!(log, "Networking keystore not found!");
trace!(log, "Error {:?}", e);
info!("Networking keystore not found!");
trace!("Error {:?}", e);
None
}
Ok(mut vec) => match ed25519::Keypair::decode(&mut vec) {
Ok(kp) => {
info!(log, "Recovered keystore from {:?}", &path_to_keystore);
info!("Recovered keystore from {:?}", &path_to_keystore);
Some(Keypair::Ed25519(kp))
}
Err(e) => {
info!(log, "Could not decode networking keystore!");
trace!(log, "Error {:?}", e);
info!("Could not decode networking keystore!");
trace!("Error {:?}", e);
None
}
},
Expand Down