From c05b771e769911caa2641d51dd273fe2e51151e2 Mon Sep 17 00:00:00 2001 From: Russell Gilbert Date: Thu, 26 Feb 2026 13:07:19 +0000 Subject: [PATCH] Integrates structured logging with `tracing` Replaces existing `println!` statements with `tracing::info!` macros for improved observability and debuggability. Configures `tracing-subscriber` to enable structured logging, allowing for flexible filtering via `RUST_LOG` and compact, human-readable output by default. --- src/rustylee/Cargo.lock | 202 ++++++++++++++++++++++++++++++++ src/rustylee/Cargo.toml | 2 + src/rustylee/src/brain/mod.rs | 6 +- src/rustylee/src/brain/state.rs | 1 - src/rustylee/src/main.rs | 31 +++-- 5 files changed, 227 insertions(+), 15 deletions(-) diff --git a/src/rustylee/Cargo.lock b/src/rustylee/Cargo.lock index 33006c5..bdd238b 100644 --- a/src/rustylee/Cargo.lock +++ b/src/rustylee/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "bitflags" version = "2.11.0" @@ -52,12 +61,39 @@ dependencies = [ "objc2", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.180" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + [[package]] name = "nix" version = "0.31.1" @@ -70,6 +106,15 @@ dependencies = [ "libc", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys", +] + [[package]] name = "objc2" version = "0.6.3" @@ -85,13 +130,170 @@ version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "rustylee" version = "0.1.0" dependencies = [ "ctrlc", + "tracing", + "tracing-subscriber", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[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.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "windows-link" version = "0.2.1" diff --git a/src/rustylee/Cargo.toml b/src/rustylee/Cargo.toml index ca43573..0de3323 100644 --- a/src/rustylee/Cargo.toml +++ b/src/rustylee/Cargo.toml @@ -5,3 +5,5 @@ edition = "2024" [dependencies] ctrlc = "3.5.2" +tracing = "0.1" +tracing-subscriber = { version = "0.3.22", features = ["env-filter", "fmt"] } diff --git a/src/rustylee/src/brain/mod.rs b/src/rustylee/src/brain/mod.rs index 2f5d628..926d5cb 100644 --- a/src/rustylee/src/brain/mod.rs +++ b/src/rustylee/src/brain/mod.rs @@ -1,7 +1,7 @@ -//src/brain/mod.rs pub mod state; pub use self::state::State; -use std::thread::{self, JoinHandle}; +use tracing::info; +use std::thread::JoinHandle; pub struct Brain { @@ -33,7 +33,7 @@ impl Brain { // Logic will go here eventually } State::Stopping => { - println!("Brain: Waiting for all threads to join..."); + info!("Brain: Waiting for all threads to join..."); while let Some(handle) = self.device_threads.pop() { let _ = handle.join(); } diff --git a/src/rustylee/src/brain/state.rs b/src/rustylee/src/brain/state.rs index 7b5f4b7..f77ea1d 100644 --- a/src/rustylee/src/brain/state.rs +++ b/src/rustylee/src/brain/state.rs @@ -1,4 +1,3 @@ -//src/brain/state.rs #[derive(PartialEq, Debug, Clone, Copy)] pub enum State { Starting, diff --git a/src/rustylee/src/main.rs b/src/rustylee/src/main.rs index 04f5aa2..0cabfc7 100644 --- a/src/rustylee/src/main.rs +++ b/src/rustylee/src/main.rs @@ -1,29 +1,24 @@ mod brain; use brain::{Brain, State}; +use tracing::info; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::sync::mpsc::{self, Receiver, Sender}; use std::{thread, time::Duration}; fn main() { - // We wrap the Brain in an Arc + Mutex so the Signal Handler can access it - // though for simple flag-setting, an Atomic works too. - // Here we'll use a simple shared flag to tell the Brain to stop. let shutdown_requested = Arc::new(AtomicBool::new(false)); let s = shutdown_requested.clone(); - // 1. Signal Handling - // By default, in Rust/Unix, signals are delivered to the process. - // Setting the handler here ensures the main thread catches it. + setup_logging(); + ctrlc::set_handler(move || { - println!("\n[Signal] Shutdown signal caught!"); + info!("[Signal] Shutdown signal caught!"); s.store(true, Ordering::SeqCst); }).expect("Error setting signal handler"); let mut robot_brain = Brain::new(); - // 2. The Watchdog Loop - // This loop runs until the Brain reaches the terminal 'Stopped' state while robot_brain.state != State::Stopped { // If the signal handler tripped the flag, tell the brain to stop @@ -34,9 +29,23 @@ fn main() { // Run one "tick" of brain logic robot_brain.update(); - println!("Main Loop: I'm alive! State: {:?}", robot_brain.state); + info!("Main Loop: I'm alive! State: {:?}", robot_brain.state); thread::sleep(Duration::from_millis(500)); } - println!("Main Loop: Brain has Stopped. Exiting cleanly."); + info!("Main Loop: Brain has Stopped. Exiting cleanly."); +} + +fn setup_logging() { + use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + + let console_layer = fmt::layer() + .with_target(false) + .with_thread_ids(false) + .compact(); + + tracing_subscriber::registry() + .with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"))) + .with(console_layer) + .init(); } \ No newline at end of file