From 987834f6a6373c581c40071803d88f17a3b149ad Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Mon, 11 Jan 2016 12:21:47 +0000 Subject: [PATCH] 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). --- .travis.yml | 2 +- README.md | 1 + config.ml | 4 ++-- mirage_logs.ml | 36 ------------------------------------ mirage_logs.mli | 14 -------------- unikernel.ml | 15 ++++++++++++--- 6 files changed, 16 insertions(+), 56 deletions(-) delete mode 100644 mirage_logs.ml delete mode 100644 mirage_logs.mli diff --git a/.travis.yml b/.travis.yml index 16a450e..e990f94 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/README.md b/README.md index 3b9ae24..74fc004 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/config.ml b/config.ml index 7f34e59..eecc4bf 100644 --- a/config.ml +++ b/config.ml @@ -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 () = diff --git a/mirage_logs.ml b/mirage_logs.ml deleted file mode 100644 index d337881..0000000 --- a/mirage_logs.ml +++ /dev/null @@ -1,36 +0,0 @@ -(* Copyright (C) 2015, Thomas Leonard - 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 diff --git a/mirage_logs.mli b/mirage_logs.mli deleted file mode 100644 index df23812..0000000 --- a/mirage_logs.mli +++ /dev/null @@ -1,14 +0,0 @@ -(* Copyright (C) 2015, Thomas Leonard - 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 diff --git a/unikernel.ml b/unikernel.ml index ef5799b..a1f67e8 100644 --- a/unikernel.ml +++ b/unikernel.ml @@ -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