2011-09-09 21:24:18 +02:00
|
|
|
#include "assert.hh"
|
|
|
|
|
2015-05-27 14:56:27 +02:00
|
|
|
#include "backtrace.hh"
|
2015-06-06 12:54:48 +02:00
|
|
|
#include "buffer_utils.hh"
|
|
|
|
#include "exception.hh"
|
2012-10-17 17:01:08 +02:00
|
|
|
|
2014-01-08 20:22:58 +01:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-23 13:45:44 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2017-01-08 23:30:15 +01:00
|
|
|
#include <cstdlib>
|
2013-01-23 13:45:44 +01:00
|
|
|
|
2011-09-09 21:24:18 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-10-17 17:01:08 +02:00
|
|
|
struct assert_failed : logic_error
|
2011-09-09 21:24:18 +02:00
|
|
|
{
|
2014-10-20 20:18:38 +02:00
|
|
|
assert_failed(String message)
|
|
|
|
: m_message(std::move(message)) {}
|
2011-09-09 21:24:18 +02:00
|
|
|
|
2015-03-13 14:15:51 +01:00
|
|
|
StringView what() const override { return m_message; }
|
2012-10-17 17:01:08 +02:00
|
|
|
private:
|
|
|
|
String m_message;
|
|
|
|
};
|
2011-09-09 21:24:18 +02:00
|
|
|
|
2016-02-29 23:15:36 +01:00
|
|
|
bool notify_fatal_error(StringView msg)
|
2012-10-16 16:16:32 +02:00
|
|
|
{
|
2014-01-08 20:22:58 +01:00
|
|
|
#if defined(__CYGWIN__)
|
2017-03-17 00:08:10 +01:00
|
|
|
return MessageBox(NULL, msg.zstr(), "Kakoune: fatal error",
|
|
|
|
MB_OKCANCEL | MB_ICONERROR) == IDOK;
|
2015-05-27 19:45:48 +02:00
|
|
|
#elif defined(__linux__)
|
2022-06-19 11:17:22 +02:00
|
|
|
auto cmd = format("xmessage -buttons 'quit:0,ignore:1' '{}'", replace(msg, "'", "'\\''"));
|
2017-07-17 11:13:20 +02:00
|
|
|
int status = system(cmd.c_str());
|
|
|
|
return (WIFEXITED(status)) ? (WEXITSTATUS(status)) == 1 : false;
|
2018-10-05 11:10:14 +02:00
|
|
|
#else
|
|
|
|
return false;
|
2014-01-08 20:22:58 +01:00
|
|
|
#endif
|
2015-05-29 14:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_assert_failed(const char* message)
|
|
|
|
{
|
2015-05-29 14:35:54 +02:00
|
|
|
String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), Backtrace{}.desc());
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format("assert failed: '{}'\n{}", message, debug_info));
|
2015-05-29 14:35:09 +02:00
|
|
|
|
|
|
|
const auto msg = format("{}\n[Debug Infos]\n{}", message, debug_info);
|
|
|
|
if (not notify_fatal_error(msg))
|
|
|
|
throw assert_failed(msg);
|
2012-10-16 16:16:32 +02:00
|
|
|
}
|
|
|
|
|
2011-09-09 21:24:18 +02:00
|
|
|
}
|