2008-04-17 21:11:41 +00:00
|
|
|
/**
|
2010-08-07 11:42:27 +00:00
|
|
|
* \file lassert.cpp
|
2008-04-17 21:11:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2011-04-25 09:14:50 +00:00
|
|
|
* \author Peter Kümmel
|
2008-04-17 21:11:41 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-04-07 00:19:45 +00:00
|
|
|
#include "support/convert.h"
|
2008-04-17 21:11:41 +00:00
|
|
|
#include "support/debug.h"
|
2013-04-07 00:19:45 +00:00
|
|
|
#include "support/docstring.h"
|
|
|
|
#include "support/ExceptionMessage.h"
|
|
|
|
#include "support/gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
2008-04-17 21:11:41 +00:00
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
|
2011-04-25 09:14:50 +00:00
|
|
|
|
|
|
|
//#define LYX_CALLSTACK_PRINTING
|
|
|
|
// must be linked with -rdynamic
|
|
|
|
#ifdef LYX_CALLSTACK_PRINTING
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-04-17 21:11:41 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2013-04-07 00:19:45 +00:00
|
|
|
using namespace std;
|
|
|
|
using namespace support;
|
|
|
|
|
|
|
|
// TODO Should we try to print the call stack in the course of these?
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
docstring formatHelper(docstring const & msg,
|
|
|
|
char const * expr, char const * file, long line)
|
2008-04-17 21:11:41 +00:00
|
|
|
{
|
2013-04-28 17:06:21 +00:00
|
|
|
docstring const d = _("Assertion %1$s violated in\nfile: %2$s, line: %3$s");
|
|
|
|
LYXERR0("ASSERTION " << expr << " VIOLATED IN " << file << ":" << line);
|
2013-04-07 00:19:45 +00:00
|
|
|
|
|
|
|
return bformat(d, from_ascii(expr), from_ascii(file),
|
|
|
|
convert<docstring>(line)) + '\n' + msg;
|
|
|
|
}
|
2011-04-25 09:14:50 +00:00
|
|
|
|
2013-04-07 00:19:45 +00:00
|
|
|
|
2013-04-27 21:52:55 +00:00
|
|
|
void doWarnIf(char const * expr, char const * file, long line)
|
2013-04-07 00:19:45 +00:00
|
|
|
{
|
2013-04-28 17:06:21 +00:00
|
|
|
docstring const d = _("It should be safe to continue, but you\nmay wish to save your work and restart LyX.");
|
2013-04-07 00:19:45 +00:00
|
|
|
// comment this out if not needed
|
|
|
|
BOOST_ASSERT(false);
|
|
|
|
throw ExceptionMessage(WarningException, _("Warning!"),
|
2013-04-27 21:52:55 +00:00
|
|
|
formatHelper(d, expr, file, line));
|
2013-04-07 00:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-27 21:52:55 +00:00
|
|
|
void doBufErr(char const * expr, char const * file, long line)
|
2013-04-07 00:19:45 +00:00
|
|
|
{
|
2013-04-28 17:06:21 +00:00
|
|
|
docstring const d = _("There has been an error with this document.\nLyX will attempt to close it safely.");
|
2013-04-07 00:19:45 +00:00
|
|
|
// comment this out if not needed
|
|
|
|
BOOST_ASSERT(false);
|
|
|
|
throw ExceptionMessage(BufferException, _("Buffer Error!"),
|
2013-04-27 21:52:55 +00:00
|
|
|
formatHelper(d, expr, file, line));
|
2013-04-07 00:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-27 21:52:55 +00:00
|
|
|
void doAppErr(char const * expr, char const * file, long line)
|
2013-04-07 00:19:45 +00:00
|
|
|
{
|
2013-04-28 17:06:21 +00:00
|
|
|
docstring const d = _("LyX has encountered an application error\nand will now shut down.");
|
2008-04-17 21:11:41 +00:00
|
|
|
// comment this out if not needed
|
|
|
|
BOOST_ASSERT(false);
|
2013-04-07 00:19:45 +00:00
|
|
|
throw ExceptionMessage(ErrorException, _("Fatal Exception!"),
|
2013-04-27 21:52:55 +00:00
|
|
|
formatHelper(d, expr, file, line));
|
2008-04-17 21:11:41 +00:00
|
|
|
}
|
|
|
|
|
2011-04-25 09:14:50 +00:00
|
|
|
|
|
|
|
//TODO Return as string, so call stack could be used in dialogs.
|
|
|
|
void printCallStack()
|
|
|
|
{
|
|
|
|
#ifdef LYX_CALLSTACK_PRINTING
|
|
|
|
const int depth = 50;
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
char* mangled = 0;
|
|
|
|
for (char *p = messages[i]; *p; ++p) {
|
|
|
|
if (*p == '(') {
|
|
|
|
*p = 0;
|
|
|
|
mangled = p + 1;
|
|
|
|
} else if (*p == '+') {
|
|
|
|
*p = 0;
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-04-17 21:11:41 +00:00
|
|
|
} // namespace lyx
|