Skip to content

Commit

Permalink
Stop logging repeated errors when retrying
Browse files Browse the repository at this point in the history
Retrying on an error that is not permanent but may last for a long time,
for hundreds or even thousands of attempts (for example, connection
refused, one of the roots not mounted), it is unnecessary to spam the
log with the same error every time.
  • Loading branch information
tleedjarv committed Apr 1, 2024
1 parent b1e1f89 commit 9779470
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/fspath.ml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ let canonizeFspath p0 =
(try System.chdir parent with
Sys_error why2 -> raise (Util.Fatal (Printf.sprintf
"Cannot find canonical name of %s: unable to cd either to it \
(%s)\nor to its parent %s\n(%s)" p why parent why2)));
(%s)\nor to its parent %s (%s)" p why parent why2)));
System.getcwd () end in
System.chdir original;
let bn = Filename.basename p in
Expand Down
38 changes: 31 additions & 7 deletions src/uitext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ let alwaysDisplay message =
print_string message;
flush stdout

let alwaysDisplayErr message =
prerr_string message;
flush stderr

let alwaysDisplayAndLog message =
(* alwaysDisplay message;*)
Trace.log (message ^ "\n")
Expand Down Expand Up @@ -1580,19 +1584,39 @@ let getProfile default =
askProfile ();
!selection

let handleException e =
let suppressRepeatingMsg =
let prevMsg = ref "" in
let silencing = ref false in
fun retrying msg ->
match retrying with
| false ->
prevMsg := ""; silencing := false;
Some msg
| true when !prevMsg <> msg ->
prevMsg := msg; silencing := false;
Some msg
| true when not !silencing ->
silencing := true;
Some "\nRetrying continuously. The error above is repeating and \
is not logged repeatedly.\n"
| true ->
None

let handleException ?(retrying = false) e =
(* Keep the current status line (if any) and don't repeat it any more *)
alwaysDisplay "\n";
Util.set_infos "";
restoreTerminal();
let lbl =
if e = Sys.Break then ""
else "Error: " in
else if not retrying then "Fatal error: " else "Error: " in
let msg = lbl ^ Uicommon.exn2string e in
let () =
try Trace.log (msg ^ "\n")
with Util.Fatal _ -> () in (* Can't allow fatal errors in fatal error handler *)
if not !Trace.sendLogMsgsToStderr then alwaysDisplay ("\n" ^ msg ^ "\n")
alwaysDisplayErr (msg ^ "\n");
match suppressRepeatingMsg retrying msg with
| None -> ()
| Some msg ->
try Trace.logonly (msg ^ "\n")
with Util.Fatal _ -> () (* Can't allow fatal errors in fatal error handler *)

let rec start interface =
if interface <> Uicommon.Text then
Expand Down Expand Up @@ -1685,7 +1709,7 @@ and start2 () =
| e -> begin
(* If any other bad thing happened and the -repeat preference is
set, then restart *)
handleException e;
handleException ~retrying:true e;

Util.msg "\nRestarting in 10 seconds...\n\n";
begin try interruptibleSleep 10 with Sys.Break -> terminate () end;
Expand Down

0 comments on commit 9779470

Please sign in to comment.