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

RustFest2024 - Logging improvements suggestions #1079

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
35 changes: 34 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions crates/control/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion crates/control/src/a_star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions crates/control/src/path_planner.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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);
}

Expand Down
27 changes: 25 additions & 2 deletions crates/hulk_nao/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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} {}",
Expand All @@ -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(())
}
Expand Down
2 changes: 2 additions & 0 deletions tools/fanta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions tools/fanta/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down