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
let _debug_active = ref false
let debug_active () = !_debug_active
open Lwt.Infix
let reporter file_descr ppf =
let ppf, flush =
let buf = Buffer.create 0x100 in
( Fmt.with_buffer ~like:ppf buf,
fun () ->
let str = Buffer.contents buf in
Buffer.reset buf;
str )
in
let report src level ~over k msgf =
let k _ =
let write () =
let buf = Bytes.unsafe_of_string (flush ()) in
let rec go off len =
Lwt_unix.write file_descr buf off len >>= fun len' ->
if len' = len then Lwt.return_unit else go (off + len') (len - len')
in
go 0 (Bytes.length buf)
in
let clean () =
over ();
Lwt.return_unit
in
Lwt.async (fun () ->
Lwt.catch
(fun () -> Lwt.finalize write clean)
(fun exn ->
Logs.warn (fun m ->
m "Flushing error: %s." (Printexc.to_string exn));
Lwt.return_unit));
k ()
in
let with_metadata _tags k ppf fmt =
Format.kfprintf k ppf
("%a[%a]: " ^^ fmt ^^ "\n%!")
Logs_fmt.pp_header (level, header)
Fmt.(styled `Magenta string)
(Logs.Src.name src)
in
msgf @@ fun ? ?tags fmt -> with_metadata header tags k ppf fmt
in
{ Logs.report }
let default_reporter = reporter Lwt_unix.stderr Fmt.stderr
let set_logger =
lazy
(if
Logs.reporter () == Logs.nop_reporter
then Logs.set_reporter default_reporter)
let activate_debug () =
if not !_debug_active then (
_debug_active := true;
Lazy.force set_logger;
Logs.set_level ~all:true (Some Logs.Debug);
Logs.debug (fun f -> f "Cohttp debugging output is active"))
let () =
try
match Sys.getenv "COHTTP_DEBUG" with
| "false" | "0" -> ()
| _ -> activate_debug ()
with Not_found -> ()