feat: implement proper signal handling in examples

This commit is contained in:
Christopher Arndt 2022-10-09 19:48:55 +02:00
parent e68223f1e6
commit ba5ebc4f4d
3 changed files with 33 additions and 9 deletions

16
examples/signal.nim Normal file
View File

@ -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: "<signal.h>".}: cint
SIGHUP* {.importc: "SIGHUP", header: "<signal.h>".}: cint
SIGQUIT* {.importc: "SIGQUIT", header: "<signal.h>".}: cint
type CSighandlerT = proc (a: cint) {.noconv.}
proc setSignalProc* (`proc`: CSighandlerT, signals: varargs[cint]) =
for sig in signals:
discard c_signal(sig, `proc`)

View File

@ -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()

View File

@ -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