From 3c86484c4ed284fa8f1feff380959f5b52b264c6 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 29 May 2015 13:35:09 +0100 Subject: [PATCH] Extract xmessage/MessageBox support in a notify_fatal_error function --- src/assert.cc | 37 +++++++++++++++++++++---------------- src/assert.hh | 5 +++++ src/main.cc | 4 +++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/assert.cc b/src/assert.cc index 3fa1b7e8..9d063518 100644 --- a/src/assert.cc +++ b/src/assert.cc @@ -25,6 +25,26 @@ private: String m_message; }; +bool notify_fatal_error(const String& msg) +{ +#if defined(__CYGWIN__) + int res = MessageBox(NULL, msg.c_str(), "Kakoune: assert failed", + MB_OKCANCEL | MB_ICONERROR); + switch (res) + { + case IDCANCEL: + return false; + case IDOK: + return true; + } +#elif defined(__linux__) + auto cmd = "xmessage -buttons 'quit:0,ignore:1' '" + msg + "'"; + if (system(cmd.c_str()) == 1) + return true; +#endif + return false; +} + void on_assert_failed(const char* message) { char* callstack = Backtrace{}.desc(); @@ -33,23 +53,8 @@ void on_assert_failed(const char* message) write_debug(format("assert failed: '{}'\n{}", message, debug_info)); const auto msg = format("{}\n[Debug Infos]\n{}", message, debug_info); -#if defined(__CYGWIN__) - int res = MessageBox(NULL, msg.c_str(), "Kakoune: assert failed", - MB_OKCANCEL | MB_ICONERROR); - switch (res) - { - case IDCANCEL: - throw assert_failed(message); - case IDOK: - return; - } -#elif defined(__linux__) - auto cmd = "xmessage -buttons 'quit:0,ignore:1' '" + msg + "'"; - if (system(cmd.c_str()) != 1) + if (not notify_fatal_error(msg)) throw assert_failed(msg); -#else - throw assert_failed(msg); -#endif } } diff --git a/src/assert.hh b/src/assert.hh index a65b09fd..f178e1ad 100644 --- a/src/assert.hh +++ b/src/assert.hh @@ -4,6 +4,11 @@ namespace Kakoune { +class String; + +// return true if user asked to ignore the error +bool notify_fatal_error(const String& message); + void on_assert_failed(const char* message); } diff --git a/src/main.cc b/src/main.cc index 2cda1c5a..822d53fe 100644 --- a/src/main.cc +++ b/src/main.cc @@ -284,8 +284,10 @@ void signal_handler(int signal) if (signal != SIGTERM) { char* callstack = Backtrace{}.desc(); - write_stderr(format("Received {}, exiting.\nCallstack:\n{}", text, callstack)); + auto msg = format("Received {}, exiting.\nCallstack:\n{}", text, callstack); free(callstack); + write_stderr(msg); + notify_fatal_error(msg); } if (Server::has_instance())