Permalink
Browse files

Enhancement for Issue #1074. Added operator overload for pdnslog() to…

… accept a log level when writing out to syslog. Added syslog levels to global lua table which correspond to the second argument in pdnslog. Example: pdnslog(INFO, pdnslog.Info), pdnslog(DEBUG, pdnslog.Debug) , these all correspond to their name in the Urgency enum
  • Loading branch information...
1 parent 244ad69 commit 674a305358ed0625ee984678c1ca0f61bd8fc7a4 @zmallen zmallen committed Oct 22, 2013
Showing with 91 additions and 21 deletions.
  1. +90 −21 pdns/lua-pdns.cc
  2. +1 −0 pdns/lua-pdns.hh
View
@@ -110,7 +110,30 @@ void pushResourceRecordsTable(lua_State* lua, const vector<DNSResourceRecord>& 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()<<Logger::Error<<"From Lua script: "<<message<<endl;
+ } else if(argc >= 2) {
+ string message=lua_tostring(lua, 1);
+ int urgencylevel = lua_tonumber(lua, 2);
+ switch(urgencylevel)
+ {
+ case Logger::Alert:
+ theL()<<Logger::Alert<<message<<endl;
+ break;
+ case Logger::Critical:
+ theL()<<Logger::Critical<<message<<endl;
+ break;
+ case Logger::Error:
+ theL()<<Logger::Error<<message<<endl;
+ break;
+ case Logger::Warning:
+ theL()<<Logger::Warning<<message<<endl;
+ break;
+ case Logger::Notice:
+ theL()<<Logger::Notice<<message<<endl;
+ break;
+ case Logger::Info:
+ theL()<<Logger::Info<<message<<endl;
+ break;
+ case Logger::Debug:
+ theL()<<Logger::Debug<<message<<endl;
+ break;
+ case Logger::All:
+ theL()<<Logger::All<<message<<endl;
+ break;
+ case Logger::NTLog:
+ theL()<<Logger::All<<message<<endl;
+ break;
+ case Logger::None:
+ theL()<<Logger::None<<message<<endl;
+ break;
+ default:
+ theL()<<Logger::Error<<message<<endl;
+ break;
+ }
}
return 0;
}
@@ -234,25 +300,6 @@ PowerDNSLua::PowerDNSLua(const std::string& fname)
lua_pushcfunction(d_lua, logLua);
lua_setglobal(d_lua, "pdnslog");
-#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_newtable(d_lua);
for(vector<QType::namenum>::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");
View
@@ -33,5 +33,6 @@ protected: // FIXME?
void pushResourceRecordsTable(lua_State* lua, const vector<DNSResourceRecord>& records);
void popResourceRecordsTable(lua_State *lua, const string &query, vector<DNSResourceRecord>& ret);
+void pushSyslogSecurityLevelTable(lua_State *lua);
int getLuaTableLength(lua_State* lua, int depth);
#endif

0 comments on commit 674a305

Please sign in to comment.