Use mirage-logs library for log reporter

Also, configure Xen debug messages to go to the log ring buffer but not
the console (they will be shown only if an error occurs).
This commit is contained in:
Thomas Leonard 2016-01-11 12:21:47 +00:00
parent 4ddb80cd9d
commit 987834f6a6
6 changed files with 16 additions and 56 deletions

View file

@ -19,4 +19,4 @@ addons:
- camlp4-extra
- time
env:
- FORK_USER=talex5 FORK_BRANCH=unikernel OCAML_VERSION=4.02 MIRAGE_BACKEND=xen PINS="mirage-xen:https://github.com/talex5/mirage-platform.git#mm mirage-qubes:https://github.com/talex5/mirage-qubes.git mirage-nat:https://github.com/talex5/mirage-nat.git#simplify-checksum tcpip:https://github.com/mirage/mirage-tcpip.git"
- FORK_USER=talex5 FORK_BRANCH=unikernel OCAML_VERSION=4.02 MIRAGE_BACKEND=xen PINS="mirage-xen:https://github.com/talex5/mirage-platform.git#mm mirage-qubes:https://github.com/talex5/mirage-qubes.git mirage-nat:https://github.com/talex5/mirage-nat.git#simplify-checksum tcpip:https://github.com/mirage/mirage-tcpip.git mirage-logs:https://github.com/talex5/mirage-logs.git"

View file

@ -23,6 +23,7 @@ To build (tested by creating a fresh Fedora 23 AppVM in Qubes):
opam pin add -y tcpip https://github.com/mirage/mirage-tcpip.git
opam pin add -y mirage-qubes https://github.com/talex5/mirage-qubes.git
opam pin add -y mirage-nat 'https://github.com/talex5/mirage-nat.git#simplify-checksum'
opam pin add -y mirage-logs https://github.com/talex5/mirage-logs.git
opam install mirage
3. Build mirage-firewall:

View file

@ -7,8 +7,8 @@ open Mirage
let main =
foreign
~libraries:["mirage-net-xen"; "tcpip.stack-direct"; "tcpip.xen"; "mirage-qubes"; "mirage-nat"]
~packages:["vchan"; "cstruct"; "tcpip"; "mirage-net-xen"; "mirage-qubes"; "mirage-nat"]
~libraries:["mirage-net-xen"; "tcpip.stack-direct"; "tcpip.xen"; "mirage-qubes"; "mirage-nat"; "mirage-logs"]
~packages:["vchan"; "cstruct"; "tcpip"; "mirage-net-xen"; "mirage-qubes"; "mirage-nat"; "mirage-logs"]
"Unikernel.Main" (clock @-> job)
let () =

View file

@ -1,36 +0,0 @@
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
See the README file for details. *)
let buf = Buffer.create 200
let log_fmt = Format.formatter_of_buffer buf
let string_of_level =
let open Logs in function
| App -> "APP"
| Error -> "ERR"
| Warning -> "WRN"
| Info -> "INF"
| Debug -> "DBG"
let fmt_timestamp tm =
let open Clock in
Printf.sprintf "%04d-%02d-%02d %02d:%02d.%02d"
(tm.tm_year + 1900) (tm.tm_mon + 1) tm.tm_mday tm.tm_hour tm.tm_min tm.tm_sec
module Make (C : V1.CLOCK) = struct
let init_logging () =
let report src level ~over k msgf =
let now = C.time () |> Clock.gmtime |> fmt_timestamp in
let lvl = string_of_level level in
let k _ =
let msg = Buffer.contents buf in
Buffer.clear buf;
output_string stderr (msg ^ "\n");
flush stderr;
MProf.Trace.label msg;
over ();
k () in
msgf @@ fun ?header:_ ?tags:_ fmt ->
Format.kfprintf k log_fmt ("%s: %s [%s] " ^^ fmt) now lvl (Logs.Src.name src) in
Logs.set_reporter { Logs.report }
end

View file

@ -1,14 +0,0 @@
(* Copyright (C) 2015, Thomas Leonard <thomas.leonard@unikernel.com>
See the README file for details. *)
(** Mirage support for Logs library. *)
module Make (Clock : V1.CLOCK) : sig
val init_logging : unit -> unit
(** [init_logging ()] configures the Logs library to log to stderr,
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). *)
end

View file

@ -11,10 +11,13 @@ module Log = (val Logs.src_log src : Logs.LOG)
let () =
let open Logs in
(* Set default log level *)
set_level (Some Logs.Info)
set_level (Some Logs.Info);
(* Debug-level logging for XenStore while tracking down occasional EACCES error. *)
Src.list () |> List.find (fun src -> Src.name src = "xenstore.client") |> fun xs ->
Src.set_level xs (Some Debug)
module Main (Clock : V1.CLOCK) = struct
module Log_reporter = Mirage_logs.Make(Clock)
module Logs_reporter = Mirage_logs.Make(Clock)
module Uplink = Uplink.Make(Clock)
(* Set up networking and listen for incoming packets. *)
@ -41,10 +44,16 @@ module Main (Clock : V1.CLOCK) = struct
Uplink.listen uplink router
]
(* Control which of the messages that reach the reporter are logged to the console.
The rest will be displayed only if an error occurs.
Note: use the regular [Logs] configuration settings to determine which messages
reach the reporter in the first place. *)
let console_threshold _ = Logs.Info
(* Main unikernel entry point (called from auto-generated main.ml). *)
let start () =
let start_time = Clock.time () in
Log_reporter.init_logging ();
Logs_reporter.(create ~ring_size:20 ~console_threshold () |> run) @@ fun () ->
(* Start qrexec agent, GUI agent and QubesDB agent in parallel *)
let qrexec = RExec.connect ~domid:0 () in
let gui = GUI.connect ~domid:0 () in