1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-15 05:59:16 +00:00

Improve Lua-related warning messages - show call stack

This commit is contained in:
fgenesis 2012-05-23 01:16:14 +02:00
parent 2278e55d0c
commit 6ffb668668

View file

@ -319,18 +319,20 @@ static inline void luaPushPointer(lua_State *L, void *ptr)
lua_pushnumber(L, 0); lua_pushnumber(L, 0);
} }
static std::string luaFormatStackInfo(lua_State *L) static std::string luaFormatStackInfo(lua_State *L, int level = 1)
{ {
lua_Debug ar; lua_Debug ar;
if (lua_getstack(L, 1, &ar)) std::ostringstream os;
lua_getinfo(L, "Sl", &ar); if (lua_getstack(L, level, &ar) && lua_getinfo(L, "Sln", &ar))
{
os << ar.short_src << ":" << ar.currentline
<< " ([" << ar.what << "] " << ar.namewhat << " " << (ar.name ? ar.name : "(?)") << ")";
}
else else
{ {
snprintf(ar.short_src, sizeof(ar.short_src), "???"); os << "???:0";
ar.currentline = 0;
} }
std::ostringstream os;
os << ar.short_src << ":" << ar.currentline;
return os.str(); return os.str();
} }
@ -339,6 +341,17 @@ static void scriptDebug(lua_State *L, const std::string& msg)
debugLog(luaFormatStackInfo(L) + ": " + msg); debugLog(luaFormatStackInfo(L) + ": " + msg);
} }
static void scriptError(lua_State *L, const std::string& msg)
{
lua_Debug dummy;
std::ostringstream os;
os << msg;
for (int level = 0; lua_getstack(L, level, &dummy); ++level)
os << '\n' << luaFormatStackInfo(L, level);
scriptError(os.str());
}
#if CHECK_POINTER_TYPES #if CHECK_POINTER_TYPES
// Not intended to be called. // Not intended to be called.
@ -374,11 +387,10 @@ static void ensureType(lua_State *L, T *& ptr, ScriptObjectType ty)
if (!so->isType(ty)) if (!so->isType(ty))
{ {
std::ostringstream os; std::ostringstream os;
os << "WARNING: " << luaFormatStackInfo(L) os << "WARNING: script passed wrong pointer to function (expected type: "
<< ": script passed wrong pointer to function (expected type: "
<< ScriptObject::getTypeString(ty) << "; got: " << ScriptObject::getTypeString(ty) << "; got: "
<< so->getTypeString() << ')'; << so->getTypeString() << ')';
scriptError(os.str()); scriptError(L, os.str());
ptr = NULL; // note that the pointer is passed by reference ptr = NULL; // note that the pointer is passed by reference
} }
@ -618,11 +630,9 @@ luaFunc(indexWarnGlobal)
if (doWarn) if (doWarn)
{ {
std::ostringstream os; std::string s = "WARNING: script tried to get/call undefined global variable ";
os << "WARNING: " << luaFormatStackInfo(L) s += varname;
<< ": script tried to get/call undefined global variable " scriptError(L, s);
<< varname;
scriptError(os.str());
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -647,11 +657,10 @@ luaFunc(newindexWarnGlobal)
if (doWarn) if (doWarn)
{ {
std::ostringstream os; std::ostringstream os;
os << "WARNING: " << luaFormatStackInfo(L) os << "WARNING: script set global "
<< ": script set global " << lua_typename(L, -2)
<< (lua_type(L, -2) == LUA_TFUNCTION ? "function" : "variable")
<< " " << varname; << " " << varname;
scriptError(os.str()); scriptError(L, os.str());
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -670,10 +679,9 @@ luaFunc(indexWarnInstance)
if (lua_isnil(L, -1)) if (lua_isnil(L, -1))
{ {
std::ostringstream os; std::ostringstream os;
os << "WARNING: " << luaFormatStackInfo(L) os << "WARNING: script tried to get/call undefined instance variable "
<< ": script tried to get/call undefined instance variable " << getString(L, -2);
<< lua_tostring(L, -2); scriptError(L, os.str());
scriptError(os.str());
} }
lua_remove(L, -2); lua_remove(L, -2);