1 /* $Id: filter_log.cpp,v 1.33 2008-02-20 15:07:52 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "filter_log.hpp"
24 #include "package.hpp"
28 #include <boost/thread/mutex.hpp>
29 #include <boost/date_time/posix_time/posix_time.hpp>
31 #include "gduutil.hpp"
33 #include "xmlutil.hpp"
37 #include <yaz/wrbuf.h>
38 #include <yaz/querytowrbuf.h>
41 namespace mp = metaproxy_1;
42 namespace yf = metaproxy_1::filter;
44 namespace metaproxy_1 {
49 typedef boost::shared_ptr<Log::Impl::LFile> LFilePtr;
52 Impl(const std::string &x = "-");
54 void process(metaproxy_1::Package & package);
55 void configure(const xmlNode * ptr);
57 void openfile(const std::string &fname);
58 // needs to be static to be called by C pointer-to-function-syntax
59 static void stream_write(ODR o, void *handle, int type,
60 const char *buf, int len);
61 // needs to be static to be called by C pointer-to-function-syntax
62 static void option_write(const char *name, void *handle);
64 std::string m_msg_config;
73 // Only used during configure stage (no threading),
74 // for performance avoid opening files which other log filter
75 // instances already have opened
76 static std::list<LFilePtr> filter_log_files;
78 boost::mutex m_session_mutex;
79 std::map<mp::Session, std::string> m_sessions;
82 class Log::Impl::LFile {
88 LFile(std::string fname);
89 LFile(std::string fname, std::ostream &use_this);
95 // define Pimpl wrapper forwarding to Impl
97 yf::Log::Log() : m_p(new Impl)
101 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
106 { // must have a destructor because of boost::scoped_ptr
109 void yf::Log::configure(const xmlNode *xmlnode, bool test_only)
111 m_p->configure(xmlnode);
114 void yf::Log::process(mp::Package &package) const
116 m_p->process(package);
120 // define Implementation stuff
122 // static initialization
123 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
126 // yf::Log::Impl::Impl()
129 // m_req_apdu = false;
130 // m_res_apdu = false;
131 // m_req_session = false;
132 // m_res_session = false;
133 // m_init_options = false;
137 yf::Log::Impl::Impl(const std::string &x)
140 m_user_access(false),
143 m_req_session(false),
144 m_res_session(false),
145 m_init_options(false)
151 yf::Log::Impl::~Impl()
156 void yf::Log::Impl::configure(const xmlNode *ptr)
158 for (ptr = ptr->children; ptr; ptr = ptr->next)
160 if (ptr->type != XML_ELEMENT_NODE)
162 if (!strcmp((const char *) ptr->name, "message"))
163 m_msg_config = mp::xml::get_text(ptr);
164 else if (!strcmp((const char *) ptr->name, "filename"))
166 std::string fname = mp::xml::get_text(ptr);
169 else if (!strcmp((const char *) ptr->name, "category"))
171 const struct _xmlAttr *attr;
172 for (attr = ptr->properties; attr; attr = attr->next)
174 if (!strcmp((const char *) attr->name, "access"))
175 m_access = mp::xml::get_bool(attr->children, true);
176 else if (!strcmp((const char *) attr->name, "user-access"))
177 m_user_access = mp::xml::get_bool(attr->children, true);
178 else if (!strcmp((const char *) attr->name, "request-apdu"))
179 m_req_apdu = mp::xml::get_bool(attr->children, true);
180 else if (!strcmp((const char *) attr->name, "response-apdu"))
181 m_res_apdu = mp::xml::get_bool(attr->children, true);
182 else if (!strcmp((const char *) attr->name, "apdu"))
184 m_req_apdu = mp::xml::get_bool(attr->children, true);
185 m_res_apdu = m_req_apdu;
187 else if (!strcmp((const char *) attr->name,
190 mp::xml::get_bool(attr->children, true);
191 else if (!strcmp((const char *) attr->name,
194 mp::xml::get_bool(attr->children, true);
195 else if (!strcmp((const char *) attr->name,
199 mp::xml::get_bool(attr->children, true);
200 m_res_session = m_req_session;
202 else if (!strcmp((const char *) attr->name,
205 mp::xml::get_bool(attr->children, true);
207 throw mp::filter::FilterException(
208 "Bad attribute " + std::string((const char *)
214 throw mp::filter::FilterException("Bad element "
215 + std::string((const char *)
221 void yf::Log::Impl::process(mp::Package &package)
223 Z_GDU *gdu = package.request().get();
224 std::string user("-");
226 // getting timestamp for receiving of package
227 boost::posix_time::ptime receive_time
228 = boost::posix_time::microsec_clock::local_time();
230 // scope for session lock
232 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
234 if (gdu && gdu->which == Z_GDU_Z3950)
236 Z_APDU *apdu_req = gdu->u.z3950;
237 if (apdu_req->which == Z_APDU_initRequest)
239 Z_InitRequest *req = apdu_req->u.initRequest;
240 Z_IdAuthentication *a = req->idAuthentication;
243 if (a->which == Z_IdAuthentication_idPass)
244 user = a->u.idPass->userId;
245 else if (a->which == Z_IdAuthentication_open)
248 m_sessions[package.session()] = user;
252 std::map<mp::Session,std::string>::iterator it =
253 m_sessions.find(package.session());
254 if (it != m_sessions.end())
257 if (package.session().is_closed())
258 m_sessions.erase(package.session());
260 // scope for locking Ostream
262 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
269 //<< receive_time << " "
270 //<< to_iso_string(receive_time) << " "
271 << to_iso_extended_string(receive_time) << " "
272 << m_msg_config << " "
274 << "000000.000000" << " "
285 //<< receive_time << " "
286 //<< to_iso_string(receive_time) << " "
287 << to_iso_extended_string(receive_time) << " "
288 << m_msg_config << " " << user << " "
290 << "000000.000000" << " "
298 m_file->out << receive_time << " " << m_msg_config;
299 m_file->out << " request id=" << package.session().id();
300 m_file->out << " close="
301 << (package.session().is_closed() ? "yes" : "no")
307 if (gdu && gdu->which == Z_GDU_Z3950 &&
308 gdu->u.z3950->which == Z_APDU_initRequest)
310 m_file->out << receive_time << " " << m_msg_config;
311 m_file->out << " init options:";
312 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
313 option_write, m_file.get());
322 mp::odr odr(ODR_PRINT);
323 odr_set_stream(odr, m_file.get(), stream_write, 0);
324 z_GDU(odr, &gdu, 0, 0);
330 // unlocked during move
333 gdu = package.response().get();
335 // getting timestamp for sending of package
336 boost::posix_time::ptime send_time
337 = boost::posix_time::microsec_clock::local_time();
339 boost::posix_time::time_duration duration = send_time - receive_time;
341 // scope for locking Ostream
343 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
350 //<< send_time << " "
351 //<< to_iso_string(send_time) << " "
352 << to_iso_extended_string(send_time) << " "
353 << m_msg_config << " "
355 << to_iso_string(duration) << " "
365 //<< send_time << " "
366 //<< to_iso_string(send_time) << " "
367 << to_iso_extended_string(send_time) << " "
368 << m_msg_config << " " << user << " "
370 << to_iso_string(duration) << " "
378 m_file->out << send_time << " " << m_msg_config;
379 m_file->out << " response id=" << package.session().id();
380 m_file->out << " close="
381 << (package.session().is_closed() ? "yes " : "no ")
382 << "duration=" << duration
388 if (gdu && gdu->which == Z_GDU_Z3950 &&
389 gdu->u.z3950->which == Z_APDU_initResponse)
391 m_file->out << receive_time << " " << m_msg_config;
392 m_file->out << " init options:";
393 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
394 option_write, m_file.get());
403 mp::odr odr(ODR_PRINT);
404 odr_set_stream(odr, m_file.get(), stream_write, 0);
405 z_GDU(odr, &gdu, 0, 0);
413 void yf::Log::Impl::openfile(const std::string &fname)
415 std::list<LFilePtr>::const_iterator it
416 = filter_log_files.begin();
417 for (; it != filter_log_files.end(); it++)
419 if ((*it)->m_fname == fname)
425 // open stdout for empty file
426 LFilePtr newfile(fname.length() == 0
427 ? new LFile(fname, std::cout)
429 filter_log_files.push_back(newfile);
434 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
436 yf::Log::Impl::LFile *lfile = (yf::Log::Impl::LFile*) handle;
437 lfile->out.write(buf, len);
440 void yf::Log::Impl::option_write(const char *name, void *handle)
442 yf::Log::Impl::LFile *lfile = (yf::Log::Impl::LFile*) handle;
443 lfile->out << " " << name;
447 yf::Log::Impl::LFile::LFile(std::string fname) :
448 m_fname(fname), fout(fname.c_str(),std::ios_base::app), out(fout)
452 yf::Log::Impl::LFile::LFile(std::string fname, std::ostream &use_this) :
453 m_fname(fname), out(use_this)
460 static mp::filter::Base* filter_creator()
462 return new mp::filter::Log;
466 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
476 * indent-tabs-mode: nil
477 * c-file-style: "stroustrup"
479 * vim: shiftwidth=4 tabstop=8 expandtab