]> granicus.if.org Git - pdns/commitdiff
implement json log grep (generic)
authorBert Hubert <bert.hubert@netherlabs.nl>
Sun, 18 Nov 2012 19:07:11 +0000 (19:07 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Sun, 18 Nov 2012 19:07:11 +0000 (19:07 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2902 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/json.cc
pdns/json.hh

index 13030e7bd0dddde6cbd0d6297405047fc4e88092..2d4226b797dd4c59f9d7a2483bea91cc0c655fec 100644 (file)
@@ -1,5 +1,11 @@
 #include "json.hh"
 #include "namespaces.hh"
+#include <stdio.h>
+#include <boost/circular_buffer.hpp>
+#include <boost/tokenizer.hpp>
+#include "namespaces.hh"
+#include "misc.hh"
+#include <boost/foreach.hpp>
 
 std::string escapeJSON( const std::string & name)
 {
@@ -29,3 +35,49 @@ string returnJSONObject(const map<string, string>& items)
   ostr<<"}";
   return ostr.str();
 }
+
+string makeLogGrepJSON(map<string, string>& varmap, const string& fname, const string& prefix)
+{
+  FILE* ptr = fopen(fname.c_str(), "r");
+  if(!ptr) {
+    return "[]";
+  }
+  boost::shared_ptr<FILE> fp(ptr, fclose);
+
+  string line;
+  string needle=varmap["needle"];
+  trim_right(needle);
+
+  boost::replace_all(needle, "%20", " ");  
+  boost::replace_all(needle, "%22", "\"");    
+
+  boost::tokenizer<boost::escaped_list_separator<char> > t(needle, boost::escaped_list_separator<char>("\\", " ", "\""));
+  vector<string> matches(t.begin(), t.end());
+  matches.push_back(prefix);
+  
+  boost::circular_buffer<string> lines(200);
+  while(stringfgets(fp.get(), line)) {
+    vector<string>::const_iterator iter;
+    for(iter = matches.begin(); iter != matches.end(); ++iter) {
+      if(!strcasestr(line.c_str(), iter->c_str()))
+        break;
+    }
+    if(iter == matches.end()) { 
+      trim_right(line);
+      lines.push_front(line);
+    }
+  }
+  bool first=true;
+  string ret="[";
+  if(!lines.empty()) {
+    BOOST_FOREACH(const string& line, lines) {
+      if(!first) {
+        ret += ",\n";
+      }
+      else first=false;
+      ret += "[\"" + escapeJSON(line)+"\"]";
+    }
+  }
+  ret+="]";
+  return ret;
+}
index 25e3217099fb18031f80de0e0b6f8dfc17aed9be..8912b697be123bdd40d30e1991adab3a249586b2 100644 (file)
@@ -21,4 +21,5 @@
 #include <map>
 
 std::string returnJSONObject(const std::map<std::string, std::string>& items);
-std::string escapeJSON( const std::string & name);
\ No newline at end of file
+std::string escapeJSON( const std::string & name);
+std::string makeLogGrepJSON(std::map<std::string, std::string>& varmap, const std::string& fname, const std::string& prefix="");