Debian Codegraph @

lib/feedback.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Xml_datatype
12
13 type level =
14 | Debug
15 | Info
16 | Notice
17 | Warning
18 | Error
19
20 let pp_lvl fmt lvl = let open Format in match lvl with
21 | Error -> fprintf fmt "Error: "
22 | Info -> fprintf fmt "Info: "
23 | Debug -> fprintf fmt "Debug: "
24 | Warning -> fprintf fmt "Warning: "
25 | Notice -> fprintf fmt ""
26
27 type doc_id = int
28 type route_id = int
29
30 type feedback_content =
31 | Processed
32 | Incomplete
33 | Complete
34 | ProcessingIn of string
35 | InProgress of int
36 | WorkerStatus of string * string
37 | AddedAxiom
38 | GlobRef of Loc.t * string * string * string * string
39 | GlobDef of Loc.t * string * string * string
40 | FileDependency of string option * string
41 | FileLoaded of string * string
42 (* Extra metadata *)
43 | Custom of Loc.t option * string * xml
44 (* Generic messages *)
45 | Message of level * Loc.t option * Quickfix.t list * Pp.t
46
47 type feedback = {
48 doc_id : doc_id; (* The document being concerned *)
49 span_id : Stateid.t;
50 route : route_id;
51 contents : feedback_content;
52 }
53
54 (** Feeders *)
55 let feeders : (int, feedback -> unit) Hashtbl.t = Hashtbl.create 7
56
57 let add_feeder =
58 let f_id = ref 0 in fun f ->
59 incr f_id;
60 Hashtbl.add feeders !f_id f;
61 !f_id
62
63 let del_feeder fid = Hashtbl.remove feeders fid
64
65 let default_route = 0
66 let span_id = ref Stateid.dummy
67 let doc_id = ref 0
68 let feedback_route = ref default_route
69
70 let set_id_for_feedback ?(route=default_route) d i =
71 doc_id := d;
72 span_id := i;
73 feedback_route := route
74
75 let warn_no_listeners = ref true
76 let feedback ?did ?id ?route what =
77 let m = {
78 contents = what;
79 route = Option.default !feedback_route route;
80 doc_id = Option.default !doc_id did;
81 span_id = Option.default !span_id id;
82 } in
83 if !warn_no_listeners && Hashtbl.length feeders = 0 then begin
84 Format.eprintf "Warning, feedback message received but no listener to handle it!@\n%!";
85 match m.contents with
86 | Message (lvl,_,_,msg) ->
87 Format.eprintf "%a%a" pp_lvl lvl Pp.pp_with msg
88 | _ -> ()
89 end ;
90 Hashtbl.iter (fun _ f -> f m) feeders
91
92 (* Logging messages *)
93 let feedback_logger ?loc ?(quickfix=[]) lvl msg =
94 feedback ~route:!feedback_route ~id:!span_id (Message (lvl, loc, quickfix, msg))
95
96 let msg_info ?loc x = feedback_logger ?loc Info x
97 let msg_notice ?loc x = feedback_logger ?loc Notice x
98 let msg_warning ?loc ?quickfix x = feedback_logger ?loc ?quickfix Warning x
99 (* let msg_error ?loc x = feedback_logger ?loc Error x *)
100 let msg_debug ?loc x = feedback_logger ?loc Debug x
101
102 (* Helper for tools willing to understand only the messages *)
103 let console_feedback_listener fmt =
104 let open Format in
105 let pp_loc fmt loc = let open Loc in match loc with
106 | None -> fprintf fmt ""
107 | Some loc ->
108 let where =
109 match loc.fname with InFile { file } -> file | ToplevelInput -> "Toplevel input" in
110 fprintf fmt "\"%s\", line %d, characters %d-%d:@\n"
111 where loc.line_nb (loc.bp-loc.bol_pos) (loc.ep-loc.bol_pos) in
112 let checker_feed (fb : feedback) =
113 match fb.contents with
114 | Processed -> ()
115 | Incomplete -> ()
116 | Complete -> ()
117 | ProcessingIn _ -> ()
118 | InProgress _ -> ()
119 | WorkerStatus (_,_) -> ()
120 | AddedAxiom -> ()
121 | GlobRef (_,_,_,_,_) -> ()
122 | GlobDef (_,_,_,_) -> ()
123 | FileDependency (_,_) -> ()
124 | FileLoaded (_,_) -> ()
125 | Custom (_,_,_) -> ()
126 | Message (lvl,loc,_,msg) ->
127 fprintf fmt "@[%a@]%a@[%a@]\n%!" pp_loc loc pp_lvl lvl Pp.pp_with msg
128 in checker_feed