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

Commit

Permalink
feat: add log example
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Dec 8, 2023
1 parent b03755a commit c0b06f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ snafu = { version = "0.7.5", default-features = false, features = [
] }
no_std_io = { version = "0.6.0", features = ["alloc"] }
log = "0.4.20"
chrono = { version = "0.4.31", default-features = false }
chrono = { version = "0.4.31", default-features = false, features = ["alloc"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
dlmalloc = { version = "0.2.4", features = ["global"] }
19 changes: 19 additions & 0 deletions pros/examples/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![no_std]
#![no_main]

use core::time::Duration;
use pros::prelude::*;

#[derive(Debug, Default)]
struct ExampleRobot;
impl Robot for ExampleRobot {
fn opcontrol(&mut self) -> pros::Result {
pros::logger::ProsLogger::init().unwrap();

let str = "PROS";
info!("Hello, {str}!");

Ok(())
}
}
robot!(ExampleRobot);
14 changes: 10 additions & 4 deletions pros/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::time::Duration;

use alloc::{ffi::CString, format, string::ToString};
use alloc::{ffi::CString, format};

use log::{Log, Metadata, Record};

Expand All @@ -26,8 +26,6 @@ impl Log for ProsLogger {
}

fn log(&self, record: &Record) {
let level_string = format!("{:<5}", record.level().to_string());

let target = if !record.target().is_empty() {
record.target()
} else {
Expand All @@ -38,7 +36,15 @@ impl Log for ProsLogger {
chrono::Duration::from_std(Duration::from_millis(unsafe { pros_sys::millis() as _ }))
.unwrap();

let message = format!("{} {} [{}] {}", format!("{}m {}s {}ms", now.num_minutes(), now.num_seconds(), now.num_milliseconds()), level_string, target, record.args());
let message = format!(
"{}m{}s{}ms [{}] {}: {}",
now.num_minutes(),
now.num_seconds(),
now.num_milliseconds(),
record.level(),
target,
record.args()
);

println!("{}", message);
// Print to the debug teminal
Expand Down

0 comments on commit c0b06f8

Please sign in to comment.