18
18
use crate :: config:: BooleanKey ;
19
19
use crate :: metadata:: now_unix_timestamp;
20
20
use env_logger:: fmt:: Color ;
21
- use env_logger:: Target :: Stdout ;
21
+ use env_logger:: Target :: { Stderr , Stdout } ;
22
22
use env_logger:: DEFAULT_FILTER_ENV ;
23
23
use log:: Level ;
24
24
use log:: LevelFilter :: { Debug , Info , Trace } ;
@@ -27,7 +27,6 @@ use std::cell::RefCell;
27
27
use std:: env;
28
28
use std:: fmt:: Display ;
29
29
use std:: io:: Write ;
30
- use std:: ops:: Deref ;
31
30
use Color :: { Blue , Cyan , Green , Red , Yellow } ;
32
31
33
32
pub const DRIVER_PATH : & str = "Driver path: " ;
@@ -39,6 +38,7 @@ enum OutputType {
39
38
Logger ,
40
39
Json ,
41
40
Shell ,
41
+ Mixed ,
42
42
}
43
43
44
44
#[ derive( Default ) ]
@@ -47,6 +47,7 @@ pub struct Logger {
47
47
trace : bool ,
48
48
output : OutputType ,
49
49
json : RefCell < JsonOutput > ,
50
+ minimal_json : RefCell < MinimalJson > ,
50
51
}
51
52
52
53
#[ derive( Default , Serialize , Deserialize ) ]
@@ -70,6 +71,12 @@ pub struct JsonOutput {
70
71
pub result : Result ,
71
72
}
72
73
74
+ #[ derive( Default , Serialize , Deserialize ) ]
75
+ pub struct MinimalJson {
76
+ pub driver_path : String ,
77
+ pub browser_path : String ,
78
+ }
79
+
73
80
impl Logger {
74
81
pub fn new ( ) -> Self {
75
82
let debug = BooleanKey ( "debug" , false ) . get_value ( ) ;
@@ -83,11 +90,13 @@ impl Logger {
83
90
output_type = OutputType :: Json ;
84
91
} else if output. eq_ignore_ascii_case ( "shell" ) {
85
92
output_type = OutputType :: Shell ;
93
+ } else if output. eq_ignore_ascii_case ( "mixed" ) {
94
+ output_type = OutputType :: Mixed ;
86
95
} else {
87
96
output_type = OutputType :: Logger ;
88
97
}
89
98
match output_type {
90
- OutputType :: Logger => {
99
+ OutputType :: Logger | OutputType :: Mixed => {
91
100
if env:: var ( DEFAULT_FILTER_ENV ) . unwrap_or_default ( ) . is_empty ( ) {
92
101
let mut filter = match debug {
93
102
true => Debug ,
@@ -96,9 +105,14 @@ impl Logger {
96
105
if trace {
97
106
filter = Trace
98
107
}
108
+ let target = if output_type == OutputType :: Logger {
109
+ Stdout
110
+ } else {
111
+ Stderr
112
+ } ;
99
113
env_logger:: Builder :: new ( )
100
114
. filter_module ( env ! ( "CARGO_CRATE_NAME" ) , filter)
101
- . target ( Stdout )
115
+ . target ( target )
102
116
. format ( |buf, record| {
103
117
let mut level_style = buf. style ( ) ;
104
118
match record. level ( ) {
@@ -141,6 +155,7 @@ impl Logger {
141
155
browser_path : "" . to_string ( ) ,
142
156
} ,
143
157
} ) ,
158
+ minimal_json : RefCell :: new ( Default :: default ( ) ) ,
144
159
}
145
160
}
146
161
@@ -178,12 +193,11 @@ impl Logger {
178
193
}
179
194
if level == Level :: Info || level <= Level :: Error {
180
195
if message. starts_with ( DRIVER_PATH ) {
181
- let driver_path = message. replace ( DRIVER_PATH , "" ) ;
182
- self . json . borrow_mut ( ) . result . driver_path = driver_path. to_owned ( ) ;
183
- self . json . borrow_mut ( ) . result . message = driver_path;
196
+ self . json . borrow_mut ( ) . result . driver_path =
197
+ self . clean_driver_path ( & message) ;
184
198
} else if message. starts_with ( BROWSER_PATH ) {
185
- let browser_path = message . replace ( BROWSER_PATH , "" ) ;
186
- self . json . borrow_mut ( ) . result . browser_path = browser_path ;
199
+ self . json . borrow_mut ( ) . result . browser_path =
200
+ self . clean_browser_path ( & message ) ;
187
201
} else {
188
202
self . json . borrow_mut ( ) . result . message = message;
189
203
}
@@ -197,11 +211,28 @@ impl Logger {
197
211
}
198
212
}
199
213
_ => {
214
+ if self . output == OutputType :: Mixed && level == Level :: Info {
215
+ if message. starts_with ( DRIVER_PATH ) {
216
+ self . minimal_json . borrow_mut ( ) . driver_path =
217
+ self . clean_driver_path ( & message) ;
218
+ } else if message. starts_with ( BROWSER_PATH ) {
219
+ self . minimal_json . borrow_mut ( ) . browser_path =
220
+ self . clean_browser_path ( & message) ;
221
+ }
222
+ }
200
223
log:: log!( level, "{}" , message) ;
201
224
}
202
225
}
203
226
}
204
227
228
+ fn clean_driver_path ( & self , message : & str ) -> String {
229
+ message. replace ( DRIVER_PATH , "" )
230
+ }
231
+
232
+ fn clean_browser_path ( & self , message : & str ) -> String {
233
+ message. replace ( BROWSER_PATH , "" )
234
+ }
235
+
205
236
fn create_json_log ( & self , message : String , level : Level ) -> Logs {
206
237
Logs {
207
238
level : level. to_string ( ) . to_uppercase ( ) ,
@@ -210,17 +241,22 @@ impl Logger {
210
241
}
211
242
}
212
243
244
+ fn get_json_blog < T > ( & self , json_output : & T ) -> String
245
+ where
246
+ T : Serialize ,
247
+ {
248
+ serde_json:: to_string_pretty ( json_output) . unwrap ( )
249
+ }
250
+
213
251
pub fn set_code ( & self , code : i32 ) {
214
252
self . json . borrow_mut ( ) . result . code = code;
215
253
}
216
254
217
255
pub fn flush ( & self ) {
218
- let json_output = & self . json . borrow ( ) ;
219
- let json = json_output. deref ( ) ;
220
- if !json. logs . is_empty ( ) {
221
- print ! ( "{}" , serde_json:: to_string_pretty( json) . unwrap( ) ) ;
222
- } else if self . output == OutputType :: Json {
223
- panic ! ( "JSON output has been specified, but no entries have been collected" )
256
+ if self . output == OutputType :: Json {
257
+ println ! ( "{}" , self . get_json_blog( & self . json) ) ;
258
+ } else if self . output == OutputType :: Mixed {
259
+ println ! ( "{}" , self . get_json_blog( & self . minimal_json) ) ;
224
260
}
225
261
}
226
262
}
0 commit comments