]> git.lyx.org Git - lyx.git/blob - src/support/lassert.cpp
9deeaaf79e5246a1f6fbc89b42ce66692c96f669
[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/convert.h"
15 #include "support/debug.h"
16 #include "support/docstring.h"
17 #include "support/ExceptionMessage.h"
18 #include "support/gettext.h"
19 #include "support/lstrings.h"
20
21 #include <boost/assert.hpp>
22
23
24 //#define LYX_CALLSTACK_PRINTING
25 // must be linked with -rdynamic
26 #ifdef LYX_CALLSTACK_PRINTING
27 #include <cstdio>
28 #include <cstdlib>
29 #include <execinfo.h>
30 #include <cxxabi.h>
31 #endif
32
33
34 namespace lyx {
35
36 using namespace std;
37 using namespace support;
38
39 // TODO Should we try to print the call stack in the course of these?
40
41 void doAssert(char const * expr, char const * file, long line)
42 {
43         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
44         // comment this out if not needed
45         BOOST_ASSERT(false);
46 }
47
48
49 docstring formatHelper(docstring const & msg,
50         char const * expr, char const * file, long line)
51 {
52         static const docstring d = 
53                 from_ascii(N_("Assertion %1$s violated in\nfile: %2$s, line: %3$s"));
54         
55         return bformat(d, from_ascii(expr), from_ascii(file), 
56                 convert<docstring>(line)) + '\n' + msg;
57 }
58
59
60 void doWarnIf(char const * expr, char const * file, long line)
61 {
62         static const docstring d =
63             from_ascii(N_("It should be safe to continue, but you may wish to save your work and restart LyX."));
64         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
65         // comment this out if not needed
66         BOOST_ASSERT(false);
67         throw ExceptionMessage(WarningException, _("Warning!"), 
68                 formatHelper(d, expr, file, line));
69 }
70
71
72 void doBufErr(char const * expr, char const * file, long line)
73 {
74         static const docstring d =
75             from_ascii(N_("There has been an error with this document. LyX will attempt to close it safely."));
76         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
77         // comment this out if not needed
78         BOOST_ASSERT(false);
79         throw ExceptionMessage(BufferException, _("Buffer Error!"),
80                 formatHelper(d, expr, file, line));
81 }
82
83
84 void doAppErr(char const * expr, char const * file, long line)
85 {
86         static const docstring d =
87             from_ascii(N_("LyX has encountered an application error and will now shut down."));
88         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
89         // comment this out if not needed
90         BOOST_ASSERT(false);
91         throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
92                 formatHelper(d, expr, file, line));
93 }
94
95
96 //TODO Return as string, so call stack could be used in dialogs.
97 void printCallStack()
98 {
99 #ifdef LYX_CALLSTACK_PRINTING
100         const int depth = 50;
101         
102         // get void*'s for all entries on the stack
103         void* array[depth];
104         size_t size = backtrace(array, depth);
105         
106         char** messages = backtrace_symbols(array, size);
107         
108         for (size_t i = 0; i < size && messages != NULL; i++) {
109                 std::string orig(messages[i]);
110                 // extract mangled: bin/lyx2.0(_ZN3lyx7support7packageEv+0x32) [0x8a2e02b]
111                 char* mangled = 0;
112                 for (char *p = messages[i]; *p; ++p) {
113                         if (*p == '(') {
114                                 *p = 0;
115                                 mangled = p + 1;
116                         } else if (*p == '+') {
117                                 *p = 0;
118                                 break;
119                         }
120                 }
121                 int err = 0;
122                 char* demangled = abi::__cxa_demangle(mangled, 0, 0, &err);
123                 if (err == 0) {
124                         fprintf(stderr, "[bt]: (%d) %s %s\n", i, messages[i], demangled);
125                         free((void*)demangled);
126                 } else {
127                         fprintf(stderr, "[bt]: (%d) %s\n", i, orig.c_str());
128                 }               
129         }
130 #endif
131 }
132
133 } // namespace lyx