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.
15 #include "support/convert.h"
16 #include "support/debug.h"
17 #include "support/docstring.h"
18 #include "support/ExceptionMessage.h"
19 #include "support/gettext.h"
20 #include "support/lstrings.h"
22 #include <boost/assert.hpp>
26 #ifdef LYX_CALLSTACK_PRINTING
37 using namespace support;
40 void doAssertWithCallstack(bool value)
49 void doAssert(char const * expr, char const * file, long line)
51 LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
52 // comment this out if not needed
53 doAssertWithCallstack(false);
57 docstring formatHelper(docstring const & msg,
58 char const * expr, char const * file, long line)
60 docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
61 LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
63 return bformat(d, from_ascii(expr), from_ascii(file),
64 convert<docstring>(line)) + '\n' + msg;
68 void doWarnIf(char const * expr, char const * file, long line)
70 docstring const d = _("It should be safe to continue, but you\nmay wish to save your work and restart LyX.");
71 // comment this out if not needed
72 doAssertWithCallstack(false);
73 throw ExceptionMessage(WarningException, _("Warning!"),
74 formatHelper(d, expr, file, line));
78 void doBufErr(char const * expr, char const * file, long line)
80 docstring const d = _("There has been an error with this document.\nLyX will attempt to close it safely.");
81 // comment this out if not needed
82 doAssertWithCallstack(false);
83 throw ExceptionMessage(BufferException, _("Buffer Error!"),
84 formatHelper(d, expr, file, line));
88 void doAppErr(char const * expr, char const * file, long line)
90 docstring const d = _("LyX has encountered an application error\nand will now shut down.");
91 // comment this out if not needed
92 doAssertWithCallstack(false);
93 throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
94 formatHelper(d, expr, file, line));
98 docstring printCallStack()
100 #ifndef LYX_CALLSTACK_PRINTING
103 const int depth = 200;
105 // get void*'s for all entries on the stack
107 size_t size = backtrace(array, depth);
109 char** messages = backtrace_symbols(array, size);
112 for (size_t i = 1; i < size && messages != NULL; i++) {
113 const std::string orig(messages[i]);
115 for (char *p = messages[i]; *p; ++p) {
119 } else if (*p == '+') {
125 const char* demangled = abi::__cxa_demangle(mangled, 0, 0, &status);
126 const QByteArray line = QString("(%1) %2: %3\n").arg(i, 3).arg(messages[i])
127 .arg(demangled ? demangled : orig.c_str()).toLocal8Bit();
128 free((void*)demangled);
130 fprintf(stderr, "%s", line.constData());
131 bt += from_local8bit(line.constData());