Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
wip logger
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Dec 7, 2023
1 parent 66031da commit 7ef0e40
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions pros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod lcd;
pub mod adi;
pub mod link;
pub mod lvgl;
mod tracing;

pub type Result<T = ()> = core::result::Result<T, alloc::boxed::Box<dyn core::error::Error>>;

Expand Down
115 changes: 115 additions & 0 deletions pros/src/tracing.rs
Original file line number Diff line number Diff line change
@@ -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) {}
}

Check failure on line 41 in pros/src/tracing.rs

View workflow job for this annotation

GitHub Actions / Check

unexpected closing delimiter: `}`

Check failure on line 41 in pros/src/tracing.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

unexpected closing delimiter: `}`

/// Configure the console to display colours.

This comment has been minimized.

Copy link
@Gavin-Niederman

Gavin-Niederman Dec 8, 2023

Member

delete this, we don't build to windows

///
/// 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()
}

0 comments on commit 7ef0e40

Please sign in to comment.