diff --git a/src/environment/v1/logging.mli b/src/environment/v1/logging.mli index 118cfc335..832e6a7f6 100644 --- a/src/environment/v1/logging.mli +++ b/src/environment/v1/logging.mli @@ -4,7 +4,7 @@ val log_info: ('a, Format.formatter, unit, unit) format4 -> 'a val log_notice: ('a, Format.formatter, unit, unit) format4 -> 'a val warn: ('a, Format.formatter, unit, unit) format4 -> 'a val log_error: ('a, Format.formatter, unit, unit) format4 -> 'a -val fatal_error: ('a, Format.formatter, unit, 'b) format4 -> 'a +val fatal_error: ('a, Format.formatter, unit, unit) format4 -> 'a val lwt_debug: ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_info: ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a diff --git a/src/node/main/node_run_command.ml b/src/node/main/node_run_command.ml index 8a999ae3b..b757e4e6c 100644 --- a/src/node/main/node_run_command.ml +++ b/src/node/main/node_run_command.ml @@ -180,9 +180,12 @@ let init_rpc (rpc_config: Node_config_file.rpc) node = return (Some server) let init_signal () = - let handler id = try Lwt_exit.exit id with _ -> () in - ignore (Lwt_unix.on_signal Sys.sigint handler : Lwt_unix.signal_handler_id) ; - ignore (Lwt_unix.on_signal Sys.sigterm handler : Lwt_unix.signal_handler_id) + let handler name id = try + fatal_error "Received the %s signal, triggering shutdown." name ; + Lwt_exit.exit id + with _ -> () in + ignore (Lwt_unix.on_signal Sys.sigint (handler "INT") : Lwt_unix.signal_handler_id) ; + ignore (Lwt_unix.on_signal Sys.sigterm (handler "TERM") : Lwt_unix.signal_handler_id) let run ?verbosity ?sandbox (config : Node_config_file.t) = Node_data_version.ensure_data_dir config.data_dir >>=? fun () -> @@ -201,6 +204,7 @@ let run ?verbosity ?sandbox (config : Node_config_file.t) = lwt_log_notice "Shutting down the RPC server..." >>= fun () -> Lwt_utils.may RPC_server.shutdown rpc >>= fun () -> lwt_log_notice "BYE (%d)" x >>= fun () -> + Logging.close () >>= fun () -> return () let process sandbox verbosity args = diff --git a/src/node/updater/updater.ml b/src/node/updater/updater.ml index 576f3fee6..b6a464b9c 100644 --- a/src/node/updater/updater.ml +++ b/src/node/updater/updater.ml @@ -34,7 +34,9 @@ let fork_test_network = Context.fork_test_network let datadir = ref None let get_datadir () = match !datadir with - | None -> fatal_error "not initialized" + | None -> + fatal_error "Node not initialized" ; + Lwt_exit.exit 1 | Some m -> m let init dir = diff --git a/src/utils/logging.ml b/src/utils/logging.ml index 81240ca86..cd7dede9d 100644 --- a/src/utils/logging.ml +++ b/src/utils/logging.ml @@ -16,7 +16,7 @@ module type LOG = sig val log_notice: ('a, Format.formatter, unit, unit) format4 -> 'a val warn: ('a, Format.formatter, unit, unit) format4 -> 'a val log_error: ('a, Format.formatter, unit, unit) format4 -> 'a - val fatal_error: ('a, Format.formatter, unit, 'b) format4 -> 'a + val fatal_error: ('a, Format.formatter, unit, unit) format4 -> 'a val lwt_debug: ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_info: ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a @@ -56,10 +56,7 @@ module Make(S : sig val name: string end) : LOG = struct let log_notice fmt = ign_log_f ~section ~level:Lwt_log.Notice fmt let warn fmt = ign_log_f ~section ~level:Lwt_log.Warning fmt let log_error fmt = ign_log_f ~section ~level:Lwt_log.Error fmt - let fatal_error fmt = - Format.kasprintf - (fun s -> Lwt_log.ign_fatal ~section s; Lwt_exit.exit 1) - fmt + let fatal_error fmt = ign_log_f ~section ~level:Lwt_log.Fatal fmt let lwt_debug fmt = log_f ~section ~level:Lwt_log.Debug fmt let lwt_log_info fmt = log_f ~section ~level:Lwt_log.Info fmt @@ -203,6 +200,9 @@ let init ?(template = default_template) output = Lwt_log.default := logger ; Lwt.return_unit +let close () = + Lwt_log.close !Lwt_log.default + type level = Lwt_log_core.level = | Debug (** Debugging message. They can be automatically removed by the diff --git a/src/utils/logging.mli b/src/utils/logging.mli index 045b4a4b0..e4eccc305 100644 --- a/src/utils/logging.mli +++ b/src/utils/logging.mli @@ -14,7 +14,7 @@ module type LOG = sig val log_notice: ('a, Format.formatter, unit, unit) format4 -> 'a val warn: ('a, Format.formatter, unit, unit) format4 -> 'a val log_error: ('a, Format.formatter, unit, unit) format4 -> 'a - val fatal_error: ('a, Format.formatter, unit, 'b) format4 -> 'a + val fatal_error: ('a, Format.formatter, unit, unit) format4 -> 'a val lwt_debug: ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_info: ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a @@ -85,4 +85,6 @@ end val init: ?template:template -> Output.t -> unit Lwt.t +val close: unit -> unit Lwt.t + val sections : string list ref diff --git a/src/utils/lwt_exit.ml b/src/utils/lwt_exit.ml index 09481e875..de92817c0 100644 --- a/src/utils/lwt_exit.ml +++ b/src/utils/lwt_exit.ml @@ -17,12 +17,18 @@ let () = (function | Exit -> () | exn -> - Format.eprintf - "@[Uncaught (asynchronous) exception (%d):@ %a@]" - (Unix.getpid ()) - Error_monad.pp_exn exn ; let backtrace = Printexc.get_backtrace () in - if String.length backtrace <> 0 then - Format.eprintf "\n%s" backtrace ; - Format.eprintf "@." ; + Logging.Node.Main.fatal_error "@[%a%a@]" + (fun ppf exn -> + Format.fprintf ppf + "@[Uncaught (asynchronous) exception (%d):@ %a@]" + (Unix.getpid ()) + Error_monad.pp_exn exn) + exn + (fun ppf backtrace -> + if String.length backtrace <> 0 then + Format.fprintf ppf + "@,Backtrace:@, @[%a@]" + Format.pp_print_text backtrace) + backtrace ; Lwt.wakeup exit_wakener 1)