From ba5ebc4f4d700e54f307ba36a79b136ed431687c Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Sun, 9 Oct 2022 19:48:55 +0200 Subject: [PATCH] feat: implement proper signal handling in examples --- examples/signal.nim | 16 ++++++++++++++++ examples/test_port_connect_cb.nim | 12 ++++++++---- examples/test_port_register.nim | 14 +++++++++----- 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 examples/signal.nim diff --git a/examples/signal.nim b/examples/signal.nim new file mode 100644 index 0000000..b08d10d --- /dev/null +++ b/examples/signal.nim @@ -0,0 +1,16 @@ +import system/ansi_c + +export SIG_DFL, SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM + +when not defined(windows): + export SIGPIPE + var + SIG_IGN* {.importc: "SIG_IGN", header: "".}: cint + SIGHUP* {.importc: "SIGHUP", header: "".}: cint + SIGQUIT* {.importc: "SIGQUIT", header: "".}: cint + +type CSighandlerT = proc (a: cint) {.noconv.} + +proc setSignalProc* (`proc`: CSighandlerT, signals: varargs[cint]) = + for sig in signals: + discard c_signal(sig, `proc`) diff --git a/examples/test_port_connect_cb.nim b/examples/test_port_connect_cb.nim index 7935a69..b242177 100644 --- a/examples/test_port_connect_cb.nim +++ b/examples/test_port_connect_cb.nim @@ -1,16 +1,17 @@ import std/[logging, os] import jacket +import signal var jclient: ClientTPtr var status: cint -var log = newConsoleLogger(lvlInfo) +var log = newConsoleLogger(lvlDebug) proc errorCb(msg: cstring) {.cdecl.} = # Suppress verbose JACK error messages when server is not available by # default. Pass ``lvlAll`` when creating the logger to enable them. debug "JACK error: " & $msg -proc cleanup() {.noconv.} = +proc cleanup(sig: cint) {.noconv.} = debug "Cleaning up..." if jclient != nil: @@ -46,7 +47,10 @@ if jclient == nil: error getJackStatusErrorString(status) quit 1 -setControlCHook(cleanup) +when defined(windows): + setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM) +else: + setSignalProc(cleanup, SIGABRT, SIGHUP, SIGINT, SIGQUIT, SIGTERM) discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0) discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) @@ -58,4 +62,4 @@ if jclient.activate() == 0: while true: sleep(50) -cleanup() +cleanup() \ No newline at end of file diff --git a/examples/test_port_register.nim b/examples/test_port_register.nim index 57c5e82..1a49885 100644 --- a/examples/test_port_register.nim +++ b/examples/test_port_register.nim @@ -1,22 +1,23 @@ import std/[logging, os] +import signal import jacket var jclient: ClientTPtr var status: cint -var log = newConsoleLogger(lvlInfo) +var log = newConsoleLogger(lvlDebug) proc errorCb(msg: cstring) {.cdecl.} = # Suppress verbose JACK error messages when server is not available by # default. Pass ``lvlAll`` when creating the logger to enable them. debug "JACK error: " & $msg -proc cleanup() {.noconv.} = +proc cleanup(sig: cint = 0) {.noconv.} = debug "Cleaning up..." if jclient != nil: discard jclient.clientClose jclient = nil - + quit QuitSuccess addHandler(log) @@ -28,7 +29,10 @@ if jclient == nil: error getJackStatusErrorString(status) quit 1 -setControlCHook(cleanup) +when defined(windows): + setSignalProc(cleanup, SIGABRT, SIGINT, SIGTERM) +else: + setSignalProc(cleanup, SIGHUP, SIGINT, SIGQUIT, SIGTERM) discard jclient.portRegister("in_1", JACK_DEFAULT_AUDIO_TYPE, PortIsInput.ord, 0) discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, 0) @@ -36,4 +40,4 @@ discard jclient.portRegister("out_1", JACK_DEFAULT_AUDIO_TYPE, PortIsOutput.ord, while true: sleep(50) -cleanup() +cleanup() # normally not reached