11//! A module to initialize and customize the logger object used in (most) stdout.
22
3- use std:: env;
3+ use std:: {
4+ env,
5+ io:: { stdout, Write } ,
6+ } ;
47
58use colored:: { control:: set_override, Colorize } ;
6- use log:: { Level , LevelFilter , Metadata , Record } ;
9+ use log:: { Level , LevelFilter , Log , Metadata , Record } ;
710
811#[ derive( Default ) ]
912struct SimpleLogger ;
@@ -21,21 +24,43 @@ impl SimpleLogger {
2124 }
2225}
2326
24- impl log :: Log for SimpleLogger {
27+ impl Log for SimpleLogger {
2528 fn enabled ( & self , metadata : & Metadata ) -> bool {
2629 metadata. level ( ) <= log:: max_level ( )
2730 }
2831
2932 fn log ( & self , record : & Record ) {
33+ let mut stdout = stdout ( ) . lock ( ) ;
3034 if record. target ( ) == "CI_LOG_GROUPING" {
3135 // this log is meant to manipulate a CI workflow's log grouping
32- println ! ( "{}" , record. args( ) ) ;
36+ writeln ! ( stdout, "{}" , record. args( ) ) . expect ( "Failed to write log command to stdout" ) ;
37+ stdout
38+ . flush ( )
39+ . expect ( "Failed to flush log command to stdout" ) ;
3340 } else if self . enabled ( record. metadata ( ) ) {
34- println ! (
35- "[{}]: {}" ,
36- Self :: level_color( & record. level( ) ) ,
37- record. args( )
38- ) ;
41+ let module = record. module_path ( ) ;
42+ if module. is_none_or ( |v| v. starts_with ( "cpp_linter" ) ) {
43+ writeln ! (
44+ stdout,
45+ "[{}]: {}" ,
46+ Self :: level_color( & record. level( ) ) ,
47+ record. args( )
48+ )
49+ . expect ( "Failed to write log message to stdout" ) ;
50+ } else {
51+ writeln ! (
52+ stdout,
53+ "[{}]{{{}:{}}}: {}" ,
54+ Self :: level_color( & record. level( ) ) ,
55+ module. unwrap( ) , // safe to unwrap here because the None case is caught above
56+ record. line( ) . unwrap_or_default( ) ,
57+ record. args( )
58+ )
59+ . expect ( "Failed to write detailed log message to stdout" ) ;
60+ }
61+ stdout
62+ . flush ( )
63+ . expect ( "Failed to flush log message to stdout" ) ;
3964 }
4065 }
4166
0 commit comments