]> git.lyx.org Git - lyx.git/blob - src/support/lassert.cpp
Make BufferView::singeParUpdate more robust
[lyx.git] / src / support / lassert.cpp
1 /**
2  * \file lassert.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Peter Kümmel
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "support/lassert.h"
15
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"
22
23 #include <boost/assert.hpp>
24
25
26 #ifdef LYX_CALLSTACK_PRINTING
27 #include <cstdio>
28 #include <cstdlib>
29 #include <execinfo.h>
30 #include <cxxabi.h>
31 #include <QString>
32 #endif
33
34
35 namespace lyx {
36
37 using namespace std;
38 using namespace support;
39
40
41 void doAssertWithCallstack(bool value)
42 {
43         if (!value) {
44                 printCallStack();
45                 BOOST_ASSERT(false);
46         }
47 }
48
49
50 void doAssert(char const * expr, char const * file, long line)
51 {
52         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
53         // comment this out if not needed
54         doAssertWithCallstack(false);
55 }
56
57
58 docstring formatHelper(docstring const & msg,
59         char const * expr, char const * file, long line)
60 {
61         docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
62         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
63
64         return bformat(d, from_ascii(expr), from_ascii(file),
65                 convert<docstring>(line)) + '\n' + msg;
66 }
67
68
69 void doWarnIf(char const * expr, char const * file, long line)
70 {
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));
76 }
77
78
79 void doBufErr(char const * expr, char const * file, long line)
80 {
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));
86 }
87
88
89 void doAppErr(char const * expr, char const * file, long line)
90 {
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));
96 }
97
98
99 docstring printCallStack()
100 {
101 #ifndef LYX_CALLSTACK_PRINTING
102         return docstring();
103 #else
104         const int depth = 200;
105
106         // get void*'s for all entries on the stack
107         void* array[depth];
108         size_t size = backtrace(array, depth);
109
110         char** messages = backtrace_symbols(array, size);
111
112         docstring bt;
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) {
117                         if (*p == '(') {
118                                 *p = 0;
119                                 mangled = p + 1;
120                         } else if (*p == '+') {
121                                 *p = 0;
122                                 break;
123                         }
124                 }
125                 int status = 0;
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);
131
132                 fprintf(stderr, "%s", line.constData());
133                 bt += from_local8bit(line.constData());
134         }
135                 return bt;
136 #endif
137 }
138
139 } // namespace lyx