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

enable logging with milliseconds since boot and system time in rust logs depending on configuration #494

Merged
merged 1 commit into from
Oct 14, 2024
Merged
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
27 changes: 18 additions & 9 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,31 @@ impl ::log::Log for EspLogger {

if self.enabled(metadata) && self.should_log(record) {
let marker = Self::get_marker(metadata.level());
let timestamp = unsafe { esp_log_timestamp() };
let target = record.metadata().target();
let args = record.args();
let color = Self::get_color(record.level());

let mut stdout = EspStdout::new();

if let Some(color) = color {
writeln!(
stdout,
"\x1b[0;{}m{} ({}) {}: {}\x1b[0m",
color, marker, timestamp, target, args
)
.unwrap();
} else {
writeln!(stdout, "{} ({}) {}: {}", marker, timestamp, target, args).unwrap();
write!(stdout, "\x1b[0;{}m", color).unwrap();
}
write!(stdout, "{} (", marker).unwrap();
if cfg!(esp_idf_log_timestamp_rtos) {
let timestamp = unsafe { esp_log_timestamp() };
write!(stdout, "{}", timestamp).unwrap();
} else if cfg!(esp_idf_log_timestamp_source_system) {
// TODO: https://github.com/esp-rs/esp-idf-svc/pull/494 - official usage of
// `esp_log_timestamp_str()` should be tracked and replace the not thread-safe
// `esp_log_system_timestamp()` which has a race condition flaw due to
// returning a pointer to a static buffer containing the c-string.
let timestamp =
unsafe { CStr::from_ptr(esp_log_system_timestamp()).to_str().unwrap() };
write!(stdout, "{}", timestamp).unwrap();
}
write!(stdout, ") {}: {}", target, args).unwrap();
if color.is_some() {
writeln!(stdout, "\x1b[0m").unwrap();
}
}
}
Expand Down
Loading