Project

General

Profile

Download (7.27 KB) Statistics
| Branch: | Tag: | Revision:
1
(* This file is part of the Kind 2 model checker.
2

    
3
   Copyright (c) 2015 by the Board of Trustees of the University of Iowa
4

    
5
   Licensed under the Apache License, Version 2.0 (the "License"); you
6
   may not use this file except in compliance with the License.  You
7
   may obtain a copy of the License at
8

    
9
   http://www.apache.org/licenses/LICENSE-2.0 
10

    
11
   Unless required by applicable law or agreed to in writing, software
12
   distributed under the License is distributed on an "AS IS" BASIS,
13
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14
   implied. See the License for the specific language governing
15
   permissions and limitations under the License. 
16

    
17
   This module is adapted from the model checker Cubicle
18
   http://cubicle.lri.fr
19

    
20
*)
21

    
22
open Format
23
open KindLib
24

    
25
(* Set width of pretty printing boxes to number of columns *)
26
let vt_width =
27
  try
28
    let stty_size = syscall "stty size < /dev/tty 2> /dev/null" in
29
    let w = Scanf.sscanf stty_size "%d %d" (fun _ cols -> cols) in
30
    set_margin w;
31
    w
32
  with _ -> 80
33

    
34
let print_line = 
35
  let s = String.make vt_width '-' in
36
  fun fmt () -> fprintf fmt "%s@\n" s
37

    
38
let print_double_line = 
39
  let s = String.make vt_width '=' in
40
  fun fmt () -> fprintf fmt "%s@\n" s
41

    
42

    
43
let print_title fmt s =
44
  printf "%a" print_double_line ();
45
  printf "* @{<b>%s@}@\n" s;
46
  printf "%a" print_line ()
47

    
48

    
49
let rec print_list print sep fmt = function
50
  | [] -> ()
51
  | [e] -> print fmt e
52
  | e :: l ->
53
    print fmt e;
54
    fprintf fmt sep;
55
    print_list print sep fmt l
56

    
57

    
58
type style =
59
  | User of int
60
  | Normal
61
  | Bold
62
  | Bold_off
63
  | Dim
64
  | Underline
65
  | Underline_off
66
  | Inverse
67
  | Inverse_off
68
  | Blink_off
69
  | FG_Black
70
  | FG_Red
71
  | FG_Green
72
  | FG_Yellow
73
  | FG_Blue
74
  | FG_Magenta
75
  | FG_Cyan
76
  | FG_Gray
77
  | FG_Default
78
  | BG_Black
79
  | BG_Red
80
  | BG_Green
81
  | BG_Yellow
82
  | BG_Blue
83
  | BG_Magenta
84
  | BG_Cyan
85
  | BG_Gray
86
  | BG_Default
87
  | FG_Black_B
88
  | FG_Red_B
89
  | FG_Green_B
90
  | FG_Yellow_B
91
  | FG_Blue_B
92
  | FG_Magenta_B
93
  | FG_Cyan_B
94
  | FG_Gray_B
95
  | FG_Default_B
96
  | BG_Black_B
97
  | BG_Red_B
98
  | BG_Green_B
99
  | BG_Yellow_B
100
  | BG_Blue_B
101
  | BG_Magenta_B
102
  | BG_Cyan_B
103
  | BG_Gray_B
104
  | BG_Default_B
105

    
106

    
107
let assoc_style =  function
108
  | User i  -> "38;5;" ^ string_of_int i (* 256 colors *)
109
  | Normal -> "0"
110
  | Bold -> "1"
111
  | Bold_off -> "22"
112
  | Dim ->  "2"
113
  | Underline -> "4"
114
  | Underline_off -> "24"
115
  | Inverse -> "7"
116
  | Inverse_off -> "27"
117
  | Blink_off -> "22"
118
  | FG_Black -> "30"
119
  | FG_Red -> "31"
120
  | FG_Green -> "32"
121
  | FG_Yellow -> "33"
