From 9833601f819239aa1034d2c5189214f29445dcb0 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 3 May 2012 14:46:48 +0200 Subject: Add debug function for use in lua scripts --- server/src/luascript.cc | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/server/src/luascript.cc b/server/src/luascript.cc index f4aef40..6ba52a9 100644 --- a/server/src/luascript.cc +++ b/server/src/luascript.cc @@ -35,6 +35,19 @@ #define GLOBAL_POINTER "_pracroGlobalLUAObjectPointerThisShouldBeANameThatIsNotAccidentallyOverwritten" +static int _debug(lua_State *L) +{ + Pracro::checkParameters(L, + Pracro::T_STRING, + Pracro::T_END); + + std::string msg = lua_tostring(L, lua_gettop(L)); + + DEBUG(luascript, "%s\n", msg.c_str()); + + return 0; +} + static int _value(lua_State *L) { Pracro::checkParameters(L, @@ -145,7 +158,7 @@ void LUAScript::init() L = luaL_newstate(); if(L == NULL) { - ERR(luaresume, "Could not create LUA state.\n"); + ERR(luascript, "Could not create LUA state.\n"); throw Exception("Could not create LUA state."); } @@ -159,6 +172,7 @@ void LUAScript::init() lua_register(L, "template", _template); lua_register(L, "macro", _macro); lua_register(L, "user", _user); + lua_register(L, "debug", _debug); register_praxisd(L); } @@ -214,7 +228,7 @@ void LUAScript::run() } if(L == NULL) { - ERR(luaresume, "LUA state not initialized!"); + ERR(luascript, "LUA state not initialized!"); return; } @@ -227,16 +241,16 @@ void LUAScript::run() std::string codename = name(); if(i->second != "") codename += ": " + i->second; - DEBUG(luaresume, "Running %s: %s\n", codename.c_str(), program.c_str()); + DEBUG(luascript, "Running %s: %s\n", codename.c_str(), program.c_str()); if(luaL_loadbuffer(L, program.c_str(), program.size(), codename.c_str())) { - ERR(luaresume, "loadbufer: %s\n", lua_tostring(L, lua_gettop(L))); + ERR(luascript, "loadbuffer: %s\n", lua_tostring(L, lua_gettop(L))); throw Exception(lua_tostring(L, lua_gettop(L))); } // Run the loaded code if(lua_pcall(L, 0, LUA_MULTRET, 0)) { - ERR(luaresume, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); + ERR(luascript, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); throw Exception(lua_tostring(L, lua_gettop(L))); } @@ -247,12 +261,12 @@ void LUAScript::run() std::string LUAScript::resultString() throw(Exception) { if(top != lua_gettop(L) - 1) { - ERR(luaresume, "Program did not return a single value.\n"); + ERR(luascript, "Program did not return a single value.\n"); throw Exception("Program did not return a single value."); } if(lua_isstring(L, lua_gettop(L)) == false) { - ERR(luaresume, "Program did not return a string value.\n"); + ERR(luascript, "Program did not return a string value.\n"); throw Exception("Program did not return a string value."); } -- cgit v1.2.3