diff --git a/pdns/lua-pdns.cc b/pdns/lua-pdns.cc index 9d4d4b0499bb..3d69d45a6b78 100644 --- a/pdns/lua-pdns.cc +++ b/pdns/lua-pdns.cc @@ -110,7 +110,30 @@ void pushResourceRecordsTable(lua_State* lua, const vector& r lua_settable(lua, -3); // pushes the table we just built into the master table at position pushed above } } - +// this function takes the global lua_state from the PowerDNSLua constructor and populates it with the syslog enums values +void pushSyslogSecurityLevelTable(lua_State* lua) +{ + lua_pushnumber(lua, Logger::All); + lua_setfield(lua, -2, "All"); + lua_pushnumber(lua, Logger::NTLog); + lua_setfield(lua, -2, "NTLog"); + lua_pushnumber(lua, Logger::Alert); + lua_setfield(lua, -2, "Alert"); + lua_pushnumber(lua, Logger::Critical); + lua_setfield(lua, -2, "Critical"); + lua_pushnumber(lua, Logger::Error); + lua_setfield(lua, -2, "Error"); + lua_pushnumber(lua, Logger::Warning); + lua_setfield(lua, -2, "Warning"); + lua_pushnumber(lua, Logger::Notice); + lua_setfield(lua, -2, "Notice"); + lua_pushnumber(lua, Logger::Info); + lua_setfield(lua, -2, "Info"); + lua_pushnumber(lua, Logger::Debug); + lua_setfield(lua, -2, "Debug"); + lua_pushnumber(lua, Logger::None); + lua_setfield(lua, -2, "None"); +} int getLuaTableLength(lua_State* lua, int depth) { #ifndef LUA_VERSION_NUM @@ -215,9 +238,52 @@ int setVariableLua(lua_State* lua) int logLua(lua_State *lua) { - if(lua_gettop(lua) >= 1) { + // get # of arguments from the pdnslog() lua stack + // if it is 1, then the old pdnslog(msg) is used, which we keep for posterity and to prevent lua scripts from breaking + // if it is >= 2, then we process it as pdnslog(msg, urgencylevel) for more granular logging + int argc = lua_gettop(lua); + if(argc == 1) { string message=lua_tostring(lua, 1); theL()<= 2) { + string message=lua_tostring(lua, 1); + int urgencylevel = lua_tonumber(lua, 2); + switch(urgencylevel) + { + case Logger::Alert: + theL()<::const_iterator iter = QType::names.begin(); iter != QType::names.end(); ++iter) { @@ -271,7 +318,29 @@ PowerDNSLua::PowerDNSLua(const std::string& fname) lua_setfield(d_lua, -2, "NOTIMP"); lua_pushnumber(d_lua, 5); lua_setfield(d_lua, -2, "REFUSED"); + // set syslog codes used by Logger/enum Urgency + pushSyslogSecurityLevelTable(d_lua); + lua_setglobal(d_lua, "pdns"); + +#ifndef LUA_VERSION_NUM + luaopen_base(d_lua); + luaopen_string(d_lua); + + if(lua_dofile(d_lua, fname.c_str())) +#else + luaL_openlibs(d_lua); + if(luaL_dofile(d_lua, fname.c_str())) +#endif + throw runtime_error(string("Error loading Lua file '")+fname+"': "+ string(lua_isstring(d_lua, -1) ? lua_tostring(d_lua, -1) : "unknown error")); + + lua_settop(d_lua, 0); + + lua_pushcfunction(d_lua, setVariableLua); + lua_setglobal(d_lua, "setvariable"); + + lua_pushcfunction(d_lua, getLocalAddressLua); + lua_setglobal(d_lua, "getlocaladdress"); lua_pushlightuserdata(d_lua, (void*)this); lua_setfield(d_lua, LUA_REGISTRYINDEX, "__PowerDNSLua"); diff --git a/pdns/lua-pdns.hh b/pdns/lua-pdns.hh index 51ac65c54167..de9bdbfb1cb8 100644 --- a/pdns/lua-pdns.hh +++ b/pdns/lua-pdns.hh @@ -33,5 +33,6 @@ protected: // FIXME? void pushResourceRecordsTable(lua_State* lua, const vector& records); void popResourceRecordsTable(lua_State *lua, const string &query, vector& ret); +void pushSyslogSecurityLevelTable(lua_State *lua); int getLuaTableLength(lua_State* lua, int depth); #endif