122
  | FG_Blue -> "34"
123
  | FG_Magenta -> "35"
124
  | FG_Cyan -> "36"
125
  | FG_Gray -> "37"
126
  | FG_Default -> "39"
127
  | BG_Black -> "40"
128
  | BG_Red -> "41"
129
  | BG_Green -> "42"
130
  | BG_Yellow -> "43"
131
  | BG_Blue -> "44"
132
  | BG_Magenta -> "45"
133
  | BG_Cyan -> "46"
134
  | BG_Gray -> "47"
135
  | BG_Default -> "49"
136
  | FG_Black_B -> "90"
137
  | FG_Red_B -> "91"
138
  | FG_Green_B -> "92"
139
  | FG_Yellow_B -> "93"
140
  | FG_Blue_B -> "94"
141
  | FG_Magenta_B -> "95"
142
  | FG_Cyan_B -> "96"
143
  | FG_Gray_B -> "97"
144
  | FG_Default_B -> "99"
145
  | BG_Black_B -> "100"
146
  | BG_Red_B -> "101"
147
  | BG_Green_B -> "102"
148
  | BG_Yellow_B -> "103"
149
  | BG_Blue_B -> "104"
150
  | BG_Magenta_B -> "105"
151
  | BG_Cyan_B -> "106"
152
  | BG_Gray_B -> "107"
153
  | BG_Default_B -> "109"
154

    
155

    
156
let style_of_tag = function
157
  | "n" -> Normal
158
  | "b" -> Bold
159
  | "/b" -> Bold_off
160
  | "dim" -> Dim
161
  | "u" -> Underline
162
  | "/u" -> Underline_off
163
  | "i" -> Inverse
164
  | "/i" -> Inverse_off
165
  | "/bl" -> Blink_off
166
  | "black" -> FG_Black
167
  | "red" -> FG_Red
168
  | "green" -> FG_Green
169
  | "yellow" -> FG_Yellow
170
  | "blue" -> FG_Blue
171
  | "magenta" -> FG_Magenta
172
  | "cyan" -> FG_Cyan
173
  | "gray" -> FG_Gray
174
  | "default" -> FG_Default
175
  | "bg_black" -> BG_Black
176
  | "bg_red" -> BG_Red
177
  | "bg_green" -> BG_Green
178
  | "bg_yellow" -> BG_Yellow
179
  | "bg_blue" -> BG_Blue
180
  | "bg_magenta" -> BG_Magenta
181
  | "bg_cyan" -> BG_Cyan
182
  | "bg_gray" -> BG_Gray
183
  | "bg_default" -> BG_Default
184
  | "black_b" -> FG_Black_B
185
  | "red_b" -> FG_Red_B
186
  | "green_b" -> FG_Green_B
187
  | "yellow_b" -> FG_Yellow_B
188
  | "blue_b" -> FG_Blue_B
189
  | "magenta_b" -> FG_Magenta_B
190
  | "cyan_b" -> FG_Cyan_B
191
  | "gray_b" -> FG_Gray_B
192
  | "default_b" -> FG_Default_B
193
  | "bg_black_b" -> BG_Black_B
194
  | "bg_red_b" -> BG_Red_B
195
  | "bg_green_b" -> BG_Green_B
196
  | "bg_yellow_b" -> BG_Yellow_B
197
  | "bg_blue_b" -> BG_Blue_B
198
  | "bg_magenta_b" -> BG_Magenta_B
199
  | "bg_cyan_b" -> BG_Cyan_B
200
  | "bg_gray_b" -> BG_Gray_B
201
  | "bg_default_b" -> BG_Default_B
202
  | t ->
203
    try Scanf.sscanf t "c:%d" (fun x -> User x)
204
    with Scanf.Scan_failure _ | End_of_file ->
205
      eprintf "tag : %s@." t;
206
      raise Not_found
207

    
208

    
209
let start_tag t = 
210
  try Printf.sprintf "[%sm" (assoc_style (style_of_tag t))
