]> granicus.if.org Git - pdns/commitdiff
Enhancement for Issue #1074. Added operator overload for pdnslog() to accept a log...
authorZack A <zma4580@gmail.com>
Tue, 22 Oct 2013 16:27:07 +0000 (12:27 -0400)
committerZack A <zma4580@gmail.com>
Fri, 25 Oct 2013 12:53:25 +0000 (08:53 -0400)
pdns/lua-pdns.cc
pdns/lua-pdns.hh

index 9d4d4b0499bbcf56048f45d3c10fdf74062e594d..3d69d45a6b780e9d49fe0f71b342e43da62edcbe 100644 (file)
@@ -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");
index 51ac65c54167315bdfc7acc2b4c1bc63ec24e3b6..de9bdbfb1cb8df465eca44912ab51932d31ba386 100644 (file)
@@ -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