-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.ml
105 lines (88 loc) · 3 KB
/
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
(* vim:sw=4 ts=4 sts=4 expandtab spell spelllang=en
*)
(* Copyright 2012, Cedric Cellier
*
* This file is part of RobiNet.
*
* RobiNet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RobiNet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with RobiNet. If not, see <http://www.gnu.org/licenses/>.
*)
(**
Logging facility
We keep lazily the last N messages of every log levels.
Additionally, messages of higher level than some threshold are copied onto stderr.
*)
open Batteries
(* Basically, Info is the lowest thing you want to see by default. *)
type level = Critical | Error | Warning | Info | Debug
type msg = Clock.Time.t * (string Lazy.t)
type queue = int * msg array
type logger =
{ name : string ;
use_wall_clock : bool ;
queues : queue array }
(* log level <-> queue index *)
let int_of_level = function
| Critical -> 0
| Error -> 1
| Warning -> 2
| Info -> 3
| Debug -> 4
let nb_levels = 5
(* output to console happen based on a constant current loglevel *)
let console_lvl = ref Error
let console_log name (t, lstr) =
Printf.printf "%a: %s: %s\n%!" Clock.printer t name (Lazy.force lstr)
(* queue management *)
let enqueue (qs, ar) m =
let next_wrap max v = if v >= max-1 then 0 else v+1 in
ar.(qs) <- m ;
next_wrap (Array.length ar) qs, ar
let queue_iter f (qs, ar) = (* TODO: ?(order=DESC) *)
let aux i =
let t, lstr = ar.(i) in
let str = Lazy.force lstr in
if String.length str > 0 then f t str in
for i = qs-1 downto 0 do aux i done ;
for i = (Array.length ar) - 1 downto qs do aux i done
(* log *)
let log logger level lstr =
let lvl = int_of_level level in
let now =
if logger.use_wall_clock then
Clock.Time.wall_clock ()
else
Clock.now () in
let msg = now, lstr in
logger.queues.(lvl) <- enqueue logger.queues.(lvl) msg ;
if lvl <= int_of_level !console_lvl then console_log logger.name msg
let log_exceptions logger ?(level=Warning) what f x =
try
f x
with e ->
log logger level (lazy (
Printf.sprintf "Ignoring exception %s while performing %s"
(Printexc.to_string e)
what))
(* creation *)
let make_queue size =
0, Array.create size (Clock.Time.o 0., lazy "")
let loggers = Hashtbl.create 131
let make ?(use_wall_clock=false) name size =
let logger = {
name ;
use_wall_clock ;
queues = Array.init nb_levels (fun _ -> make_queue size)
} in
Hashtbl.add loggers name logger ;
logger