3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
14 #include "support/lassert.h"
16 #include "support/convert.h"
17 #include "support/debug.h"
18 #include "support/docstring.h"
19 #include "support/ExceptionMessage.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
23 #include <boost/assert.hpp>
26 #ifdef LYX_CALLSTACK_PRINTING
38 using namespace support;
41 void doAssertWithCallstack(bool value)
50 void doAssert(char const * expr, char const * file, long line)
52 LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
53 // comment this out if not needed
54 doAssertWithCallstack(false);
58 docstring formatHelper(docstring const & msg,
59 char const * expr, char const * file, long line)
61 docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
62 LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
64 return bformat(d, from_ascii(expr), from_ascii(file),
65 convert<docstring>(line)) + '\n' + msg;
69 void doWarnIf(char const * expr, char const * file, long line)
71 docstring const d = _("It should be safe to continue, but you\nmay wish to save your work and restart LyX.");
72 // comment this out if not needed
73 doAssertWithCallstack(false);
74 throw ExceptionMessage(WarningException, _("Warning!"),
75 formatHelper(d, expr, file, line));
79 void doBufErr(char const * expr, char const * file, long line)
81 docstring const d = _("There has been an error with this document.\nLyX will attempt to close it safely.");
82 // comment this out if not needed
83 doAssertWithCallstack(false);
84 throw ExceptionMessage(BufferException, _("Buffer Error!"),
85 formatHelper(d, expr, file, line));
89 void doAppErr(char const * expr, char const * file, long line)
91 docstring const d = _("LyX has encountered an application error\nand will now shut down.");
92 // comment this out if not needed
93 doAssertWithCallstack(false);
94 throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
95 formatHelper(d, expr, file, line));
99 docstring printCallStack()
101 #ifndef LYX_CALLSTACK_PRINTING
104 const int depth = 200;
106 // get void*'s for all entries on the stack
108 size_t size = backtrace(array, depth);
110 char** messages = backtrace_symbols(array, size);
113 for (size_t i = 1; i < size && messages != nullptr; i++) {
114 const std::string orig(messages[i]);
115 char* mangled = nullptr;
116 for (char *p = messages[i]; *p; ++p) {
120 } else if (*p == '+') {
126 const char* demangled =
127 abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
128 const QByteArray line = QString("(%1) %2: %3\n").arg(i, 3).arg(messages[i])
129 .arg(demangled ? demangled : orig.c_str()).toLocal8Bit();
130 free((void*)demangled);
132 fprintf(stderr, "%s", line.constData());
133 bt += from_local8bit(line.constData());