@@ -6,6 +6,7 @@ use std::path::PathBuf;
6
6
pub enum Severity {
7
7
Error ,
8
8
Warning ,
9
+ #[ allow( unused) ]
9
10
Note ,
10
11
}
11
12
@@ -42,13 +43,13 @@ pub struct Location {
42
43
/** Path to the affected file if appropriate, relative to the source root */
43
44
pub file : Option < String > ,
44
45
#[ serde( skip_serializing_if = "Option::is_none" ) ]
45
- pub start_line : Option < i32 > ,
46
+ pub start_line : Option < usize > ,
46
47
#[ serde( skip_serializing_if = "Option::is_none" ) ]
47
- pub start_column : Option < i32 > ,
48
+ pub start_column : Option < usize > ,
48
49
#[ serde( skip_serializing_if = "Option::is_none" ) ]
49
- pub end_line : Option < i32 > ,
50
+ pub end_line : Option < usize > ,
50
51
#[ serde( skip_serializing_if = "Option::is_none" ) ]
51
- pub end_column : Option < i32 > ,
52
+ pub end_column : Option < usize > ,
52
53
}
53
54
54
55
#[ derive( Serialize ) ]
@@ -81,12 +82,45 @@ fn is_default_visibility(v: &Visibility) -> bool {
81
82
}
82
83
83
84
pub struct LogWriter {
85
+ extractor : String ,
84
86
path : Option < PathBuf > ,
85
87
inner : Option < std:: io:: BufWriter < std:: fs:: File > > ,
86
88
}
87
89
88
90
impl LogWriter {
89
- pub fn write ( & mut self , mesg : & DiagnosticMessage ) -> std:: io:: Result < ( ) > {
91
+ pub fn message ( & self , id : & str , name : & str ) -> DiagnosticMessage {
92
+ DiagnosticMessage {
93
+ timestamp : std:: time:: SystemTime :: now ( )
94
+ . duration_since ( std:: time:: UNIX_EPOCH )
95
+ . expect ( "" )
96
+ . as_millis ( ) as u64 ,
97
+ source : Source {
98
+ id : format ! ( "{}/{}" , self . extractor, id) ,
99
+ name : name. to_owned ( ) ,
100
+ extractor_name : Some ( self . extractor . to_owned ( ) ) ,
101
+ } ,
102
+ markdown_message : String :: new ( ) ,
103
+ plaintext_message : String :: new ( ) ,
104
+ help_links : vec ! [ ] ,
105
+ severity : None ,
106
+ internal : false ,
107
+ visibility : Visibility {
108
+ cli_summary_table : false ,
109
+ status_page : false ,
110
+ telemetry : false ,
111
+ } ,
112
+ location : None ,
113
+ }
114
+ }
115
+ pub fn write ( & mut self , mesg : & DiagnosticMessage ) {
116
+ let full_error_message = mesg. full_error_message ( ) ;
117
+
118
+ match mesg. severity {
119
+ Some ( Severity :: Error ) => tracing:: error!( "{}" , full_error_message) ,
120
+ Some ( Severity :: Warning ) => tracing:: warn!( "{}" , full_error_message) ,
121
+ Some ( Severity :: Note ) => tracing:: info!( "{}" , full_error_message) ,
122
+ None => tracing:: debug!( "{}" , full_error_message) ,
123
+ }
90
124
if self . inner . is_none ( ) {
91
125
let mut open_failed = false ;
92
126
self . inner = self . path . as_ref ( ) . and_then ( |path| {
@@ -113,10 +147,12 @@ impl LogWriter {
113
147
}
114
148
}
115
149
if let Some ( mut writer) = self . inner . as_mut ( ) {
116
- serde_json:: to_writer ( & mut writer, mesg) ?;
117
- & mut writer. write_all ( b"\n " ) ?;
150
+ serde_json:: to_writer ( & mut writer, mesg)
151
+ . unwrap_or_else ( |e| tracing:: debug!( "Failed to write log entry: {}" , e) ) ;
152
+ & mut writer
153
+ . write_all ( b"\n " )
154
+ . unwrap_or_else ( |e| tracing:: debug!( "Failed to write log entry: {}" , e) ) ;
118
155
}
119
- Ok ( ( ) )
120
156
}
121
157
}
122
158
@@ -134,7 +170,7 @@ impl DiagnosticLoggers {
134
170
135
171
let root = match std:: env:: var ( & env_var) {
136
172
Err ( e) => {
137
- tracing:: error!( "{}: {}" , & env_var , e ) ;
173
+ tracing:: error!( "{}: {}" , e , & env_var ) ;
138
174
None
139
175
}
140
176
Ok ( dir) => {
@@ -160,37 +196,14 @@ impl DiagnosticLoggers {
160
196
} ;
161
197
}
162
198
THREAD_NUM . with ( |n| LogWriter {
199
+ extractor : self . extractor . to_owned ( ) ,
163
200
inner : None ,
164
201
path : self
165
202
. root
166
203
. as_ref ( )
167
204
. map ( |root| root. to_owned ( ) . join ( format ! ( "extractor_{}.jsonl" , n) ) ) ,
168
205
} )
169
206
}
170
- pub fn message ( & self , id : & str , name : & str ) -> DiagnosticMessage {
171
- DiagnosticMessage {
172
- timestamp : std:: time:: SystemTime :: now ( )
173
- . duration_since ( std:: time:: UNIX_EPOCH )
174
- . expect ( "" )
175
- . as_millis ( ) as u64 ,
176
- source : Source {
177
- id : id. to_owned ( ) ,
178
- name : name. to_owned ( ) ,
179
- extractor_name : Some ( self . extractor . to_owned ( ) ) ,
180
- } ,
181
- markdown_message : String :: new ( ) ,
182
- plaintext_message : String :: new ( ) ,
183
- help_links : vec ! [ ] ,
184
- severity : None ,
185
- internal : false ,
186
- visibility : Visibility {
187
- cli_summary_table : false ,
188
- status_page : false ,
189
- telemetry : false ,
190
- } ,
191
- location : None ,
192
- }
193
- }
194
207
}
195
208
static EMPTY_LOCATION : Location = Location {
196
209
file : None ,
@@ -200,10 +213,23 @@ static EMPTY_LOCATION: Location = Location {
200
213
end_column : None ,
201
214
} ;
202
215
impl DiagnosticMessage {
216
+ pub fn full_error_message ( & self ) -> String {
217
+ match & self . location {
218
+ Some ( Location {
219
+ file : Some ( path) ,
220
+ start_line : Some ( line) ,
221
+ ..
222
+ } ) => format ! ( "{}:{}: {}" , path, line, self . plaintext_message) ,
223
+ _ => self . plaintext_message . to_owned ( ) ,
224
+ }
225
+ }
226
+
203
227
pub fn text < ' a > ( & ' a mut self , text : & str ) -> & ' a mut Self {
204
228
self . plaintext_message = text. to_owned ( ) ;
205
229
self
206
230
}
231
+
232
+ #[ allow( unused) ]
207
233
pub fn markdown < ' a > ( & ' a mut self , text : & str ) -> & ' a mut Self {
208
234
self . markdown_message = text. to_owned ( ) ;
209
235
self
@@ -212,14 +238,17 @@ impl DiagnosticMessage {
212
238
self . severity = Some ( severity) ;
213
239
self
214
240
}
241
+ #[ allow( unused) ]
215
242
pub fn help_link < ' a > ( & ' a mut self , link : & str ) -> & ' a mut Self {
216
243
self . help_links . push ( link. to_owned ( ) ) ;
217
244
self
218
245
}
246
+ #[ allow( unused) ]
219
247
pub fn internal < ' a > ( & ' a mut self ) -> & ' a mut Self {
220
248
self . internal = true ;
221
249
self
222
250
}
251
+ #[ allow( unused) ]
223
252
pub fn cli_summary_table < ' a > ( & ' a mut self ) -> & ' a mut Self {
224
253
self . visibility . cli_summary_table = true ;
225
254
self
@@ -228,36 +257,25 @@ impl DiagnosticMessage {
228
257
self . visibility . status_page = true ;
229
258
self
230
259
}
260
+ #[ allow( unused) ]
231
261
pub fn telemetry < ' a > ( & ' a mut self ) -> & ' a mut Self {
232
262
self . visibility . telemetry = true ;
233
263
self
234
264
}
235
- pub fn file < ' a > ( & ' a mut self , path : & str ) -> & ' a mut Self {
236
- self . location . get_or_insert ( EMPTY_LOCATION . to_owned ( ) ) . file = Some ( path. to_owned ( ) ) ;
237
- self
238
- }
239
- pub fn start_line < ' a > ( & ' a mut self , start_line : i32 ) -> & ' a mut Self {
240
- self . location
241
- . get_or_insert ( EMPTY_LOCATION . to_owned ( ) )
242
- . start_line = Some ( start_line) ;
243
- self
244
- }
245
- pub fn start_column < ' a > ( & ' a mut self , start_column : i32 ) -> & ' a mut Self {
246
- self . location
247
- . get_or_insert ( EMPTY_LOCATION . to_owned ( ) )
248
- . start_column = Some ( start_column) ;
249
- self
250
- }
251
- pub fn end_line < ' a > ( & ' a mut self , end_line : i32 ) -> & ' a mut Self {
252
- self . location
253
- . get_or_insert ( EMPTY_LOCATION . to_owned ( ) )
254
- . end_line = Some ( end_line) ;
255
- self
256
- }
257
- pub fn end_column < ' a > ( & ' a mut self , end_column : i32 ) -> & ' a mut Self {
258
- self . location
259
- . get_or_insert ( EMPTY_LOCATION . to_owned ( ) )
260
- . end_column = Some ( end_column) ;
265
+ pub fn location < ' a > (
266
+ & ' a mut self ,
267
+ path : & str ,
268
+ start_line : usize ,
269
+ start_column : usize ,
270
+ end_line : usize ,
271
+ end_column : usize ,
272
+ ) -> & ' a mut Self {
273
+ let loc = self . location . get_or_insert ( EMPTY_LOCATION . to_owned ( ) ) ;
274
+ loc. file = Some ( path. to_owned ( ) ) ;
275
+ loc. start_line = Some ( start_line) ;
276
+ loc. start_column = Some ( start_column) ;
277
+ loc. end_line = Some ( end_line) ;
278
+ loc. end_column = Some ( end_column) ;
261
279
self
262
280
}
263
281
}
0 commit comments