X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2Flassert.cpp;h=fa2ffb063c8ee5446558729d7d7e37fdac348495;hb=8d640dc77608bedddb5b00982c23665584f52d21;hp=9c0daff32876de3f9e8d7e1b7048db458da5b6ca;hpb=1d5e483b442885cf67f7ff6d1c505ec7b978e7e1;p=lyx.git diff --git a/src/support/lassert.cpp b/src/support/lassert.cpp index 9c0daff328..fa2ffb063c 100644 --- a/src/support/lassert.cpp +++ b/src/support/lassert.cpp @@ -10,14 +10,19 @@ */ #include +#include +#include "support/convert.h" #include "support/debug.h" +#include "support/docstring.h" +#include "support/ExceptionMessage.h" +#include "support/gettext.h" +#include "support/lstrings.h" #include +#include -//#define LYX_CALLSTACK_PRINTING -// must be linked with -rdynamic #ifdef LYX_CALLSTACK_PRINTING #include #include @@ -28,21 +33,74 @@ namespace lyx { -void doAssert(char const * expr, char const * file, long line) +using namespace std; +using namespace support; + + +void doAssertWithCallstack(bool value) { - // TODO Should we try to print the call stack before exiting? + 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); } -//TODO Return as string, so call stack could be used in dialogs. -void printCallStack() +docstring formatHelper(docstring const & msg, + char const * expr, char const * file, long line) { -#ifdef LYX_CALLSTACK_PRINTING - const int depth = 50; + 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(line)) + '\n' + msg; +} + + +void doWarnIf(char const * expr, char const * file, long 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 + doAssertWithCallstack(false); + throw ExceptionMessage(WarningException, _("Warning!"), + formatHelper(d, expr, file, line)); +} + + +void doBufErr(char const * expr, char const * file, long 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 + doAssertWithCallstack(false); + throw ExceptionMessage(BufferException, _("Buffer Error!"), + formatHelper(d, expr, file, line)); +} + + +void doAppErr(char const * expr, char const * file, long line) +{ + docstring const d = _("LyX has encountered an application error\nand will now shut down."); + // comment this out if not needed + doAssertWithCallstack(false); + throw ExceptionMessage(ErrorException, _("Fatal Exception!"), + formatHelper(d, expr, file, line)); +} + + +docstring printCallStack() +{ +#ifndef LYX_CALLSTACK_PRINTING + return docstring(); +#else + const int depth = 200; // get void*'s for all entries on the stack void* array[depth]; @@ -50,9 +108,9 @@ void printCallStack() 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 == '(') { @@ -63,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 }