@@ -18,6 +18,13 @@ use core::cell::RefCell;
1818use core:: fmt:: Write ;
1919use score_log:: fmt:: { score_write, Error , FormatSpec , Result , ScoreWrite } ;
2020use 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.
2330struct 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