Improve lua stack trace display

This commit is contained in:
Thomas Goyne 2013-09-21 11:22:58 -07:00
parent 8fade74026
commit 1a8fbf35f1

View file

@ -57,9 +57,12 @@
#include <algorithm> #include <algorithm>
#include <boost/algorithm/string/classification.hpp> #include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp> #include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp> #include <boost/tokenizer.hpp>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
@ -601,7 +604,7 @@ namespace Automation4 {
return lua_gettop(L) - pretop; return lua_gettop(L) - pretop;
} }
static int append_stack_trace(lua_State *L) static int add_stack_trace(lua_State *L)
{ {
int level = 1; int level = 1;
if (lua_isnumber(L, 2)) { if (lua_isnumber(L, 2)) {
@ -609,41 +612,43 @@ namespace Automation4 {
lua_pop(L, 1); lua_pop(L, 1);
} }
if (lua_gettop(L) == 0) std::string message = get_string_or_default(L, 1);
lua_pushliteral(L, ""); // No error message if (lua_gettop(L))
else if (!lua_isstring(L, 1)) lua_pop(L, 1);
return 1; // Error message isn't a string
else // Strip the location from the error message since it's redundant with
lua_pushliteral(L, "\n"); // the stack trace
boost::regex location("^\\[string \".*\"\\]:[0-9]+: ");
message = regex_replace(message, location, "", boost::format_first_only);
std::vector<std::string> frames;
frames.emplace_back(std::move(message));
lua_Debug ar; lua_Debug ar;
while (lua_getstack(L, level++, &ar)) { while (lua_getstack(L, level++, &ar)) {
lua_pushliteral(L, "\n");
lua_getinfo(L, "Snl", &ar); lua_getinfo(L, "Snl", &ar);
if (ar.what[0] == 't') // tail if (ar.what[0] == 't')
lua_pushfstring(L, "(tail call)"); frames.emplace_back("(tail call)");
else { else {
if (*ar.namewhat) std::string file = ar.source;
lua_pushfstring(L, "%s: ", ar.name); if (file == "=[C]")
else { file = "<C function>";
if (*ar.what == 'm') // main
lua_pushfstring(L, "[main]: ");
else if (*ar.what == 'C') // C
lua_pushliteral(L, "?: ");
else // Anonymous function
lua_pushfstring(L, "? <%d:%d>: ", ar.linedefined, ar.lastlinedefined);
}
lua_pushfstring(L, "%s", ar.source + (ar.source[0] == '=')); std::string function = ar.name ? ar.name : "";
if (ar.currentline > 0) if (*ar.what == 'm')
lua_pushfstring(L, ":%d", ar.currentline); function = "<main>";
else if (*ar.what == 'C')
function = '?';
else if (!*ar.namewhat)
function = str(boost::format("<anonymous function at lines %d-%d>") % ar.linedefined % ar.lastlinedefined);
frames.emplace_back(str(boost::format(" File \"%s\", line %d\n%s") % file % ar.currentline % function));
} }
lua_concat(L, lua_gettop(L));
} }
lua_concat(L, lua_gettop(L));
push_value(L, join(frames | boost::adaptors::reversed, "\n"));
return 1; return 1;
} }
@ -655,7 +660,7 @@ namespace Automation4 {
LuaProgressSink lps(L, ps, can_open_config); LuaProgressSink lps(L, ps, can_open_config);
// Insert our error handler under the function to call // Insert our error handler under the function to call
lua_pushcclosure(L, append_stack_trace, 0); lua_pushcclosure(L, add_stack_trace, 0);
lua_insert(L, -nargs - 2); lua_insert(L, -nargs - 2);
if (lua_pcall(L, nargs, nresults, -nargs - 2)) { if (lua_pcall(L, nargs, nresults, -nargs - 2)) {