1
1
//! A module to initialize and customize the logger object used in (most) stdout.
2
2
3
- use std:: env;
3
+ use std:: {
4
+ env,
5
+ io:: { stdout, Write } ,
6
+ } ;
4
7
5
8
use colored:: { control:: set_override, Colorize } ;
6
- use log:: { Level , LevelFilter , Metadata , Record } ;
9
+ use log:: { Level , LevelFilter , Log , Metadata , Record } ;
7
10
8
11
#[ derive( Default ) ]
9
12
struct SimpleLogger ;
@@ -21,21 +24,43 @@ impl SimpleLogger {
21
24
}
22
25
}
23
26
24
- impl log :: Log for SimpleLogger {
27
+ impl Log for SimpleLogger {
25
28
fn enabled ( & self , metadata : & Metadata ) -> bool {
26
29
metadata. level ( ) <= log:: max_level ( )
27
30
}
28
31
29
32
fn log ( & self , record : & Record ) {
33
+ let mut stdout = stdout ( ) . lock ( ) ;
30
34
if record. target ( ) == "CI_LOG_GROUPING" {
31
35
// 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" ) ;
33
40
} 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" ) ;
39
64
}
40
65
}
41
66
0 commit comments