Client: initialize logging in the client (for the daemons)

This commit is contained in:
Benjamin Canou 2018-02-28 18:02:56 +01:00
parent 8a6deca8a0
commit e148b370c0

View File

@ -25,6 +25,34 @@ let builtin_commands =
return ()) ; return ()) ;
] ]
(* Duplicated from the node, here for now since the client still
embeds the baker. To be moved where appropriate when the baker is
definitively moved/factorized in its own binary. *)
let find_log_rules () =
match Option.try_with (fun () -> Sys.getenv "TEZOS_LOG"),
Option.try_with (fun () -> Sys.getenv "LWT_LOG")
with
| Some rules, None -> "environment variable TEZOS_LOG", Some rules
| None, Some rules -> "environment variable LWT_LOG", Some rules
| None, None -> "default rules", None
| Some rules, Some _ ->
Format.eprintf
"@[<v 2>@{<warning>@{<title>Warning@}@} \
Both environment variables TEZOS_LOG and LWT_LOG \
defined, using TEZOS_LOG.@]@\n@." ;
"environment varible TEZOS_LOG", Some rules
let init_logger () =
Lwt_log_core.add_rule "*" Lwt_log_core.Notice ;
let origin, rules = find_log_rules () in
Option.iter rules ~f:begin fun rules ->
try Lwt_log_core.load_rules rules ~fail_on_error:true
with _ ->
Pervasives.failwith
(Format.asprintf "Incorrect log rules defined in %s." origin)
end ;
Logging_unix.(init ~template:"$(message)" Stderr)
(* Main (lwt) entry *) (* Main (lwt) entry *)
let main select_commands = let main select_commands =
let executable_name = Filename.basename Sys.executable_name in let executable_name = Filename.basename Sys.executable_name in
@ -45,6 +73,7 @@ let main select_commands =
(if Unix.isatty Unix.stdout then Ansi else Plain) Short) ; (if Unix.isatty Unix.stdout then Ansi else Plain) Short) ;
ignore Cli_entries.(setup_formatter Format.err_formatter ignore Cli_entries.(setup_formatter Format.err_formatter
(if Unix.isatty Unix.stderr then Ansi else Plain) Short) ; (if Unix.isatty Unix.stderr then Ansi else Plain) Short) ;
init_logger () >>= fun () ->
Lwt.catch begin fun () -> begin Lwt.catch begin fun () -> begin
Client_config.parse_config_args Client_config.parse_config_args
(new unix_full (new unix_full
@ -122,6 +151,7 @@ let main select_commands =
end >>= fun retcode -> end >>= fun retcode ->
Format.pp_print_flush Format.err_formatter () ; Format.pp_print_flush Format.err_formatter () ;
Format.pp_print_flush Format.std_formatter () ; Format.pp_print_flush Format.std_formatter () ;
Logging_unix.close () >>= fun () ->
Lwt.return retcode Lwt.return retcode
(* Where all the user friendliness starts *) (* Where all the user friendliness starts *)