forked from dbuenzli/logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_lwt.ml
61 lines (54 loc) · 2.6 KB
/
test_lwt.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
(*---------------------------------------------------------------------------
Copyright (c) 2015 The logs programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)
let ( >>= ) = Lwt.bind
let lwt_reporter () =
let buf_fmt ~like =
let b = Buffer.create 512 in
Fmt.with_buffer ~like b,
fun () -> let m = Buffer.contents b in Buffer.reset b; m
in
let app, app_flush = buf_fmt ~like:Fmt.stdout in
let dst, dst_flush = buf_fmt ~like:Fmt.stderr in
let reporter = Logs_fmt.reporter ~app ~dst () in
let report src level ~over k msgf =
let k () =
let write () = match level with
| Logs.App -> Lwt_io.write Lwt_io.stdout (app_flush ())
| _ -> Lwt_io.write Lwt_io.stderr (dst_flush ())
in
let unblock () = over (); Lwt.return_unit in
Lwt.finalize write unblock |> Lwt.ignore_result;
k ()
in
reporter.Logs.report src level ~over:(fun () -> ()) k msgf;
in
{ Logs.report = report }
let main () =
Fmt_tty.setup_std_outputs ();
Logs.set_level @@ Some Logs.Debug;
Logs.set_reporter @@ lwt_reporter ();
Lwt_main.run @@ begin
Logs_lwt.info (fun m -> m ~header:"START" ?tags:None "Starting main")
>>= fun () -> Logs_lwt.warn (fun m -> m "Hey be warned by %d." 7)
>>= fun () -> Logs_lwt.err (fun m -> m "Hey be errored.")
>>= fun () -> Logs_lwt.debug (fun m -> m "Be debugged a bit ?")
>>= fun () -> Logs_lwt.app (fun m -> m "Application console or stdout.")
>>= fun () -> Logs_lwt.info (fun m -> m "Ending main")
>>= fun () -> exit (if (Logs.err_count () > 0) then 1 else 0)
end
let () = main ()
(*---------------------------------------------------------------------------
Copyright (c) 2015 The logs programmers
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)