From c875856d20f4963f5f20ceb8cf8f71d81b5d6036 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Sat, 21 Aug 2010 19:25:37 +0000 Subject: SWORD25: Implemented script tracing To turn on traces turn on debugchannel 'script' and then use debug level as a bitmask: 1 - show function calls 2 - show function exits 3 - show every line svn-id: r53267 --- engines/sword25/script/luascript.cpp | 24 ++++++++++++++++++++++++ engines/sword25/sword25.cpp | 3 +++ 2 files changed, 27 insertions(+) diff --git a/engines/sword25/script/luascript.cpp b/engines/sword25/script/luascript.cpp index f3b98a5c16..c6508c325f 100644 --- a/engines/sword25/script/luascript.cpp +++ b/engines/sword25/script/luascript.cpp @@ -39,6 +39,9 @@ // ----------------------------------------------------------------------------- #include "common/array.h" +#include "common/debug-channels.h" + +#include "sword25/sword25.h" #include "sword25/package/packagemanager.h" #include "sword25/script/luascript.h" #include "sword25/script/luabindhelper.h" @@ -92,6 +95,13 @@ int PanicCB(lua_State *L) { BS_LOG_ERRORLN("Lua panic. Error message: %s", lua_isnil(L, -1) ? "" : lua_tostring(L, -1)); return 0; } + +void debugHook(lua_State *L, lua_Debug *ar) { + if (!lua_getinfo(L, "Sn", ar)) + return; + + debug("LUA: %s %s: %s %d", ar->namewhat, ar->name, ar->short_src, ar->currentline); +} } // ----------------------------------------------------------------------------- @@ -139,6 +149,20 @@ bool LuaScriptEngine::Init() { luaopen_pluto(m_State); lua_pop(m_State, 1); + // Initialize debugging callback + if (DebugMan.isDebugChannelEnabled(kDebugScript)) { + int mask = 0; + if (gDebugLevel == 1) + mask |= LUA_MASKCALL; + if (gDebugLevel == 2) + mask |= LUA_MASKRET; + if (gDebugLevel == 4) + mask |= LUA_MASKLINE; + + if (mask != 0) + lua_sethook(m_State, debugHook, mask, 0); + } + BS_LOGLN("Lua initialized."); return true; diff --git a/engines/sword25/sword25.cpp b/engines/sword25/sword25.cpp index a1f3eb0f46..5a4e2bebad 100644 --- a/engines/sword25/sword25.cpp +++ b/engines/sword25/sword25.cpp @@ -33,6 +33,7 @@ */ #include "common/config-manager.h" +#include "common/debug-channels.h" #include "engines/util.h" #include "sword25/sword25.h" @@ -55,6 +56,8 @@ void LogToStdout(const char *Message) { Sword25Engine::Sword25Engine(OSystem *syst, const ADGameDescription *gameDesc): Engine(syst), _gameDescription(gameDesc) { + + DebugMan.addDebugChannel(kDebugScript, "Script", "Script debug level"); } Sword25Engine::~Sword25Engine() { -- cgit v1.2.3