From 7ef0e40838d789ab52327cefdac6abed932a2d09 Mon Sep 17 00:00:00 2001 From: doinkythederp Date: Wed, 6 Dec 2023 17:31:52 -0800 Subject: [PATCH] wip logger --- pros/Cargo.toml | 4 ++ pros/src/lib.rs | 1 + pros/src/tracing.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 pros/src/tracing.rs diff --git a/pros/Cargo.toml b/pros/Cargo.toml index 3b9f4da3..e3fc2c98 100644 --- a/pros/Cargo.toml +++ b/pros/Cargo.toml @@ -19,6 +19,10 @@ snafu = { version = "0.7.5", default-features = false, features = [ "rust_1_61", ] } no_std_io = { version = "0.6.0", features = ["alloc"] } +tracing = { version = "0.1.40", default-features = false } +tracing-subscriber = { version = "0.3.18", default-features = false, features = ["alloc"] } +log = "0.4.20" +chrono = { version = "0.4.31", default-features = false } [target.'cfg(target_arch = "wasm32")'.dependencies] dlmalloc = { version = "0.2.4", features = ["global"] } diff --git a/pros/src/lib.rs b/pros/src/lib.rs index 700715f3..3e5153ce 100644 --- a/pros/src/lib.rs +++ b/pros/src/lib.rs @@ -22,6 +22,7 @@ pub mod lcd; pub mod adi; pub mod link; pub mod lvgl; +mod tracing; pub type Result = core::result::Result>; diff --git a/pros/src/tracing.rs b/pros/src/tracing.rs new file mode 100644 index 00000000..e20cd02e --- /dev/null +++ b/pros/src/tracing.rs @@ -0,0 +1,115 @@ +use core::time::Duration; + +use log::{Level, Log, Metadata, Record, SetLoggerError}; + +struct ProsLogger; + +impl Log for ProsLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + true + } + + fn log(&self, record: &Record) { + let level_string = format!("{:<5}", record.level().to_string()) + + let target = if !record.target().is_empty() { + record.target() + } else { + record.module_path().unwrap_or_default() + }; + + let now = Duration::from unsafe { pros_sys::millis()}; + + let message = format!( + "{}{} [{}{}] {}", + timestamp, + level_string, + target, + thread, + record.args() + ); + + #[cfg(not(feature = "stderr"))] + println!("{}", message); + + #[cfg(feature = "stderr")] + eprintln!("{}", message); + } + } + + fn flush(&self) {} +} + +/// Configure the console to display colours. +/// +/// This is only needed on Windows when using the 'colored' feature. +#[cfg(all(windows, feature = "colored"))] +pub fn set_up_color_terminal() { + use std::io::{stdout, IsTerminal}; + + if stdout().is_terminal() { + unsafe { + use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; + use windows_sys::Win32::System::Console::{ + GetConsoleMode, GetStdHandle, SetConsoleMode, CONSOLE_MODE, + ENABLE_VIRTUAL_TERMINAL_PROCESSING, STD_OUTPUT_HANDLE, + }; + + let stdout = GetStdHandle(STD_OUTPUT_HANDLE); + + if stdout == INVALID_HANDLE_VALUE { + return; + } + + let mut mode: CONSOLE_MODE = 0; + + if GetConsoleMode(stdout, &mut mode) == 0 { + return; + } + + SetConsoleMode(stdout, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); + } + } +} + +/// Configure the console to display colours. +/// +/// This method does nothing if not running on Windows with the colored feature. +#[cfg(not(all(windows, feature = "colored")))] +pub fn set_up_color_terminal() {} + +/// Initialise the logger with its default configuration. +/// +/// Log messages will not be filtered. +/// The `RUST_LOG` environment variable is not used. +pub fn init() -> Result<(), SetLoggerError> { + SimpleLogger::new().init() +} + +/// Initialise the logger with its default configuration. +/// +/// Log messages will not be filtered. +/// The `RUST_LOG` environment variable is not used. +/// +/// This function is only available if the `timestamps` feature is enabled. +#[cfg(feature = "timestamps")] +pub fn init_utc() -> Result<(), SetLoggerError> { + SimpleLogger::new().with_utc_timestamps().init() +} + +/// Initialise the logger with the `RUST_LOG` environment variable. +/// +/// Log messages will be filtered based on the `RUST_LOG` environment variable. +pub fn init_with_env() -> Result<(), SetLoggerError> { + SimpleLogger::new().env().init() +} + +/// Initialise the logger with a specific log level. +/// +/// Log messages below the given [`Level`] will be filtered. +/// The `RUST_LOG` environment variable is not used. +pub fn init_with_level(level: Level) -> Result<(), SetLoggerError> { + SimpleLogger::new() + .with_level(level.to_level_filter()) + .init() +}