211
  with Not_found -> ""
212

    
213
let stop_tag t =
214
  (* try *)
215
    let st = match style_of_tag t with
216
      | Bold -> Bold_off
217
      | Underline -> Underline_off
218
      | Inverse -> Inverse_off
219

    
220
      | FG_Black | FG_Red | FG_Green | FG_Yellow | FG_Blue
221
      | FG_Magenta | FG_Cyan | FG_Gray | FG_Default -> FG_Default
222

    
223
      | BG_Black | BG_Red | BG_Green | BG_Yellow | BG_Blue 
224
      | BG_Magenta | BG_Cyan | BG_Gray | BG_Default -> BG_Default
225

    
226
      | FG_Black_B | FG_Red_B | FG_Green_B | FG_Yellow_B | FG_Blue_B 
227
      | FG_Magenta_B | FG_Cyan_B | FG_Gray_B | FG_Default_B -> FG_Default
228

    
229
      | BG_Black_B | BG_Red_B | BG_Green_B | BG_Yellow_B | BG_Blue_B
230
      | BG_Magenta_B | BG_Cyan_B | BG_Gray_B | BG_Default_B -> BG_Default
231

    
232
      | _ -> Normal
233
    in
234
    Printf.sprintf "[%sm" (assoc_style st)
235
  (* with Not_found -> eprintf "didnr find %s@." t; raise Not_found  *)
236

    
237

    
238
let add_colors formatter =
239
  (* pp_set_tags formatter true; *)
240
  let old_fs = pp_get_formatter_tag_functions formatter () in
241
  pp_set_formatter_tag_functions formatter
242
    { old_fs with
243
      mark_open_tag = start_tag;
244
      mark_close_tag = stop_tag }
245

    
246
let _ =
247
  add_colors std_formatter;
248
  add_colors err_formatter;
249
  add_colors !KindLib.log_ppf;
250
  pp_set_margin std_formatter vt_width;
251
  pp_set_margin err_formatter vt_width
252

    
253

    
254

    
255
(* ********************************************************************** *)
256
(* Event tags used when outputting info.                                  *)
257
(* ********************************************************************** *)
258

    
259
type event_tag =
260
  | Timeout
261
  | Success
262
  | Failure
263
  | Warning
264
  | Note
265
  | Error
266
  | Interruption
267
  | Done
268

    
269
(* Formats a string to construct a tag. *)
270
let tagify fmt s = Format.fprintf fmt ("@{<b><" ^^ s ^^ ">@}")
271

    
272
let print_event_tag fmt = function
273
  | Timeout -> tagify fmt "@{<magenta>Timeout@}"
274
  | Success -> tagify fmt "@{<green_b>Success@}"
275
  | Failure -> tagify fmt "@{<red_b>Failure@}"
276
  | Warning -> tagify fmt "@{<yellow>Warning@}"
277
  | Note -> tagify fmt "@{<cyan>Note@}"
278
  | Error ->  tagify fmt "@{<magenta_b>Error@}"
279
  | Interruption -> tagify fmt "@{<magenta>Interruption@}"
280
  | Done -> tagify fmt "@{<green>Done@}"
281

    
282

    
283
let timeout_tag fmt = print_event_tag fmt Timeout
284
let success_tag fmt = print_event_tag fmt Success
285
let failure_tag fmt = print_event_tag fmt Failure
286
let error_tag fmt = print_event_tag fmt Error
287
let warning_tag fmt = print_event_tag fmt Warning
288
let note_tag fmt = print_event_tag fmt Note
289
let interruption_tag fmt = print_event_tag fmt Interruption
290
let done_tag fmt = print_event_tag fmt Done
291

    
292

    
293
let tag_of_level fmt = let open KindLib in function
294
| L_fatal | L_error -> print_event_tag fmt Error
295
| L_warn -> print_event_tag fmt Warning
296
| L_note -> print_event_tag fmt Note
297
| _ -> ()
298

    
299

    
(11-11/12)