Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print POSIX signal number (instead of OCaml) using Fmt #160

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions lib/os.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ let ensure_closed_lwt fd =
if Lwt_unix.state fd = Lwt_unix.Closed then Lwt.return_unit
else Lwt_unix.close fd

let pp_signal f x =
let open Sys in
if x = sigkill then Fmt.string f "kill"
else if x = sigterm then Fmt.string f "term"
else Fmt.int f x

let pp_cmd f (cmd, argv) =
let argv = if cmd = "" then argv else cmd :: argv in
Fmt.hbox Fmt.(list ~sep:sp (quote string)) f argv
Expand Down Expand Up @@ -65,8 +59,8 @@ let default_exec ?timeout ?cwd ?stdin ?stdout ?stderr ~pp argv =
proc >|= fun proc ->
Result.fold ~ok:(function
| Unix.WEXITED n -> Ok n
| Unix.WSIGNALED x -> Fmt.error_msg "%t failed with signal %d" pp x
| Unix.WSTOPPED x -> Fmt.error_msg "%t stopped with signal %a" pp pp_signal x)
| Unix.WSIGNALED x -> Fmt.error_msg "%t failed with signal %a" pp Fmt.Dump.signal x
| Unix.WSTOPPED x -> Fmt.error_msg "%t stopped with signal %a" pp Fmt.Dump.signal x)
~error:(fun e ->
Fmt.error_msg "%t raised %s\n%s" pp (Printexc.to_string e) (Printexc.get_backtrace ())) proc

Expand All @@ -89,8 +83,8 @@ let open_process ?cwd ?stdin ?stdout ?stderr ?pp:_ argv =
let process_result ~pp proc =
proc >|= (function
| Unix.WEXITED n -> Ok n
| Unix.WSIGNALED x -> Fmt.error_msg "%t failed with signal %d" pp x
| Unix.WSTOPPED x -> Fmt.error_msg "%t stopped with signal %a" pp pp_signal x)
| Unix.WSIGNALED x -> Fmt.error_msg "%t failed with signal %a" pp Fmt.Dump.signal x
| Unix.WSTOPPED x -> Fmt.error_msg "%t stopped with signal %a" pp Fmt.Dump.signal x)
>>= function
| Ok 0 -> Lwt_result.return ()
| Ok n -> Lwt.return @@ Fmt.error_msg "%t failed with exit status %d" pp n
Expand Down