]> git.lyx.org Git - lyx.git/blob - src/support/lassert.cpp
Remove warning
[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         docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
53         LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
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         docstring const d = _("It should be safe to continue, but you\nmay wish to save your work and restart LyX.");
63         // comment this out if not needed
64         BOOST_ASSERT(false);
65         throw ExceptionMessage(WarningException, _("Warning!"), 
66                 formatHelper(d, expr, file, line));
67 }
68
69
70 void doBufErr(char const * expr, char const * file, long line)
71 {
72         docstring const d = _("There has been an error with this document.\nLyX will attempt to close it safely.");
73         // comment this out if not needed
74         BOOST_ASSERT(false);
75         throw ExceptionMessage(BufferException, _("Buffer Error!"),
76                 formatHelper(d, expr, file, line));
77 }
78
79
80 void doAppErr(char const * expr, char const * file, long line)
81 {
82         docstring const d = _("LyX has encountered an application error\nand will now shut down.");
83         // comment this out if not needed
84         BOOST_ASSERT(false);
85         throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
86                 formatHelper(d, expr, file, line));
87 }
88
89
90 //TODO Return as string, so call stack could be used in dialogs.
91 void printCallStack()
92 {
93 #ifdef LYX_CALLSTACK_PRINTING
94         const int depth = 50;
95         
96         // get void*'s for all entries on the stack
97         void* array[depth];
98         size_t size = backtrace(array, depth);
99         
100         char** messages = backtrace_symbols(array, size);
101         
102         for (size_t i = 0; i < size && messages != NULL; i++) {
103                 std::string orig(messages[i]);
104                 // extract mangled: bin/lyx2.0(_ZN3lyx7support7packageEv+0x32) [0x8a2e02b]
105                 char* mangled = 0;
106                 for (char *p = messages[i]; *p; ++p) {
107                         if (*p == '(') {
108                                 *p = 0;
109                                 mangled = p + 1;
110                         } else if (*p == '+') {
111                                 *p = 0;
112                                 break;
113                         }
114                 }
115                 int err = 0;
116                 char* demangled = abi::__cxa_demangle(mangled, 0, 0, &err);
117                 if (err == 0) {
118                         fprintf(stderr, "[bt]: (%d) %s %s\n", i, messages[i], demangled);
119                         free((void*)demangled);
120                 } else {
121                         fprintf(stderr, "[bt]: (%d) %s\n", i, orig.c_str());
122                 }               
123         }
124 #endif
125 }
126
127 } // namespace lyx