Skip to content

Commit 874230f

Browse files
committed
log: add timestamp to stdout_logger
UTC timestamp to `stdout_logger`. Show on default, parameter available.
1 parent a3214b4 commit 874230f

File tree

4 files changed

+113
-6
lines changed

4 files changed

+113
-6
lines changed

Cargo.lock

Lines changed: 85 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/log/stdout_logger/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ rust_library(
2424
visibility = ["//visibility:public"],
2525
deps = [
2626
"//src/log/score_log",
27+
"@score_crates//:time",
2728
],
2829
)
2930

src/log/stdout_logger/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ path = "lib.rs"
1010

1111
[dependencies]
1212
score_log.workspace = true
13+
time = { version = "0.3.44", features = ["macros", "formatting"] }
1314

1415
[lints]
1516
workspace = true

src/log/stdout_logger/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ use core::cell::RefCell;
1818
use core::fmt::Write;
1919
use score_log::fmt::{score_write, Error, FormatSpec, Result, ScoreWrite};
2020
use score_log::{LevelFilter, Log, Metadata, Record};
21+
use time::format_description::FormatItem;
22+
use time::macros::format_description;
23+
use time::UtcDateTime;
24+
25+
/// Timestamp format matches the `score::mw::log` format.
26+
const TIMESTAMP_FORMAT: &[FormatItem<'_>] =
27+
format_description!("[year]/[month]/[day] [hour]:[minute]:[second].[subsecond digits:7]");
2128

2229
/// Fixed size buffer for strings.
2330
struct FixedBuf<const N: usize> {
@@ -191,6 +198,15 @@ impl StdoutLoggerBuilder {
191198
self
192199
}
193200

201+
/// Show timestamp.
202+
///
203+
/// UTC timestamp in following format:
204+
/// "[year]/[month]/[day] [hour]:[minute]:[second].[subsecond digits:7]"
205+
pub fn show_timestamp(mut self, show_timestamp: bool) -> Self {
206+
self.0.show_timestamp = show_timestamp;
207+
self
208+
}
209+
194210
/// Filter logs by level.
195211
pub fn log_level(mut self, log_level: LevelFilter) -> Self {
196212
self.0.log_level = log_level;
@@ -226,6 +242,7 @@ impl Default for StdoutLoggerBuilder {
226242
show_module: false,
227243
show_file: false,
228244
show_line: false,
245+
show_timestamp: true,
229246
log_level: LevelFilter::Info,
230247
})
231248
}
@@ -241,6 +258,7 @@ pub struct StdoutLogger {
241258
show_module: bool,
242259
show_file: bool,
243260
show_line: bool,
261+
show_timestamp: bool,
244262
log_level: LevelFilter,
245263
}
246264

@@ -269,6 +287,14 @@ impl Log for StdoutLogger {
269287

270288
// Operate in a scope of borrowed writer.
271289
WRITER.with_borrow_mut(|writer| {
290+
// Write timestamp.
291+
if self.show_timestamp {
292+
let timestamp = UtcDateTime::now()
293+
.format(&TIMESTAMP_FORMAT)
294+
.expect("failed to format timestamp");
295+
let _ = score_write!(writer, "[{}]", timestamp);
296+
}
297+
272298
// Write module, file and line.
273299
if self.show_module || self.show_file || self.show_line {
274300
let _ = score_write!(writer, "[");

0 commit comments

Comments
 (0)