-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlogger.ml
More file actions
84 lines (74 loc) · 1.97 KB
/
logger.ml
File metadata and controls
84 lines (74 loc) · 1.97 KB
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
open Printf
type level = [`Debug | `Info | `Warn | `Error | `Critical | `Nothing]
type facil = { name : string; mutable show : int; }
let int_level = function
| `Debug -> 0
| `Info -> 1
| `Warn -> 2
| `Error -> 3
| `Critical -> 4
| `Nothing -> 100
let set_filter facil level = facil.show <- int_level level
let get_level facil = match facil.show with
| 0 -> `Debug
| 1 -> `Info
| 2 -> `Warn
| 3 -> `Error
| x when x = 100 -> `Nothing
| _ -> `Critical (* ! *)
let allowed facil level = level <> `Nothing && int_level level >= facil.show
let string_level = function
| `Debug -> "debug"
| `Info -> "info"
| `Warn -> "warn"
| `Error -> "error"
| `Critical -> "critical"
| `Nothing -> "nothing"
let level = function
| "info" -> `Info
| "debug" -> `Debug
| "warn" -> `Warn
| "error" -> `Error
| "critical" -> `Critical
| "nothing" -> `Nothing
| s -> Exn.fail "unrecognized level %s" s
module Pairs = struct
type pair = string*string
type t = pair list
end
type target = {
format : level -> facil -> Time.t -> Pairs.t -> string -> string;
output : level -> facil -> string -> unit;
}
(** A logger *)
type t = {
put : level -> facil -> Time.t -> Pairs.t -> string -> unit
} [@@unboxed]
let put_simple (t:target) : t = {
put = fun level facil ts pairs str ->
if allowed facil level then
t.output level facil (t.format level facil ts pairs str)
}
let put_limited (t:target) : t =
let last = ref (`Debug,"") in
let n = ref 0 in
(* FIXME not thread safe *)
let put level facil ts pairs 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 ts pairs str);
end
in { put }