Add C++ traceback to errors

This commit is contained in:
Eduardo Bart 2013-03-12 01:33:45 -03:00
parent 24664714bd
commit 0891e2b30a
5 changed files with 48 additions and 5 deletions

View File

@ -285,6 +285,7 @@ function addLine(text, color)
flushEvent = scheduleEvent(flushLines, 10)
end
text = string.gsub(text, '\t', ' ')
table.insert(cachedLines, {text=text, color=color})
end

View File

@ -28,6 +28,7 @@
#ifdef FW_GRAPHICS
#include <framework/platform/platformwindow.h>
#include <framework/platform/platform.h>
#include <framework/luaengine/luainterface.h>
#endif
@ -104,16 +105,16 @@ void Logger::logFunc(Fw::LogLevel level, const std::string& message, std::string
prettyFunction = prettyFunction.substr(prettyFunction.find_last_of(' ') + 1);
std::string out = message;
std::stringstream ss;
ss << message;
if(!prettyFunction.empty()) {
if(g_lua.isInCppCallback())
out = g_lua.traceback(out, 1);
else
out += "\nat:\t[C++]: " + prettyFunction;
ss << g_lua.traceback("", 1);
ss << g_platform.traceback(prettyFunction, 1, 8);
}
log(level, out);
log(level, ss.str());
}
void Logger::fireOldMessages()

View File

@ -45,6 +45,7 @@ public:
std::string getCPUName();
double getTotalSystemMemory();
std::string getOSName();
std::string traceback(const std::string& where, int level = 1, int maxDepth = 32);
};
extern Platform g_platform;

View File

@ -29,6 +29,7 @@
#include <framework/stdext/stdext.h>
#include <sys/stat.h>
#include <execinfo.h>
void Platform::processArgs(std::vector<std::string>& args)
{
@ -168,5 +169,35 @@ std::string Platform::getOSName()
return std::string();
}
std::string Platform::traceback(const std::string& where, int level, int maxDepth)
{
std::stringstream ss;
ss << "\nC++ stack traceback:";
if(!where.empty())
ss << "\n\t[C++]: " << where;
void* buffer[maxDepth + level + 1];
int numLevels = backtrace(buffer, maxDepth + level + 1);
char **tracebackBuffer = backtrace_symbols(buffer, numLevels);
if(tracebackBuffer) {
for(int i = 1 + level; i < numLevels; i++) {
std::string line = tracebackBuffer[i];
if(line.find("__libc_start_main") != std::string::npos)
break;
std::size_t demanglePos = line.find("(_Z");
if(demanglePos != std::string::npos) {
demanglePos++;
int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
std::string funcName = line.substr(demanglePos, len);
line.replace(demanglePos, len, stdext::demangle_name(funcName.c_str()));
}
ss << "\n\t" << line;
}
free(tracebackBuffer);
}
return ss.str();
}
#endif

View File

@ -414,4 +414,13 @@ std::string Platform::getOSName()
return ret;
}
std::string Platform::traceback(const std::string& where)
{
std::stringstream ss;
ss << "\nat:";
ss << "\n\t[C++]: " << where;
return ss.str();
}
#endif