assert.cc (1211B)
1 #include "assert.hh" 2 3 #include "backtrace.hh" 4 #include "format.hh" 5 #include "file.hh" 6 #include "exception.hh" 7 #include "debug.hh" 8 9 #include <sys/types.h> 10 #include <unistd.h> 11 12 namespace Kakoune 13 { 14 15 struct assert_failed : logic_error 16 { 17 assert_failed(String message) 18 : m_message(std::move(message)) {} 19 20 StringView what() const override { return m_message; } 21 private: 22 String m_message; 23 }; 24 25 bool notify_fatal_error(StringView msg) 26 { 27 if (not isatty(STDOUT_FILENO) or not isatty(STDIN_FILENO)) 28 return false; 29 30 write(STDOUT_FILENO, format("\033[;31;5;1mKakoune fatal error, q: exit, i: ignore or debug pid {}\033[0m", getpid())); 31 while (true) 32 { 33 if (unsigned char c = 0; read(STDIN_FILENO, &c, 1) < 0 or c == 'q') 34 return false; 35 else if (c == 'i') 36 return true; 37 } 38 } 39 40 void on_assert_failed(const char* message) 41 { 42 String debug_info = format("pid: {}\ncallstack:\n{}", getpid(), Backtrace{}.desc()); 43 write_to_debug_buffer(format("assert failed: '{}'\n{}", message, debug_info)); 44 45 const auto msg = format("{}\n[Debug Infos]\n{}", message, debug_info); 46 if (not notify_fatal_error(msg)) 47 throw assert_failed(msg); 48 } 49 50 }