1 /* $Id: filter_log.cpp,v 1.29 2007-05-09 21:23:09 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 {
50 typedef boost::shared_ptr<Log::Impl::LFile> LFilePtr;
53 Impl(const std::string &x = "");
55 void process(metaproxy_1::Package & package) const;
56 void configure(const xmlNode * ptr);
58 void openfile(const std::string &fname);
59 // needs to be static to be called by C pointer-to-function-syntax
60 static void stream_write(ODR o, void *handle, int type,
61 const char *buf, int len);
62 // needs to be static to be called by C pointer-to-function-syntax
63 static void option_write(const char *name, void *handle);
65 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;
79 class Log::Impl::LFile {
85 LFile(std::string fname);
86 LFile(std::string fname, std::ostream &use_this);
92 // define Pimpl wrapper forwarding to Impl
94 yf::Log::Log() : m_p(new Impl)
98 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
103 { // must have a destructor because of boost::scoped_ptr
106 void yf::Log::configure(const xmlNode *xmlnode)
108 m_p->configure(xmlnode);
111 void yf::Log::process(mp::Package &package) const
113 m_p->process(package);
117 // define Implementation stuff
119 // static initialization
120 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
123 // yf::Log::Impl::Impl()
126 // m_req_apdu = false;
127 // m_res_apdu = false;
128 // m_req_session = false;
129 // m_res_session = false;
130 // m_init_options = false;
134 yf::Log::Impl::Impl(const std::string &x)
139 m_req_session(false),
140 m_res_session(false),
141 m_init_options(false)
147 yf::Log::Impl::~Impl()
152 void yf::Log::Impl::configure(const xmlNode *ptr)
154 for (ptr = ptr->children; ptr; ptr = ptr->next)
156 if (ptr->type != XML_ELEMENT_NODE)
158 if (!strcmp((const char *) ptr->name, "message"))
159 m_msg_config = mp::xml::get_text(ptr);
160 else if (!strcmp((const char *) ptr->name, "filename"))
162 std::string fname = mp::xml::get_text(ptr);
165 else if (!strcmp((const char *) ptr->name, "category"))
167 const struct _xmlAttr *attr;
168 for (attr = ptr->properties; attr; attr = attr->next)
170 if (!strcmp((const char *) attr->name,
173 mp::xml::get_bool(attr->children, true);
174 else if (!strcmp((const char *) attr->name, "request-apdu"))
175 m_req_apdu = mp::xml::get_bool(attr->children, true);
176 else if (!strcmp((const char *) attr->name, "response-apdu"))
177 m_res_apdu = mp::xml::get_bool(attr->children, true);
178 else if (!strcmp((const char *) attr->name, "apdu"))
180 m_req_apdu = mp::xml::get_bool(attr->children, true);
181 m_res_apdu = m_req_apdu;
183 else if (!strcmp((const char *) attr->name,
186 mp::xml::get_bool(attr->children, true);
187 else if (!strcmp((const char *) attr->name,
190 mp::xml::get_bool(attr->children, true);
191 else if (!strcmp((const char *) attr->name,
195 mp::xml::get_bool(attr->children, true);
196 m_res_session = m_req_session;
198 else if (!strcmp((const char *) attr->name,
201 mp::xml::get_bool(attr->children, true);
203 throw mp::filter::FilterException(
204 "Bad attribute " + std::string((const char *)
210 throw mp::filter::FilterException("Bad element "
211 + std::string((const char *)
217 void yf::Log::Impl::process(mp::Package &package) const
221 // getting timestamp for receiving of package
222 boost::posix_time::ptime receive_time
223 = boost::posix_time::microsec_clock::local_time();
227 // scope for locking Ostream
229 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
234 gdu = package.request().get();
238 //<< receive_time << " "
239 //<< to_iso_string(receive_time) << " "
240 << to_iso_extended_string(receive_time) << " "
241 << m_msg_config << " "
243 << "000000.000000" << " "
251 m_file->out << receive_time << " " << m_msg_config;
252 m_file->out << " request id=" << package.session().id();
253 m_file->out << " close="
254 << (package.session().is_closed() ? "yes" : "no")
260 gdu = package.request().get();
261 if (gdu && gdu->which == Z_GDU_Z3950 &&
262 gdu->u.z3950->which == Z_APDU_initRequest)
264 m_file->out << receive_time << " " << m_msg_config;
265 m_file->out << " init options:";
266 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
267 option_write, m_file.get());
274 gdu = package.request().get();
277 mp::odr odr(ODR_PRINT);
278 odr_set_stream(odr, m_file.get(), stream_write, 0);
279 z_GDU(odr, &gdu, 0, 0);
285 // unlocked during move
288 // getting timestamp for sending of package
289 boost::posix_time::ptime send_time
290 = boost::posix_time::microsec_clock::local_time();
292 boost::posix_time::time_duration duration = send_time - receive_time;
294 // scope for locking Ostream
296 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
300 gdu = package.response().get();
304 //<< send_time << " "
305 //<< to_iso_string(send_time) << " "
306 << to_iso_extended_string(send_time) << " "
307 << m_msg_config << " "
309 << to_iso_string(duration) << " "
317 m_file->out << send_time << " " << m_msg_config;
318 m_file->out << " response id=" << package.session().id();
319 m_file->out << " close="
320 << (package.session().is_closed() ? "yes " : "no ")
321 << "duration=" << duration
327 gdu = package.response().get();
328 if (gdu && gdu->which == Z_GDU_Z3950 &&
329 gdu->u.z3950->which == Z_APDU_initResponse)
331 m_file->out << receive_time << " " << m_msg_config;
332 m_file->out << " init options:";
333 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
334 option_write, m_file.get());
341 gdu = package.response().get();
344 mp::odr odr(ODR_PRINT);
345 odr_set_stream(odr, m_file.get(), stream_write, 0);
346 z_GDU(odr, &gdu, 0, 0);
355 void yf::Log::Impl::openfile(const std::string &fname)
357 std::list<LFilePtr>::const_iterator it
358 = filter_log_files.begin();
359 for (; it != filter_log_files.end(); it++)
361 if ((*it)->m_fname == fname)
367 // open stdout for empty file
368 LFilePtr newfile(fname.length() == 0
369 ? new LFile(fname, std::cout)
371 filter_log_files.push_back(newfile);
376 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
378 yf::Log::Impl::LFile *lfile = (yf::Log::Impl::LFile*) handle;
379 lfile->out.write(buf, len);
382 void yf::Log::Impl::option_write(const char *name, void *handle)
384 yf::Log::Impl::LFile *lfile = (yf::Log::Impl::LFile*) handle;
385 lfile->out << " " << name;
389 yf::Log::Impl::LFile::LFile(std::string fname) :
390 m_fname(fname), fout(fname.c_str()), out(fout)
394 yf::Log::Impl::LFile::LFile(std::string fname, std::ostream &use_this) :
395 m_fname(fname), out(use_this)
402 static mp::filter::Base* filter_creator()
404 return new mp::filter::Log;
408 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
418 * indent-tabs-mode: nil
419 * c-file-style: "stroustrup"
421 * vim: shiftwidth=4 tabstop=8 expandtab