From ce403b3d87bac434e631336712e7df570bf4d5ab Mon Sep 17 00:00:00 2001 From: Bert Hubert Date: Sun, 18 Nov 2012 19:07:11 +0000 Subject: [PATCH] implement json log grep (generic) git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@2902 d19b8d6e-7fed-0310-83ef-9ca221ded41b --- pdns/json.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ pdns/json.hh | 3 ++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/pdns/json.cc b/pdns/json.cc index 13030e7bd..2d4226b79 100644 --- a/pdns/json.cc +++ b/pdns/json.cc @@ -1,5 +1,11 @@ #include "json.hh" #include "namespaces.hh" +#include +#include +#include +#include "namespaces.hh" +#include "misc.hh" +#include std::string escapeJSON( const std::string & name) { @@ -29,3 +35,49 @@ string returnJSONObject(const map& items) ostr<<"}"; return ostr.str(); } + +string makeLogGrepJSON(map& varmap, const string& fname, const string& prefix) +{ + FILE* ptr = fopen(fname.c_str(), "r"); + if(!ptr) { + return "[]"; + } + boost::shared_ptr fp(ptr, fclose); + + string line; + string needle=varmap["needle"]; + trim_right(needle); + + boost::replace_all(needle, "%20", " "); + boost::replace_all(needle, "%22", "\""); + + boost::tokenizer > t(needle, boost::escaped_list_separator("\\", " ", "\"")); + vector matches(t.begin(), t.end()); + matches.push_back(prefix); + + boost::circular_buffer lines(200); + while(stringfgets(fp.get(), line)) { + vector::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; +} diff --git a/pdns/json.hh b/pdns/json.hh index 25e321709..8912b697b 100644 --- a/pdns/json.hh +++ b/pdns/json.hh @@ -21,4 +21,5 @@ #include std::string returnJSONObject(const std::map& 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& varmap, const std::string& fname, const std::string& prefix=""); -- 2.40.0