forked from ahrefs/devkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.ml
93 lines (75 loc) · 1.93 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
open Printf
type level = [`Debug | `Info | `Warn | `Error]
type facil = { name : string; mutable show : int; }
let int_level = function
| `Debug -> 0
| `Info -> 1
| `Warn -> 2
| `Error -> 3
let set_filter facil level = facil.show <- int_level level
let get_level facil = match facil.show with
| 0 -> `Debug
| 1 -> `Info
| 2 -> `Warn
| _ -> `Error (* ! *)
let allowed facil level = int_level level >= facil.show
let string_level = function
| `Debug -> "debug"
| `Info -> "info"
| `Warn -> "warn"
| `Error -> "error"
let level = function
| "info" -> `Info
| "debug" -> `Debug
| "warn" -> `Warn
| "error" -> `Error
| s -> Exn.fail "unrecognized level %s" s
module type Target =
sig
val format : level -> facil -> string -> string
val output : level -> facil -> string -> unit
end
module type Put = sig
val put : level -> facil -> string -> unit
end
module PutSimple(T : Target) : Put =
struct
let put level facil str =
if allowed facil level then
T.output level facil (T.format level facil str)
end
module PutLimited(T : Target) : Put =
struct
let last = ref (`Debug,"")
let n = ref 0
(** FIXME not thread safe *)
let put level facil str =
match allowed facil level with
| false -> ()
| true ->
let this = (level,str) in
if !last = this then
incr n
else
begin
if !n <> 0 then
begin
T.output level facil (sprintf
"last message repeated %u times, suppressed\n" !n);
n := 0
end;
last := this;
T.output level facil (T.format level facil str);
end
end
module Make(T : Put) = struct
let debug_s = T.put `Debug
let info_s = T.put `Info
let warn_s = T.put `Warn
let error_s = T.put `Error
let put_s = T.put
let debug f fmt = ksprintf (debug_s f) fmt
let info f fmt = ksprintf (info_s f) fmt
let warn f fmt = ksprintf (warn_s f) fmt
let error f fmt = ksprintf (error_s f) fmt
end