kakoune/src/assert.cc

60 lines
1.2 KiB
C++
Raw Normal View History

2011-09-09 21:24:18 +02:00
#include "assert.hh"
2012-10-17 17:01:08 +02:00
#include "exception.hh"
#include "debug.hh"
#include "backtrace.hh"
2012-10-17 17:01:08 +02:00
#if defined(__CYGWIN__)
#include <windows.h>
#endif
#include <sys/types.h>
#include <unistd.h>
2014-11-20 20:32:58 +01:00
#include <stdlib.h>
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
{
assert_failed(String message)
: m_message(std::move(message)) {}
2011-09-09 21:24:18 +02: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
2012-10-17 17:01:08 +02:00
void on_assert_failed(const char* message)
{
char* callstack = Backtrace{}.desc();
String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), callstack);
free(callstack);
write_debug(format("assert failed: '{}'\n{}", message, debug_info));
2015-03-31 00:56:33 +02:00
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;
}
#else
2014-04-01 19:54:46 +02:00
auto cmd = "xmessage -buttons 'quit:0,ignore:1' '" + msg + "'";
switch (system(cmd.c_str()))
{
case -1:
case 0:
throw assert_failed(message);
case 1:
return;
}
#endif
}
2011-09-09 21:24:18 +02:00
}