1
1
open Core.Std
2
2
open Async.Std
3
3
4
- module TagSet = Set. Make (struct
5
- type t = string * string with sexp
6
- let compare = Pervasives. compare
7
- end )
8
-
9
- let filter_by_tags (enabled_tags : (string * string) list ) =
10
- let enabled_tags = TagSet. of_list enabled_tags in
11
- (fun msgs ->
12
- Queue. filter_map msgs (fun msg ->
13
- match Log.Message. tags msg with
14
- | [] -> Some msg (* untagged messages are printed indiscriminately *)
15
- | msg_tags ->
16
- if List. exists ~f: (TagSet. mem enabled_tags) msg_tags then
17
- Some msg
18
- else
19
- None ))
20
-
21
- let label_severity msg =
22
- let debug, info, error = [`Dim ], [`Blue ], [`Red ] in
23
- let style, prefix = match Log.Message. level msg with
24
- | None -> info, " "
25
- | Some `Debug -> debug, " [DEBUG]"
26
- | Some `Info -> info, " [INFO]"
27
- | Some `Error -> error, " [ERROR]" in
28
- String. concat ~sep: " "
29
- [ prefix
30
- ; Log.Message. message msg ]
31
-
32
- let make_filtered_output (tags : (string * string) list )
33
- : Log.Output.t =
34
- let filter = filter_by_tags tags in
35
- Log.Output. create
36
- (fun msgs ->
37
- let writer = Lazy. force (Writer. stderr) in
38
- return (Queue. iter (filter msgs) ~f: (fun msg ->
39
- Writer. write writer (label_severity msg);
40
- Writer. newline writer)))
41
-
42
- let current_outputs = ref []
43
-
44
- let stderr : Log.Output.t =
45
- make_filtered_output [(" openflow" , " " )]
46
-
47
- let log = lazy (Log. create ~level: `Info ~output: [stderr])
48
-
49
- let level () = Log. level (Lazy. force log)
50
- let set_level = Log. set_level (Lazy. force log)
51
-
52
- let set_output outputs = current_outputs := outputs;
53
- Log. set_output (Lazy. force log) outputs
54
-
55
- let add_output outputs =
56
- let outputs = outputs @ ! current_outputs in
57
- current_outputs := outputs;
58
- set_output outputs
59
-
60
- let raw ?time ?(tags =[] ) fmt = Log. raw (Lazy. force log) ?time ~tags fmt
61
-
62
- let info ?time ?(tags =[] ) fmt = Log. info (Lazy. force log) ?time ~tags fmt
63
-
64
- let error ?time ?(tags =[] ) fmt = Log. error (Lazy. force log) ?time ~tags fmt
65
-
66
- let debug ?time ?(tags =[] ) fmt = Log. debug (Lazy. force log) ?time ~tags fmt
67
-
68
- let flushed () =
69
- Log. flushed (Lazy. force log)
70
-
71
- let printf ?(level =`Debug ) ?time ?(tags =[] ) fmt =
72
- Log. printf (Lazy. force log) ~tags ~level fmt
73
-
74
- let of_lazy ?(level =`Debug ) ?time ?(tags =[] ) lazy_str =
75
- (* As of core/async.111.25.00, `Log.of_lazy` is no longer part of that
76
- * package's public API. In 111.28.00, the `Log.level` call was added,
77
- * allowing users of the package to implement `of_lazy` without having to
78
- * manage the log level manually.
79
- * *)
80
- if level = Log. level (Lazy. force log) then
81
- Log. printf (Lazy. force log) ~tags ~level " %s" (Lazy. force lazy_str)
82
-
83
- let sexp ?(level =`Debug ) ?time ?(tags =[] ) msg =
84
- Log. sexp (Lazy. force log) ~tags ~level msg
85
-
86
- let string ?(level =`Debug ) ?time ?(tags =[] ) str =
87
- Log. string (Lazy. force log) ~tags ~level str
88
-
89
- let message = Log. message (Lazy. force log)
4
+ module Log = Log. Make_global ()
5
+ include Log
0 commit comments