Source file log.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
open Base
let channel =
let {
Cli.color = _;
log_level = _;
log_file = _;
log_filename = _;
log_buffer_size = _;
log_worker_id = _;
commands = _;
temporary_file_mode = _;
keep_going = _;
files_to_run = _;
files_not_to_run = _;
tests_to_run = _;
tests_not_to_run = _;
patterns_to_run = _;
patterns_not_to_run = _;
tags_to_run = _;
tags_not_to_run = _;
list;
global_timeout = _;
retry = _;
test_timeout = _;
reset_regressions = _;
on_unknown_regression_files_mode = _;
loop_mode = _;
time = _;
record = _;
from_records = _;
resume_file = _;
resume = _;
job = _;
job_count = _;
suggest_jobs;
junit = _;
skip = _;
only = _;
test_args = _;
seed = _;
} =
Cli.options
in
if
(match list with None -> false | Some (`Ascii_art | `Tsv) -> true)
|| suggest_jobs
then stderr
else stdout
let current_worker_id = ref None
let set_current_worker_id id = current_worker_id := Some id
let quote_shell s =
let contains_single_quote = ref false in
let needs_quotes = ref false in
let categorize = function
| '\'' ->
needs_quotes := true ;
contains_single_quote := true
| 'a' .. 'z'
| 'A' .. 'Z'
| '0' .. '9'
| '-' | '_' | '.' | '+' | '/' | ':' | '@' | '%' ->
()
| _ -> needs_quotes := true
in
String.iter categorize s ;
if not !needs_quotes then s
else if not !contains_single_quote then "'" ^ s ^ "'"
else Filename.quote s
let quote_shell_command command arguments =
String.concat " " (List.map quote_shell (command :: arguments))
module Color = struct
type t = string
let ( ++ ) = ( ^ )
let reset = "\027[0m"
let bold = "\027[1m"
let apply color string =
if Cli.options.color then color ^ string ^ reset else string
module FG = struct
let black = "\027[30m"
let red = "\027[31m"
let green = "\027[32m"
let yellow = "\027[33m"
let blue = "\027[34m"
let magenta = "\027[35m"
let cyan = "\027[36m"
let gray = "\027[37m"
let bright_white = "\027[97m"
end
module BG = struct
let black = "\027[40m"
let red = "\027[41m"
let green = "\027[42m"
let yellow = "\027[43m"
let blue = "\027[44m"
let magenta = "\027[45m"
let cyan = "\027[46m"
let gray = "\027[47m"
let bright_white = "\027[107m"
end
end
module Log_buffer = struct
let capacity = Cli.options.log_buffer_size
let buffer = Array.make capacity (0., None, None, None, None, None, "")
let next = ref 0
let used = ref 0
let reset () =
next := 0 ;
used := 0
let push line =
if capacity > 0 then (
if !next >= capacity then next := 0 ;
buffer.(!next) <- line ;
incr next ;
used := min capacity (!used + 1))
let iter f =
let first = !next - !used in
let last = !next - 1 in
for i = first to last do
f buffer.((i + capacity) mod capacity)
done
end
let output_timestamp output timestamp =
let time = Unix.gmtime timestamp in
output
(Printf.sprintf
"%02d:%02d:%02d.%03d"
time.tm_hour
time.tm_min
time.tm_sec
(int_of_float ((timestamp -. float (truncate timestamp)) *. 1000.)))
let output_worker_id =
let padding = 1 + int_of_float (log10 (float_of_int Cli.options.job_count)) in
fun output worker_id ->
match worker_id with
| None -> output String.(make (1 + padding) ' ')
| Some id ->
let ids = string_of_int id in
output "#" ;
output String.(make (padding - length ids) '0') ;
output ids
let log_line_to ~use_colors
(timestamp, worker_id, color, prefix, prefix_color, progress, message)
channel =
let output = output_string channel in
output "[" ;
output_timestamp output timestamp ;
output "] " ;
if use_colors then Option.iter output color ;
if Cli.options.log_worker_id && Cli.options.job_count > 1 then (
output "[" ;
output_worker_id output worker_id ;
output "]" ;
output " ") ;
Option.iter
(fun prefix ->
output "[" ;
if use_colors then Option.iter output prefix_color ;
output prefix ;
(if use_colors then
match prefix_color with
| None -> ()
| Some _ ->
output Color.reset ;
Option.iter output color) ;
output "] ")
prefix ;
Option.iter output progress ;
output message ;
if use_colors && color <> None then output Color.reset ;
output "\n"
let log_string ~(level : Cli.log_level) ?color ?prefix ?prefix_color
?progress_msg message =
match String.split_on_char '\n' message with
| [] | [""] -> ()
| lines ->
let log_line message =
let line =
( Unix.gettimeofday (),
!current_worker_id,
color,
prefix,
prefix_color,
progress_msg,
message )
in
Option.iter (log_line_to ~use_colors:false line) Cli.options.log_file ;
match (Cli.options.log_level, level) with
| _, Quiet -> invalid_arg "Log.log_string: level cannot be Quiet"
| Error, Error
| Warn, (Error | Warn)
| Report, (Error | Warn | Report)
| Info, (Error | Warn | Report | Info)
| Debug, (Error | Warn | Report | Info | Debug) ->
(if level = Error then
Log_buffer.iter @@ fun line ->
log_line_to ~use_colors:Cli.options.color line channel) ;
Log_buffer.reset () ;
log_line_to ~use_colors:Cli.options.color line channel ;
flush channel
| (Quiet | Error | Warn | Report | Info), _ -> Log_buffer.push line
in
List.iter log_line lines
let log ~level ?color ?prefix fmt =
Format.kasprintf (log_string ~level ?color ?prefix) fmt
let debug ?color = log ~level:Debug ?color
let info ?color = log ~level:Info ?color
let report ?color = log ~level:Report ?color
let warn x = log ~level:Warn ~color:Color.FG.red ~prefix:"warn" x
let error x = log ~level:Error ~color:Color.FG.red ~prefix:"error" x
type test_result = Successful | Failed of string | Aborted
let test_result ~test_index ~test_count ~failure_count ~iteration test_result
test_name =
let prefix, prefix_color =
match test_result with
| Successful -> ("SUCCESS", Color.(FG.green ++ bold))
| Failed _ -> ("FAILURE", Color.(FG.red ++ bold))
| Aborted -> ("ABORTED", Color.(FG.red ++ bold))
in
let message =
if Cli.options.loop_mode <> Count 1 then
Printf.sprintf "(loop %d) %s" iteration test_name
else test_name
in
let progress_msg =
sf
"(%d/%d%s) "
test_index
test_count
(if failure_count > 0 then sf ", %d failed" failure_count else "")
in
log_string ~level:Report ~prefix ~prefix_color ~progress_msg message
let command ?color ?prefix command arguments =
let message = quote_shell_command command arguments in
log_string ~level:Debug ?color ?prefix message ;
if Cli.options.commands then print_endline message