refactor: reuse logic in main.swift

This commit is contained in:
lwouis committed 2024-12-02 19:34:38 +01:00
1 parent 1053740723
commit a4899658df
1 file changed
+20 -18
+20 -18
View File
@@ -1,6 +1,23 @@
import AppKit
import Darwin
// - if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called
// - if the app crashes in swift code (e.g. unexpected nil object), SIGTRAP is sent
// we intercept these signals, and do an emergency exit
[SIGTERM, SIGTRAP].forEach {
signal($0) { s in
emergencyExit("Exiting after receiving signal", s)
}
}
// - if the app crashes in objective-c code, an NSException may be sent
// we intercept the exception, and do an emergency exit
NSSetUncaughtExceptionHandler { (exception) in
emergencyExit("Exiting after receiving uncaught NSException", exception)
}
App.shared.run()
func printStackTrace() {
let stackSymbols = Thread.callStackSymbols
for symbol in stackSymbols {
@@ -8,25 +25,10 @@ func printStackTrace() {
}
}
// - if the app is quit/force-quit from Activity Monitor, it will receive SIGTERM and applicationWillTerminate won't be called
// - if the app crashes in swift code (e.g. unexpected nil object), SIGTRAP is sent
// we intercept these signals, and reset the native command-tab shortcut
[SIGTERM, SIGTRAP].forEach {
signal($0) { s in
setNativeCommandTabEnabled(true)
debugPrint("Exiting after receiving signal", s)
printStackTrace()
exit(0)
}
}
// - if the app crashes in objective-c code, an NSException may be sent
// we intercept the exception, and reset the native command-tab shortcut
NSSetUncaughtExceptionHandler { (exception) in
// during an emergency exit, we re-enable the native command+tab, and log
fileprivate func emergencyExit(_ logs: Any?...) {
setNativeCommandTabEnabled(true)
debugPrint("Exiting after receiving uncaught NSException", exception)
print(logs)
printStackTrace()
exit(0)
}
App.shared.run()