1 /* $Id: filter_log.cpp,v 1.27 2006-09-29 12:24:49 marc Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
7 #include "filter_log.hpp"
13 #include <boost/thread/mutex.hpp>
14 #include <boost/date_time/posix_time/posix_time.hpp>
16 #include "gduutil.hpp"
18 #include "xmlutil.hpp"
22 #include <yaz/wrbuf.h>
23 #include <yaz/querytowrbuf.h>
26 namespace mp = metaproxy_1;
27 namespace yf = metaproxy_1::filter;
29 namespace metaproxy_1 {
35 typedef boost::shared_ptr<Log::Impl::LFile> LFilePtr;
38 Impl(const std::string &x = "");
40 void process(metaproxy_1::Package & package) const;
41 void configure(const xmlNode * ptr);
43 void openfile(const std::string &fname);
44 // needs to be static to be called by C pointer-to-function-syntax
45 static void stream_write(ODR o, void *handle, int type,
46 const char *buf, int len);
47 // needs to be static to be called by C pointer-to-function-syntax
48 static void option_write(const char *name, void *handle);
50 std::string m_msg_config;
58 // Only used during configure stage (no threading),
59 // for performance avoid opening files which other log filter
60 // instances already have opened
61 static std::list<LFilePtr> filter_log_files;
64 class Log::Impl::LFile {
70 LFile(std::string fname);
71 LFile(std::string fname, std::ostream &use_this);
77 // define Pimpl wrapper forwarding to Impl
79 yf::Log::Log() : m_p(new Impl)
83 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
88 { // must have a destructor because of boost::scoped_ptr
91 void yf::Log::configure(const xmlNode *xmlnode)
93 m_p->configure(xmlnode);
96 void yf::Log::process(mp::Package &package) const
98 m_p->process(package);
102 // define Implementation stuff
104 // static initialization
105 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
108 // yf::Log::Impl::Impl()
111 // m_req_apdu = false;
112 // m_res_apdu = false;
113 // m_req_session = false;
114 // m_res_session = false;
115 // m_init_options = false;
119 yf::Log::Impl::Impl(const std::string &x)
124 m_req_session(false),
125 m_res_session(false),
126 m_init_options(false)
132 yf::Log::Impl::~Impl()
137 void yf::Log::Impl::configure(const xmlNode *ptr)
139 for (ptr = ptr->children; ptr; ptr = ptr->next)
141 if (ptr->type != XML_ELEMENT_NODE)
143 if (!strcmp((const char *) ptr->name, "message"))
144 m_msg_config = mp::xml::get_text(ptr);
145 else if (!strcmp((const char *) ptr->name, "filename"))
147 std::string fname = mp::xml::get_text(ptr);
150 else if (!strcmp((const char *) ptr->name, "category"))
152 const struct _xmlAttr *attr;
153 for (attr = ptr->properties; attr; attr = attr->next)
155 if (!strcmp((const char *) attr->name,
158 mp::xml::get_bool(attr->children, true);
159 else if (!strcmp((const char *) attr->name, "request-apdu"))
160 m_req_apdu = mp::xml::get_bool(attr->children, true);
161 else if (!strcmp((const char *) attr->name, "response-apdu"))
162 m_res_apdu = mp::xml::get_bool(attr->children, true);
163 else if (!strcmp((const char *) attr->name, "apdu"))
165 m_req_apdu = mp::xml::get_bool(attr->children, true);
166 m_res_apdu = m_req_apdu;
168 else if (!strcmp((const char *) attr->name,
171 mp::xml::get_bool(attr->children, true);
172 else if (!strcmp((const char *) attr->name,
175 mp::xml::get_bool(attr->children, true);
176 else if (!strcmp((const char *) attr->name,
180 mp::xml::get_bool(attr->children, true);
181 m_res_session = m_req_session;
183 else if (!strcmp((const char *) attr->name,
186 mp::xml::get_bool(attr->children, true);
188 throw mp::filter::FilterException(
189 "Bad attribute " + std::string((const char *)
195 throw mp::filter::FilterException("Bad element "
196 + std::string((const char *)
202 void yf::Log::Impl::process(mp::Package &package) const
206 // getting timestamp for receiving of package
207 boost::posix_time::ptime receive_time
208 = boost::posix_time::microsec_clock::local_time();
212 // scope for locking Ostream
214 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
219 gdu = package.request().get();
223 //<< receive_time << " "
224 //<< to_iso_string(receive_time) << " "
225 << to_iso_extended_string(receive_time) << " "
226 << m_msg_config << " "
228 << "000000.000000" << " "
236 m_file->out << receive_time << " " << m_msg_config;
237 m_file->out << " request id=" << package.session().id();
238 m_file->out << " close="
239 << (package.session().is_closed() ? "yes" : "no")
245 gdu = package.request().get();
246 if (gdu && gdu->which == Z_GDU_Z3950 &&
247 gdu->u.z3950->which == Z_APDU_initRequest)
249 m_file->out << receive_time << " " << m_msg_config;
250 m_file->out << " init options:";
251 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
252 option_write, m_file.get());
259 gdu = package.request().get();
262 mp::odr odr(ODR_PRINT);
263 odr_set_stream(odr, m_file.get(), stream_write, 0);
264 z_GDU(odr, &gdu, 0, 0);
270 // unlocked during move
273 // getting timestamp for sending of package
274 boost::posix_time::ptime send_time
275 = boost::posix_time::microsec_clock::local_time();
277 boost::posix_time::time_duration duration = send_time - receive_time;
279 // scope for locking Ostream
281 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
285 gdu = package.response().get();
289 //<< send_time << " "
290 //<< to_iso_string(send_time) << " "
291 << to_iso_extended_string(send_time) << " "
292 << m_msg_config << " "
294 << to_iso_string(duration) << " "
302 m_file->out << send_time << " " << m_msg_config;
303 m_file->out << " response id=" << package.session().id();
304 m_file->out << " close="
305 << (package.session().is_closed() ? "yes " : "no ")
306 << "duration=" << duration
312 gdu = package.response().get();
313 if (gdu && gdu->which == Z_GDU_Z3950 &&
314 gdu->u.z3950->which == Z_APDU_initResponse)
316 m_file->out << receive_time << " " << m_msg_config;
317 m_file->out << " init options:";
318 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
319 option_write, m_file.get());
326 gdu = package.response().get();
329 mp::odr odr(ODR_PRINT);
330 odr_set_stream(odr, m_file.get(), stream_write, 0);
331 z_GDU(odr, &gdu, 0, 0);
340 void yf::Log::Impl::openfile(const std::string &fname)
342 std::list<LFilePtr>::const_iterator it
343 = filter_log_files.begin();
344 for (; it != filter_log_files.end(); it++)
346 if ((*it)->m_fname == fname)
352 // open stdout for empty file
353 LFilePtr newfile(fname.length() == 0
354 ? new LFile(fname, std::cout)
356 filter_log_files.push_back(newfile);
361 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
363 yf::Log::Impl::LFile *lfile = (yf::Log::Impl::LFile*) handle;
364 lfile->out.write(buf, len);
367 void yf::Log::Impl::option_write(const char *name, void *handle)
369 yf::Log::Impl::LFile *lfile = (yf::Log::Impl::LFile*) handle;
370 lfile->out << " " << name;
374 yf::Log::Impl::LFile::LFile(std::string fname) :
375 m_fname(fname), fout(fname.c_str()), out(fout)
379 yf::Log::Impl::LFile::LFile(std::string fname, std::ostream &use_this) :
380 m_fname(fname), out(use_this)
387 static mp::filter::Base* filter_creator()
389 return new mp::filter::Log;
393 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
403 * indent-tabs-mode: nil
404 * c-file-style: "stroustrup"
406 * vim: shiftwidth=4 tabstop=8 expandtab