From 2e601bac025ea44e900ed5495763ff166f2c74e2 Mon Sep 17 00:00:00 2001 From: Dominik Stefancik Date: Mon, 24 Jun 2024 13:34:58 +0200 Subject: [PATCH 1/2] Setup logging from particular crates --- crates/hulk_nao/src/main.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/hulk_nao/src/main.rs b/crates/hulk_nao/src/main.rs index 54dcbfe456..550e3b4912 100644 --- a/crates/hulk_nao/src/main.rs +++ b/crates/hulk_nao/src/main.rs @@ -12,6 +12,7 @@ use color_eyre::{ install, }; use ctrlc::set_handler; +use fern::Dispatch; use framework::Parameters as FrameworkParameters; use hardware::IdInterface; use hardware_interface::{HardwareInterface, Parameters as HardwareParameters}; @@ -29,7 +30,9 @@ mod microphones; mod speakers; pub fn setup_logger() -> Result<(), fern::InitError> { - fern::Dispatch::new() + let base_config = Dispatch::new(); + + let common_config = Dispatch::new() .format(|out, message, record| { out.finish(format_args!( "{} {:<18} {:>5} {}", @@ -40,7 +43,27 @@ pub fn setup_logger() -> Result<(), fern::InitError> { )) }) .level(log::LevelFilter::Debug) - .chain(stdout()) + .chain(stdout()); + + // a log config for logs coming from specific crates + let live_log_config = Dispatch::new() + .format(|out, message, record| { + out.finish(format_args!( + "{} {:<18} {:>5} {}", + chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), + record.target(), + record.level(), + message + )) + }) + // only apply for logs from the specific crate + .level_for("control", log::LevelFilter::Debug) + .level_for("hulk", log::LevelFilter::Debug) + .chain(fern::log_file("control-program.log")?); + + base_config + .chain(common_config) + .chain(live_log_config) .apply()?; Ok(()) } From cec6632db74f1cd590a06dd85031ff4f26146dde Mon Sep 17 00:00:00 2001 From: Dominik Stefancik Date: Mon, 24 Jun 2024 13:36:32 +0200 Subject: [PATCH 2/2] Setup logging from particular crates --- Cargo.lock | 35 +++++++++++++++++++++++++++++- crates/control/Cargo.toml | 1 + crates/control/src/a_star.rs | 2 +- crates/control/src/path_planner.rs | 9 ++++++++ tools/fanta/Cargo.toml | 2 ++ tools/fanta/src/main.rs | 18 +++++++++++++++ 6 files changed, 65 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a455456ce7..f8c2487e4e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -676,7 +676,7 @@ dependencies = [ "flate2", "globset", "grep-cli", - "nu-ansi-term", + "nu-ansi-term 0.47.0", "once_cell", "path_abs", "plist", @@ -1440,6 +1440,7 @@ dependencies = [ "smallvec", "spl_network_messages", "splines", + "tracing", "types", "walking_engine", ] @@ -2228,6 +2229,8 @@ dependencies = [ "fern", "log", "tokio", + "tracing", + "tracing-subscriber", ] [[package]] @@ -3977,6 +3980,16 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.47.0" @@ -4388,6 +4401,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "owned_ttf_parser" version = "0.20.0" @@ -5934,15 +5953,29 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + [[package]] name = "tracing-subscriber" version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "nu-ansi-term 0.46.0", "sharded-slab", + "smallvec", "thread_local", "tracing-core", + "tracing-log", ] [[package]] diff --git a/crates/control/Cargo.toml b/crates/control/Cargo.toml index 30b62496c8..456ed7a757 100644 --- a/crates/control/Cargo.toml +++ b/crates/control/Cargo.toml @@ -32,5 +32,6 @@ serde = { workspace = true } smallvec = { workspace = true } spl_network_messages = { workspace = true } splines = { workspace = true } +tracing = "0.1.40" types = { workspace = true } walking_engine = { workspace = true } diff --git a/crates/control/src/a_star.rs b/crates/control/src/a_star.rs index 35cbb92f2c..62eb0548c6 100644 --- a/crates/control/src/a_star.rs +++ b/crates/control/src/a_star.rs @@ -43,7 +43,7 @@ where /// `destination` is the index of the target tile. /// `success` is true if it reached the target, false otherwise. /// `steps` is a vector of each step towards the target, *including* the starting position. -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] pub struct NavigationPath { pub destination: usize, pub success: bool, diff --git a/crates/control/src/path_planner.rs b/crates/control/src/path_planner.rs index 730ac8f86b..2e27c45f6a 100644 --- a/crates/control/src/path_planner.rs +++ b/crates/control/src/path_planner.rs @@ -1,8 +1,10 @@ use color_eyre::{eyre::eyre, Result}; use geometry::{arc::Arc, circle::Circle, direction::Direction, line_segment::LineSegment}; use linear_algebra::{distance, point, vector, Isometry2, Orientation2, Point2}; +use log::{info, warn}; use ordered_float::NotNan; use smallvec::SmallVec; +use tracing::warn_span; use coordinate_systems::{Field, Ground}; use types::{ @@ -307,6 +309,13 @@ impl PathPlanner { let navigation_path = a_star_search(0, 1, self); if !navigation_path.success { + // This warning is used for regular logging + // it will be logged into a file (see function setup_logger in crates/hulk_nao/src/main.rs) + warn!(target: "control", "Path not found: {:?}", navigation_path); + + // This warning is used for Tokio related logging using "tracing" crate + // see function setup_logger in tools/fanta/src/main.rs) + warn_span! {"Path not found: {:?}", navigation_path}; return Ok(None); } diff --git a/tools/fanta/Cargo.toml b/tools/fanta/Cargo.toml index d9798d6004..eca2442e51 100644 --- a/tools/fanta/Cargo.toml +++ b/tools/fanta/Cargo.toml @@ -12,3 +12,5 @@ communication = { workspace = true } fern = { workspace = true } log = { workspace = true } tokio = { workspace = true } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" diff --git a/tools/fanta/src/main.rs b/tools/fanta/src/main.rs index 8cc24cfc1c..dccc4beb1c 100644 --- a/tools/fanta/src/main.rs +++ b/tools/fanta/src/main.rs @@ -24,6 +24,24 @@ struct CommandlineArguments { async fn main() -> Result<()> { setup_logger()?; + // create a subscriber for logs used by "tracing" crate + // for emitting log messages, see crates/control/src/path_planner.rs, line 318 + let subscriber = tracing_subscriber::fmt() + // Use a more compact, abbreviated log format + .compact() + // Display source code file paths + .with_file(true) + // Display source code line numbers + .with_line_number(true) + // Display the thread ID an event was recorded on + .with_thread_ids(true) + // Don't display the event's target (module path) + .with_target(false) + // Build the subscriber + .finish(); + + tracing::subscriber::set_global_default(subscriber)?; + let arguments = CommandlineArguments::parse(); let output_to_subscribe = CyclerOutput::from_str(&arguments.path)?; let communication = Communication::new(Some(format!("ws://{}:1337", arguments.address)), true);