Source file logger.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
(* Logging fuzz states *)

type t = out_channel

(* Constructor *)

let setup_signal_handler (oc : out_channel) =
  let handler _ =
    print_endline ">>> Caught interrupt, flushing logs";
    close_out oc;
    exit 1
  in
  Sys.set_signal Sys.sigint (Sys.Signal_handle handler)

let init (logname : string) : t =
  let oc = open_out logname in
  setup_signal_handler oc;
  oc

(* Logging *)

let log (logmode : Modes.logmode) (oc : t) (msg : string) : unit =
  let timestamp =
    let tm = Unix.localtime (Unix.time ()) in
    Printf.sprintf "[%02d:%02d:%02d]" tm.tm_hour tm.tm_min tm.tm_sec
  in
  let msg = Format.asprintf "[%s] >>> %s" timestamp msg in
  if logmode = Verbose then print_endline msg;
  msg ^ "\n" |> output_string oc;
  flush oc

let mark (logmode : Modes.logmode) (oc : t) (msg : string) : unit =
  let timestamp =
    let tm = Unix.localtime (Unix.time ()) in
    Printf.sprintf "[%02d:%02d:%02d]" tm.tm_hour tm.tm_min tm.tm_sec
  in
  let msg = Format.asprintf "[%s] ### %s" timestamp msg in
  if logmode = Verbose then print_endline msg;
  msg ^ "\n" |> output_string oc;
  flush oc

let warn (logmode : Modes.logmode) (oc : t) (msg : string) : unit =
  let timestamp =
    let tm = Unix.localtime (Unix.time ()) in
    Printf.sprintf "[%02d:%02d:%02d]" tm.tm_hour tm.tm_min tm.tm_sec
  in
  let msg = Format.asprintf "[%s] !!! %s" timestamp msg in
  if logmode = Verbose then print_endline msg;
  msg ^ "\n" |> output_string oc;
  flush oc

(* Closing *)

let close (oc : t) = close_out oc