@@ -250,37 +250,53 @@ let get_pos_info pos =
250
250
(pos.pos_fname, pos.pos_lnum, pos.pos_cnum - pos.pos_bol)
251
251
;;
252
252
253
+ let setup_colors () =
254
+ Misc.Color. setup ! Clflags. color
255
+
253
256
let print_loc ppf loc =
257
+ setup_colors () ;
254
258
let (file, line, startchar) = get_pos_info loc.loc_start in
255
259
let endchar = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + startchar in
256
260
if file = " //toplevel//" then begin
257
261
if highlight_locations ppf [loc] then () else
258
262
fprintf ppf " Characters %i-%i"
259
263
loc.loc_start.pos_cnum loc.loc_end.pos_cnum
260
264
end else begin
261
- fprintf ppf " %s%a%s%i" msg_file print_filename file msg_line line;
265
+ fprintf ppf " %s@{<loc> %a%s%i" msg_file print_filename file msg_line line;
262
266
if startchar > = 0 then
263
- fprintf ppf " %s%i%s%i" msg_chars startchar msg_to endchar
267
+ fprintf ppf " %s%i%s%i" msg_chars startchar msg_to endchar;
268
+ fprintf ppf " @}"
264
269
end
265
270
;;
266
271
267
272
let print ppf loc =
273
+ setup_colors () ;
268
274
if loc.loc_start.pos_fname = " //toplevel//"
269
275
&& highlight_locations ppf [loc] then ()
270
- else fprintf ppf " %a%s@." print_loc loc msg_colon
276
+ else fprintf ppf " @{<loc>%a@}%s@." print_loc loc msg_colon
277
+ ;;
278
+
279
+ let error_prefix = " Error"
280
+ let warning_prefix = " Warning"
281
+
282
+ let print_error_prefix ppf () =
283
+ setup_colors () ;
284
+ fprintf ppf " @{<error>%s@}:" error_prefix;
285
+ ()
271
286
;;
272
287
273
288
let print_error ppf loc =
274
289
print ppf loc;
275
- fprintf ppf " Error: " ;
290
+ print_error_prefix ppf ()
276
291
;;
277
292
278
- let print_error_cur_file ppf = print_error ppf (in_file ! input_name);;
293
+ let print_error_cur_file ppf () = print_error ppf (in_file ! input_name);;
279
294
280
295
let default_warning_printer loc ppf w =
281
296
if Warnings. is_active w then begin
297
+ setup_colors () ;
282
298
print ppf loc;
283
- fprintf ppf " Warning %a@." Warnings. print w
299
+ fprintf ppf " @{<warning>%s@} %a@." warning_prefix Warnings. print w
284
300
end
285
301
;;
286
302
@@ -314,8 +330,31 @@ type error =
314
330
if_highlight : string ; (* alternative message if locations are highlighted *)
315
331
}
316
332
317
- let errorf ?(loc = none) ?(sub = [] ) ?(if_highlight = " " ) =
318
- Printf. ksprintf (fun msg -> {loc; msg; sub; if_highlight})
333
+ let pp_ksprintf ?before k fmt =
334
+ let buf = Buffer. create 64 in
335
+ let ppf = Format. formatter_of_buffer buf in
336
+ Misc.Color. set_color_tag_handling ppf;
337
+ begin match before with
338
+ | None -> ()
339
+ | Some f -> f ppf
340
+ end ;
341
+ kfprintf
342
+ (fun _ ->
343
+ pp_print_flush ppf () ;
344
+ let msg = Buffer. contents buf in
345
+ k msg)
346
+ ppf fmt
347
+
348
+ let errorf ?(loc = none) ?(sub = [] ) ?(if_highlight = " " ) fmt =
349
+ pp_ksprintf
350
+ (fun msg -> {loc; msg; sub; if_highlight})
351
+ fmt
352
+
353
+ let errorf_prefixed ?(loc =none) ?(sub =[] ) ?(if_highlight =" " ) fmt =
354
+ pp_ksprintf
355
+ ~before: (fun ppf -> fprintf ppf " %a " print_error_prefix () )
356
+ (fun msg -> {loc; msg; sub; if_highlight})
357
+ fmt
319
358
320
359
let error ?(loc = none) ?(sub = [] ) ?(if_highlight = " " ) msg =
321
360
{loc; msg; sub; if_highlight}
@@ -361,13 +400,7 @@ let report_error ppf err =
361
400
;;
362
401
363
402
let error_of_printer loc print x =
364
- let buf = Buffer. create 64 in
365
- let ppf = Format. formatter_of_buffer buf in
366
- pp_print_string ppf " Error: " ;
367
- print ppf x;
368
- pp_print_flush ppf () ;
369
- let msg = Buffer. contents buf in
370
- errorf ~loc " %s" msg
403
+ errorf_prefixed ~loc " %a@?" print x
371
404
372
405
let error_of_printer_file print x =
373
406
error_of_printer (in_file ! input_name) print x
@@ -376,11 +409,12 @@ let () =
376
409
register_error_of_exn
377
410
(function
378
411
| Sys_error msg ->
379
- Some (errorf ~loc: (in_file ! input_name) " Error: I/O error: %s" msg)
412
+ Some (errorf_prefixed ~loc: (in_file ! input_name)
413
+ " I/O error: %s" msg)
380
414
| Warnings. Errors n ->
381
415
Some
382
- (errorf ~loc: (in_file ! input_name)
383
- " Error: Some fatal warnings were triggered (%d occurrences)" n)
416
+ (errorf_prefixed ~loc: (in_file ! input_name)
417
+ " Some fatal warnings were triggered (%d occurrences)" n)
384
418
| _ ->
385
419
None
386
420
)
@@ -407,4 +441,4 @@ let () =
407
441
)
408
442
409
443
let raise_errorf ?(loc = none) ?(sub = [] ) ?(if_highlight = " " ) =
410
- Printf. ksprintf (fun msg -> raise (Error ({loc; msg; sub; if_highlight})))
444
+ pp_ksprintf (fun msg -> raise (Error ({loc; msg; sub; if_highlight})))
0 commit comments