summaryrefslogtreecommitdiff
path: root/src/assert.cc
blob: 74b939a41cfd85a5129692eb6befc538cea42a2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "assert.hh"

#include "exception.hh"
#include "debug.hh"

#if defined(__CYGWIN__)
#include <windows.h>
#endif

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

namespace Kakoune
{

struct assert_failed : logic_error
{
    assert_failed(String message)
        : m_message(std::move(message)) {}

    const char* what() const override { return m_message.c_str(); }
private:
    String m_message;
};

void on_assert_failed(const char* message)
{
    String debug_info = "pid: " + to_string(getpid());
    write_debug("assert failed: '"_str + message + "' " + debug_info);

    const auto msg = message + "\n[Debug Infos]\n"_str + 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
    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
}

}