]> git.lyx.org Git - lyx.git/blobdiff - src/support/lassert.cpp
Fix bug #4269
[lyx.git] / src / support / lassert.cpp
index 9deeaaf79e5246a1f6fbc89b42ce66692c96f669..c690dbcee03a213a2a10c771dc5ea1c945f50d7b 100644 (file)
@@ -10,6 +10,7 @@
  */
 
 #include <config.h>
+#include <lassert.h>
 
 #include "support/convert.h"
 #include "support/debug.h"
@@ -20,9 +21,8 @@
 
 #include <boost/assert.hpp>
 
+#include <QString>
 
-//#define LYX_CALLSTACK_PRINTING
-// must be linked with -rdynamic
 #ifdef LYX_CALLSTACK_PRINTING
 #include <cstdio>
 #include <cstdlib>
@@ -36,46 +36,50 @@ namespace lyx {
 using namespace std;
 using namespace support;
 
-// TODO Should we try to print the call stack in the course of these?
+
+void doAssertWithCallstack(bool value)
+{
+       if (!value) {
+               printCallStack();
+               BOOST_ASSERT(false);
+       }
+}
+
 
 void doAssert(char const * expr, char const * file, long line)
 {
        LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
        // comment this out if not needed
-       BOOST_ASSERT(false);
+       doAssertWithCallstack(false);
 }
 
 
 docstring formatHelper(docstring const & msg,
        char const * expr, char const * file, long line)
 {
-       static const docstring d = 
-               from_ascii(N_("Assertion %1$s violated in\nfile: %2$s, line: %3$s"));
-       
-       return bformat(d, from_ascii(expr), from_ascii(file), 
+       docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
+       LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
+
+       return bformat(d, from_ascii(expr), from_ascii(file),
                convert<docstring>(line)) + '\n' + msg;
 }
 
 
 void doWarnIf(char const * expr, char const * file, long line)
 {
-       static const docstring d =
-           from_ascii(N_("It should be safe to continue, but you may wish to save your work and restart LyX."));
-       LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
+       docstring const d = _("It should be safe to continue, but you\nmay wish to save your work and restart LyX.");
        // comment this out if not needed
-       BOOST_ASSERT(false);
-       throw ExceptionMessage(WarningException, _("Warning!"), 
+       doAssertWithCallstack(false);
+       throw ExceptionMessage(WarningException, _("Warning!"),
                formatHelper(d, expr, file, line));
 }
 
 
 void doBufErr(char const * expr, char const * file, long line)
 {
-       static const docstring d =
-           from_ascii(N_("There has been an error with this document. LyX will attempt to close it safely."));
-       LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
+       docstring const d = _("There has been an error with this document.\nLyX will attempt to close it safely.");
        // comment this out if not needed
-       BOOST_ASSERT(false);
+       doAssertWithCallstack(false);
        throw ExceptionMessage(BufferException, _("Buffer Error!"),
                formatHelper(d, expr, file, line));
 }
@@ -83,31 +87,30 @@ void doBufErr(char const * expr, char const * file, long line)
 
 void doAppErr(char const * expr, char const * file, long line)
 {
-       static const docstring d =
-           from_ascii(N_("LyX has encountered an application error and will now shut down."));
-       LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
+       docstring const d = _("LyX has encountered an application error\nand will now shut down.");
        // comment this out if not needed
-       BOOST_ASSERT(false);
+       doAssertWithCallstack(false);
        throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
                formatHelper(d, expr, file, line));
 }
 
 
-//TODO Return as string, so call stack could be used in dialogs.
-void printCallStack()
+docstring printCallStack()
 {
-#ifdef LYX_CALLSTACK_PRINTING
-       const int depth = 50;
-       
+#ifndef LYX_CALLSTACK_PRINTING
+       return docstring();
+#else
+       const int depth = 200;
+
        // get void*'s for all entries on the stack
        void* array[depth];
        size_t size = backtrace(array, depth);
-       
+
        char** messages = backtrace_symbols(array, size);
-       
-       for (size_t i = 0; i < size && messages != NULL; i++) {
-               std::string orig(messages[i]);
-               // extract mangled: bin/lyx2.0(_ZN3lyx7support7packageEv+0x32) [0x8a2e02b]
+
+       docstring bt;
+       for (size_t i = 1; i < size && messages != NULL; i++) {
+               const std::string orig(messages[i]);
                char* mangled = 0;
                for (char *p = messages[i]; *p; ++p) {
                        if (*p == '(') {
@@ -118,15 +121,16 @@ void printCallStack()
                                break;
                        }
                }
-               int err = 0;
-               char* demangled = abi::__cxa_demangle(mangled, 0, 0, &err);
-               if (err == 0) {
-                       fprintf(stderr, "[bt]: (%d) %s %s\n", i, messages[i], demangled);
-                       free((void*)demangled);
-               } else {
-                       fprintf(stderr, "[bt]: (%d) %s\n", i, orig.c_str());
-               }               
+               int status = 0;
+               const char* demangled = abi::__cxa_demangle(mangled, 0, 0, &status);
+               const QByteArray line = QString("(%1) %2: %3\n").arg(i, 3).arg(messages[i])
+                                                               .arg(demangled ? demangled : orig.c_str()).toLocal8Bit();
+               free((void*)demangled);
+
+               fprintf(stderr, "%s", line.constData());
+               bt += from_local8bit(line.constData());
        }
+               return bt;
 #endif
 }