1717
1818use crate :: config:: { BooleanKey , StringKey } ;
1919use crate :: metadata:: now_unix_timestamp;
20- use env_logger:: Target :: Stdout ;
20+
21+ use env_logger:: Target :: { Stderr , Stdout } ;
2122use env_logger:: DEFAULT_FILTER_ENV ;
2223use log:: LevelFilter :: { Debug , Info , Trace } ;
2324use log:: { Level , LevelFilter } ;
2425use serde:: { Deserialize , Serialize } ;
2526use std:: cell:: RefCell ;
2627use std:: env;
2728use std:: fmt:: Display ;
28- use std:: ops:: Deref ;
2929use std:: str:: FromStr ;
3030
3131pub const DRIVER_PATH : & str = "Driver path: " ;
@@ -37,6 +37,7 @@ enum OutputType {
3737 Logger ,
3838 Json ,
3939 Shell ,
40+ Mixed ,
4041}
4142
4243#[ derive( Default ) ]
@@ -45,6 +46,7 @@ pub struct Logger {
4546 trace : bool ,
4647 output : OutputType ,
4748 json : RefCell < JsonOutput > ,
49+ minimal_json : RefCell < MinimalJson > ,
4850}
4951
5052#[ derive( Default , Serialize , Deserialize ) ]
@@ -68,6 +70,13 @@ pub struct JsonOutput {
6870 pub result : Result ,
6971}
7072
73+ #[ derive( Default , Serialize , Deserialize ) ]
74+ pub struct MinimalJson {
75+ pub driver_path : String ,
76+ pub browser_path : String ,
77+ pub error : String ,
78+ }
79+
7180impl Logger {
7281 pub fn new ( ) -> Self {
7382 let debug = BooleanKey ( "debug" , false ) . get_value ( ) ;
@@ -82,13 +91,15 @@ impl Logger {
8291 output_type = OutputType :: Json ;
8392 } else if output. eq_ignore_ascii_case ( "shell" ) {
8493 output_type = OutputType :: Shell ;
94+ } else if output. eq_ignore_ascii_case ( "mixed" ) {
95+ output_type = OutputType :: Mixed ;
8596 } else {
8697 output_type = OutputType :: Logger ;
8798 }
8899 match output_type {
89- OutputType :: Logger => {
100+ OutputType :: Logger | OutputType :: Mixed => {
90101 if env:: var ( DEFAULT_FILTER_ENV ) . unwrap_or_default ( ) . is_empty ( ) {
91- let filter = if !log_level. is_empty ( ) {
102+ let mut filter = if !log_level. is_empty ( ) {
92103 LevelFilter :: from_str ( log_level) . unwrap_or ( Info )
93104 } else {
94105 let mut filter = match debug {
@@ -100,9 +111,17 @@ impl Logger {
100111 }
101112 filter
102113 } ;
114+ if trace {
115+ filter = Trace
116+ }
117+ let target = if output_type == OutputType :: Logger {
118+ Stdout
119+ } else {
120+ Stderr
121+ } ;
103122 env_logger:: Builder :: new ( )
104123 . filter_module ( env ! ( "CARGO_CRATE_NAME" ) , filter)
105- . target ( Stdout )
124+ . target ( target )
106125 . format_target ( false )
107126 . format_timestamp_millis ( )
108127 . try_init ( )
@@ -131,6 +150,7 @@ impl Logger {
131150 browser_path : "" . to_string ( ) ,
132151 } ,
133152 } ) ,
153+ minimal_json : RefCell :: new ( Default :: default ( ) ) ,
134154 }
135155 }
136156
@@ -173,12 +193,11 @@ impl Logger {
173193 }
174194 if level == Level :: Info || level <= Level :: Error {
175195 if message. starts_with ( DRIVER_PATH ) {
176- let driver_path = message. replace ( DRIVER_PATH , "" ) ;
177- self . json . borrow_mut ( ) . result . driver_path = driver_path. to_owned ( ) ;
178- self . json . borrow_mut ( ) . result . message = driver_path;
196+ self . json . borrow_mut ( ) . result . driver_path =
197+ self . clean_driver_path ( & message) ;
179198 } else if message. starts_with ( BROWSER_PATH ) {
180- let browser_path = message . replace ( BROWSER_PATH , "" ) ;
181- self . json . borrow_mut ( ) . result . browser_path = browser_path ;
199+ self . json . borrow_mut ( ) . result . browser_path =
200+ self . clean_browser_path ( & message ) ;
182201 } else {
183202 self . json . borrow_mut ( ) . result . message = message;
184203 }
@@ -192,11 +211,31 @@ impl Logger {
192211 }
193212 }
194213 _ => {
214+ if self . output == OutputType :: Mixed && level == Level :: Info || level <= Level :: Error
215+ {
216+ if message. starts_with ( DRIVER_PATH ) {
217+ self . minimal_json . borrow_mut ( ) . driver_path =
218+ self . clean_driver_path ( & message) ;
219+ } else if message. starts_with ( BROWSER_PATH ) {
220+ self . minimal_json . borrow_mut ( ) . browser_path =
221+ self . clean_browser_path ( & message) ;
222+ } else {
223+ self . minimal_json . borrow_mut ( ) . error = message. clone ( ) ;
224+ }
225+ }
195226 log:: log!( level, "{}" , message) ;
196227 }
197228 }
198229 }
199230
231+ fn clean_driver_path ( & self , message : & str ) -> String {
232+ message. replace ( DRIVER_PATH , "" )
233+ }
234+
235+ fn clean_browser_path ( & self , message : & str ) -> String {
236+ message. replace ( BROWSER_PATH , "" )
237+ }
238+
200239 fn create_json_log ( & self , message : String , level : Level ) -> Logs {
201240 Logs {
202241 level : level. to_string ( ) . to_uppercase ( ) ,
@@ -205,17 +244,22 @@ impl Logger {
205244 }
206245 }
207246
247+ fn get_json_blog < T > ( & self , json_output : & T ) -> String
248+ where
249+ T : Serialize ,
250+ {
251+ serde_json:: to_string_pretty ( json_output) . unwrap ( )
252+ }
253+
208254 pub fn set_code ( & self , code : i32 ) {
209255 self . json . borrow_mut ( ) . result . code = code;
210256 }
211257
212258 pub fn flush ( & self ) {
213- let json_output = & self . json . borrow ( ) ;
214- let json = json_output. deref ( ) ;
215- if !json. logs . is_empty ( ) {
216- print ! ( "{}" , serde_json:: to_string_pretty( json) . unwrap( ) ) ;
217- } else if self . output == OutputType :: Json {
218- panic ! ( "JSON output has been specified, but no entries have been collected" )
259+ if self . output == OutputType :: Json {
260+ print ! ( "{}" , self . get_json_blog( & self . json) ) ;
261+ } else if self . output == OutputType :: Mixed {
262+ print ! ( "{}" , self . get_json_blog( & self . minimal_json) ) ;
219263 }
220264 }
221265}
0 commit comments