Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mirage-clock uses variants now #27

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions src/mirage_logs.ml
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
(* Copyright (C) 2016, Thomas Leonard <thomas.leonard@unikernel.com>
See the README file for details. *)

module Make (C : Mirage_clock.PCLOCK) = struct
let pp_tags f tags =
let pp tag () =
let (Logs.Tag.V (def, value)) = tag in
Format.fprintf f " %s=%a" (Logs.Tag.name def) (Logs.Tag.printer def) value;
()
in
Logs.Tag.fold pp tags ()
let pp_tags f tags =
let pp tag () =
let (Logs.Tag.V (def, value)) = tag in
Format.fprintf f " %s=%a" (Logs.Tag.name def) (Logs.Tag.printer def) value;
()
in
Logs.Tag.fold pp tags ()

let create ?(ch = Format.err_formatter) () =
let report src level ~over k msgf =
let tz_offset_s = C.current_tz_offset_s () in
let posix_time = Ptime.v @@ C.now_d_ps () in
let src = Logs.Src.name src in
msgf @@ fun ?header ?tags fmt ->
let k _ =
over ();
k ()
in
Format.kfprintf k ch
("%a:%a %a [%s] @[" ^^ fmt ^^ "@]@.")
(Ptime.pp_rfc3339 ?tz_offset_s ())
posix_time
Fmt.(option ~none:(any "") pp_tags)
tags Logs_fmt.pp_header (level, header) src
let create ?(ch = Format.err_formatter) () =
let report src level ~over k msgf =
let tz_offset_s = Mirage_clock.Pclock.current_tz_offset_s () in
let posix_time = Ptime.v @@ Mirage_clock.Pclock.now_d_ps () in
let src = Logs.Src.name src in
msgf @@ fun ?header ?tags fmt ->
let k _ =
over ();
k ()
in
{ Logs.report }
end
Format.kfprintf k ch
("%a:%a %a [%s] @[" ^^ fmt ^^ "@]@.")
(Ptime.pp_rfc3339 ?tz_offset_s ())
posix_time
Fmt.(option ~none:(any "") pp_tags)
tags Logs_fmt.pp_header (level, header) src
in
{ Logs.report }
15 changes: 7 additions & 8 deletions src/mirage_logs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

This is the default log reporter used by MirageOS. *)

module Make (Clock : Mirage_clock.PCLOCK) : sig
val create : ?ch:Format.formatter -> unit -> Logs.reporter
(** [create ~ch ()] is a Logs reporter that logs to [ch] (defaults to
[Format.err_formatter]), with time-stamps provided by [Clock].
val create : ?ch:Format.formatter -> unit -> Logs.reporter
(** [create ~ch ()] is a Logs reporter that logs to [ch] (defaults to
[Format.err_formatter]), with time-stamps provided by [Clock].

If logs are written faster than the backend can consume them, the whole
unikernel will block until there is space (so log messages will not be
lost, but unikernels generating a lot of log output may run slowly). *)

If logs are written faster than the backend can consume them, the whole
unikernel will block until there is space (so log messages will not be
lost, but unikernels generating a lot of log output may run slowly). *)
end
28 changes: 9 additions & 19 deletions test/test.ml
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
(* Copyright (C) 2016, Thomas Leonard <thomas.leonard@unikernel.com>
See the README file for details. *)

module Clock = struct
type t = unit

let connect () : t Lwt.t = Lwt.return_unit
let now_d_ps _ = (0, 0L)
let current_tz_offset_s _ = Some 0
let period_d_ps _ = None
end

module Logs_reporter = Mirage_logs.Make (Clock)

let src = Logs.Src.create "test" ~doc:"mirage-logs test code"

module Log = (val Logs.src_log src : Logs.LOG)
Expand All @@ -34,27 +23,28 @@ let with_pipe fn =
Alcotest.fail (Printf.sprintf "Unexpected data in pipe: %S" (input_line r))
with End_of_file -> close_in r

let get_line_without_timestamp r =
let line = input_line r in
String.concat " " (List.tl (String.split_on_char ' ' line))

let test_console r =
Log.info (fun f -> f "Simple test");
Alcotest.(check string)
"Simple" "1970-01-01T00:00:00Z: [INFO] [test] Simple test" (input_line r);
"Simple" "[INFO] [test] Simple test" (get_line_without_timestamp r);
Log.warn (fun f ->
f ~tags:(tags ~src:"localhost" ~port:7000) "Packet rejected");
Alcotest.(check string)
"Tags"
"1970-01-01T00:00:00Z: src=localhost port=7000 [WARNING] [test] Packet \
rejected"
(input_line r);
"src=localhost port=7000 [WARNING] [test] Packet rejected"
(get_line_without_timestamp r);
Log.debug (fun f -> f "Not shown")

let test () =
with_pipe @@ fun ~r ~w ->
Lwt_main.run
(let ( >>= ) = Lwt.bind in
Clock.connect () >>= fun clock ->
Logs.(set_level (Some Info));
(Logs.(set_level (Some Info));
let reporter =
Logs_reporter.create ~ch:(Format.formatter_of_out_channel w) clock
Mirage_logs.create ~ch:(Format.formatter_of_out_channel w) ()
in
Logs.set_reporter reporter;
test_console r;
Expand Down