package oraft

  1. Overview
  2. Docs

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
open Stdio.Out_channel

type level = TRACE | DEBUG | INFO | WARN | ERROR

type t = {
  node_id : int;
  mode : Base.mode option;
  output_path : string option;
  level : level;
}

let int_of_level = function
  | TRACE -> 0
  | DEBUG -> 1
  | INFO -> 2
  | WARN -> 3
  | ERROR -> 4


let string_of_level = function
  | TRACE -> "TRACE"
  | DEBUG -> "DEBUG"
  | INFO -> "INFO"
  | WARN -> "WARN"
  | ERROR -> "ERROR"


let level_of_string s =
  match s with
  | "TRACE" -> TRACE
  | "DEBUG" -> DEBUG
  | "INFO" -> INFO
  | "WARN" -> WARN
  | "ERROR" -> ERROR
  | _ -> failwith (Printf.sprintf "Unexpected value: %s" s)


let create ~node_id ?mode ?output_path ~level () =
  { node_id; mode; output_path; level = level_of_string level }


let write t ~level ~loc ~msg =
  let mode =
    match t.mode with Some x -> Base.show_mode x | None -> "--------"
  in
  if int_of_level level >= int_of_level t.level
  then (
    let now =
      Core.Time_ns.to_string_iso8601_basic (Core.Time_ns.now ())
        ~zone:Core.Time_float.Zone.utc
    in
    let msg = Str.(global_replace (regexp "\n") "" msg) in
    let s =
      Printf.sprintf "%s %s [%d:%s] - (%s) %s\n" now (string_of_level level)
        t.node_id mode loc msg
    in
    match t.output_path with
    | Some output_path ->
        with_file output_path
          ~f:(fun file -> ignore (output_string file s))
          ~append:true
    | None -> print_endline s
  )


let debug t ~loc msg = write t ~level:DEBUG ~loc ~msg
let info t ~loc msg = write t ~level:INFO ~loc ~msg
let warn t ~loc msg = write t ~level:WARN ~loc ~msg
let error t ~loc msg = write t ~level:ERROR ~loc ~msg