Skip to content

Commit

Permalink
Use tracing to log events
Browse files Browse the repository at this point in the history
  • Loading branch information
pool2win committed Dec 3, 2024
1 parent e44431e commit 0a9216d
Show file tree
Hide file tree
Showing 23 changed files with 175 additions and 220 deletions.
57 changes: 7 additions & 50 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
env_logger = "0.10.1"
log = "0.4.20"
tokio = { version = "1", features = ["rt", "net", "macros", "rt-multi-thread", "tracing", "io-util"] }
tokio-util = { version = "0.7.0", features = ["full"] }
tokio-stream = { version = "0.1" }
toml = "0.8.8"
tracing = "0.1"
tracing-subscriber = { version = "0.3.1", default-features = false, features = ["fmt", "ansi", "env-filter", "tracing-log"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_derive = "1.0.188"
futures = "0.3.30"
Expand Down
2 changes: 0 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// <https://www.gnu.org/licenses/>.

use clap::Parser;

#[derive(Parser, Debug)]
pub struct Cli {
/// Config file to load
Expand All @@ -31,7 +30,6 @@ mod tests {

#[test]
fn it_should_build_struct_for_parsing_cli_options() {
let _ = env_logger::try_init();
let options = vec!["node", "--config-file=conf.toml"];
let args = Cli::try_parse_from(options).expect("Error parsing options");
assert_eq!(args.config_file, "conf.toml");
Expand Down
12 changes: 3 additions & 9 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use serde::Deserialize;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use tracing::info;

/// Struct to capture configuration from config.toml
#[derive(Deserialize, Default, Debug, PartialEq)]
Expand Down Expand Up @@ -133,7 +134,7 @@ pub struct NoiseConfig {
pub fn load_config_from_file(path: String) -> Option<Config> {
match read_file(path) {
Err(_) => {
log::info!("No config.toml file provided.");
info!("No config.toml file provided.");
Some(Config::default())
}
Ok(contents) => parse_config_from_string(contents),
Expand All @@ -146,7 +147,7 @@ fn parse_config_from_string(contents: String) -> Option<Config> {
match config {
Ok(config) => config,
Err(e) => {
log::info!("Error parsing config file {:?}", e);
info!("Error parsing config file {:?}", e);
None
}
}
Expand Down Expand Up @@ -207,7 +208,6 @@ mod tests {

#[test]
fn it_should_load_default_for_missing_fields() {
let _ = env_logger::try_init();
let conf = parse_config_from_string(
r#"
[network]
Expand All @@ -227,7 +227,6 @@ mod tests {

#[test]
fn it_should_load_default_for_missing_fields_for_network() {
let _ = env_logger::try_init();
let conf = parse_config_from_string(
r#"
[network]"#
Expand All @@ -246,7 +245,6 @@ mod tests {

#[test]
fn it_should_load_default_for_missing_fields_for_peer() {
let _ = env_logger::try_init();
let conf = parse_config_from_string(
r#"
[peer]"#
Expand All @@ -265,7 +263,6 @@ mod tests {

#[test]
fn it_should_load_default_for_missing_fields_in_multiple_sections() {
let _ = env_logger::try_init();
let conf = parse_config_from_string(
r#"
[network]
Expand All @@ -288,7 +285,6 @@ mod tests {

#[test]
fn it_should_load_config_without_errors() {
let _ = env_logger::try_init();
let conf = load_config_from_file("config.toml".to_string()).unwrap();
let network = conf.network;
let peer = conf.peer;
Expand All @@ -305,7 +301,6 @@ mod tests {

#[test]
fn it_should_load_default_config_when_file_not_found() {
let _ = env_logger::try_init();
let conf = load_config_from_file("no_such_file.toml".to_string()).unwrap();
let network = conf.network;
let peer = conf.peer;
Expand All @@ -322,7 +317,6 @@ mod tests {

#[test]
fn it_should_get_bind_address_from_config() {
let _ = env_logger::try_init();
let conf = load_config_from_file("config.toml".to_string()).unwrap();
let network = conf.network;
assert_eq!(get_bind_address(network), "localhost:6680");
Expand Down
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ use frost_federation::cli;
use frost_federation::config;
use frost_federation::node;

extern crate tracing;
use tracing::{error, info};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = cli::Cli::parse();

let config = config::load_config_from_file(args.config_file).unwrap();
let bind_address = config::get_bind_address(config.network);

setup_logging()?;
setup_tracing()?;

let (command_executor, command_rx) = CommandExecutor::new();
Expand All @@ -48,25 +50,22 @@ async fn main() -> Result<(), Box<dyn Error>> {
tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
if let Err(e) = executor.run_dkg().await {
log::error!("Failed to run DKG: {}", e);
error!("Failed to run DKG: {}", e);
}
});
}

// Start node
let (ready_tx, _ready_rx) = oneshot::channel();
node.start(command_rx, ready_tx).await;
log::info!("Stopping node");
info!("Stopping node");
Ok(())
}

fn setup_tracing() -> Result<(), Box<dyn Error>> {
let subscriber = tracing_subscriber::FmtSubscriber::new();
let subscriber = tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.finish();
tracing::subscriber::set_global_default(subscriber)?;
Ok(())
}

fn setup_logging() -> Result<(), Box<dyn Error>> {
env_logger::init();
Ok(())
}
Loading

0 comments on commit 0a9216d

Please sign in to comment.