From d795ddd944919551bd3d32a68baf66c54dde3956 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Thu, 1 Nov 2018 01:51:39 +0100 Subject: [PATCH] unify log output in vmmc_local/remote/bistro --- app/vmm_cli.ml | 11 +++++++++++ app/vmmc_bistro.ml | 26 ++++++-------------------- app/vmmc_local.ml | 33 ++++++++++----------------------- app/vmmc_remote.ml | 6 ++++-- 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/app/vmm_cli.ml b/app/vmm_cli.ml index f2926f3..b4e6aff 100644 --- a/app/vmm_cli.ml +++ b/app/vmm_cli.ml @@ -3,6 +3,17 @@ open Astring open Vmm_core +open Lwt.Infix + +let print_result version (header, reply) = + if not (Vmm_commands.version_eq header.Vmm_commands.version version) then + Logs.err (fun m -> m "version not equal") + else match reply with + | `Success s -> Logs.app (fun m -> m "%a" Vmm_commands.pp_wire (header, reply)) + | `Data d -> Logs.app (fun m -> m "%a" Vmm_commands.pp_wire (header, reply)) + | `Failure d -> Logs.warn (fun m -> m "%a" Vmm_commands.pp_wire (header, reply)) + | `Command _ -> Logs.err (fun m -> m "unexpected command %a" Vmm_commands.pp_wire (header, reply)) + let setup_log style_renderer level = Fmt_tty.setup_std_outputs ?style_renderer (); Logs.set_level level; diff --git a/app/vmmc_bistro.ml b/app/vmmc_bistro.ml index c436d95..063e818 100644 --- a/app/vmmc_bistro.ml +++ b/app/vmmc_bistro.ml @@ -4,24 +4,14 @@ open Lwt.Infix let version = `AV2 -let process fd = - Vmm_tls_lwt.read_tls fd >|= function - | Error _ -> Error (`Msg "read or parse error") - | Ok (header, reply) -> - if Vmm_commands.version_eq header.Vmm_commands.version version then begin - Logs.app (fun m -> m "%a" Vmm_commands.pp_wire (header, reply)) ; - Ok () - end else begin - Logs.err (fun m -> m "version not equal") ; - Error (`Msg "version not equal") - end - let read fd = (* now we busy read and process output *) let rec loop () = - process fd >>= function - | Error e -> Lwt.return (Error e) - | Ok () -> loop () + Vmm_tls_lwt.read_tls fd >>= function + | Error _ -> Lwt.return () + | Ok wire -> + Vmm_cli.print_result version wire ; + loop () in loop () @@ -71,11 +61,7 @@ let handle (host, port) cert key ca id (cmd : Vmm_commands.t) = read t let jump endp cert key ca name cmd = - match - Lwt_main.run (handle endp cert key ca name cmd) - with - | Ok () -> `Ok () - | Error (`Msg m) -> `Error (false, m) + `Ok (Lwt_main.run (handle endp cert key ca name cmd)) let info_ _ endp cert key ca name = jump endp cert key ca name (`Vm_cmd `Vm_info) diff --git a/app/vmmc_local.ml b/app/vmmc_local.ml index 09d7632..817f775 100644 --- a/app/vmmc_local.ml +++ b/app/vmmc_local.ml @@ -4,19 +4,6 @@ open Lwt.Infix let version = `AV2 -let process fd = - Vmm_lwt.read_wire fd >|= function - | Error _ -> - Error (`Msg "read or parse error") - | Ok (header, reply) -> - if Vmm_commands.version_eq header.Vmm_commands.version version then begin - Logs.app (fun m -> m "%a" Vmm_commands.pp_wire (header, reply)) ; - Ok () - end else begin - Logs.err (fun m -> m "version not equal") ; - Error (`Msg "version not equal") - end - let socket t = function | Some x -> x | None -> Vmm_core.socket_path t @@ -27,11 +14,16 @@ let connect socket_path = Lwt_unix.connect c (Lwt_unix.ADDR_UNIX socket_path) >|= fun () -> c +let process fd = + Vmm_lwt.read_wire fd >|= function + | Error _ -> Error () + | Ok wire -> Ok (Vmm_cli.print_result version wire) + let read fd = (* now we busy read and process output *) let rec loop () = process fd >>= function - | Error e -> Lwt.return (Error e) + | Error _ -> Lwt.return () | Ok () -> loop () in loop () @@ -41,20 +33,15 @@ let handle opt_socket id (cmd : Vmm_commands.t) = connect (socket sock opt_socket) >>= fun fd -> let header = Vmm_commands.{ version ; sequence = 0L ; id } in Vmm_lwt.write_wire fd (header, `Command cmd) >>= function - | Error `Exception -> Lwt.return (Error (`Msg "couldn't write")) + | Error `Exception -> Lwt.return () | Ok () -> (match next with | `Read -> read fd - | `End -> process fd) >>= fun res -> - Vmm_lwt.safe_close fd >|= fun () -> - res + | `End -> process fd >|= ignore) >>= fun () -> + Vmm_lwt.safe_close fd let jump opt_socket name cmd = - match - Lwt_main.run (handle opt_socket name cmd) - with - | Ok () -> `Ok () - | Error (`Msg m) -> `Error (false, m) + `Ok (Lwt_main.run (handle opt_socket name cmd)) let info_ _ opt_socket name = jump opt_socket name (`Vm_cmd `Vm_info) diff --git a/app/vmmc_remote.ml b/app/vmmc_remote.ml index 0aff0f5..909c520 100644 --- a/app/vmmc_remote.ml +++ b/app/vmmc_remote.ml @@ -2,11 +2,13 @@ open Lwt.Infix +let version = `AV2 + let rec read_tls_write_cons t = Vmm_tls_lwt.read_tls t >>= function - | Error _ -> Logs.err (fun m -> m "exception while reading") ; Lwt.return_unit + | Error _ -> Lwt.return_unit | Ok wire -> - Logs.app (fun m -> m "%a" Vmm_commands.pp_wire wire) ; + Vmm_cli.print_result version wire ; read_tls_write_cons t let client cas host port cert priv_key =