Debian Codegraph @

lib/cDebug.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 type flag = bool ref
12
13 type t = (unit -> Pp.t) -> unit
14
15 let debug = ref CString.Map.empty
16
17 (* Used to remember level of Set Debug "all" for debugs created by
18 plugins dynlinked after the Set *)
19 let all_flag = ref false
20
21 let set_debug_backtrace b =
22 Exninfo.record_backtrace b
23
24 let set_debug_all b =
25 set_debug_backtrace b;
26 CString.Map.iter (fun _ flag -> flag := b) !debug;
27 all_flag := b
28
29 let create_full ~name () =
30 let anomaly pp = CErrors.anomaly ~label:"CDebug.create" pp in
31 let () = match name with
32 | "all"|"backtrace" -> anomaly Pp.(str"The debug name \""++str name++str"\" is reserved.")
33 | _ ->
34 if CString.Map.mem name !debug then
35 anomaly Pp.(str "The debug name \"" ++ str name ++ str "\" is already used.")
36 in
37 let pp x =
38 Feedback.msg_debug Pp.(hv 2 (str "[" ++ str name ++ str "]" ++ spc() ++ x))
39 in
40 let flag = ref !all_flag in
41 debug := CString.Map.add name flag !debug;
42 let pp x =
43 if !flag
44 then pp (x ())
45 in
46 flag, pp
47
48 let create ~name () =
49 snd (create_full ~name ())
50
51 let get_flag flag = !flag
52
53 let set_flag flag v = flag := v
54
55 let warn_unknown_debug = CWarnings.create ~name:"unknown-debug-flag"
56 Pp.(fun name -> str "There is no debug flag \"" ++ str name ++ str "\".")
57
58 let get_flags () =
59 let pp_flag name flag = if flag then name else "-"^name in
60 let flags =
61 CString.Map.fold
62 (fun name v acc -> pp_flag name !v :: acc)
63 !debug []
64 in
65 let all = pp_flag "all" !all_flag in
66 let bt = pp_flag "backtrace" (Printexc.backtrace_status()) in
67 String.concat "," (all::bt::flags)
68
69 exception Error
70
71 let parse_flags s =
72 let parse_flag s =
73 if CString.is_empty s then raise Error
74 else if s.[0] = '-'
75 then String.sub s 1 (String.length s - 1), false
76 else s, true
77 in
78 try
79 Some (CList.map parse_flag @@ String.split_on_char ',' s)
80 with Error -> None
81
82 let set_flags s = match parse_flags s with
83 | None -> CErrors.user_err Pp.(str "Syntax error in debug flags.")
84 | Some flags ->
85 let set_one_flag (name,b) = match name with
86 | "all" -> set_debug_all b
87 | "backtrace" -> set_debug_backtrace b
88 | _ -> match CString.Map.find_opt name !debug with
89 | None -> warn_unknown_debug name
90 | Some flag -> flag := b
91 in
92 List.iter set_one_flag flags
93
94 let misc, pp_misc = create_full ~name:"misc" ()