1 /* $Id: filter_log.cpp,v 1.34 2008-02-26 23:56:28 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"
36 #include <yaz/wrbuf.h>
38 #include <yaz/querytowrbuf.h>
42 namespace mp = metaproxy_1;
43 namespace yf = metaproxy_1::filter;
45 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);
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;
74 // Only used during configure stage (no threading),
75 // for performance avoid opening files which other log filter
76 // instances already have opened
77 static std::list<LFilePtr> filter_log_files;
79 boost::mutex m_session_mutex;
80 std::map<mp::Session, std::string> m_sessions;
83 class Log::Impl::LFile {
89 LFile(std::string fname);
90 LFile(std::string fname, FILE *outf);
96 // define Pimpl wrapper forwarding to Impl
98 yf::Log::Log() : m_p(new Impl)
102 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
107 { // must have a destructor because of boost::scoped_ptr
110 void yf::Log::configure(const xmlNode *xmlnode, bool test_only)
112 m_p->configure(xmlnode);
115 void yf::Log::process(mp::Package &package) const
117 m_p->process(package);
121 // define Implementation stuff
123 // static initialization
124 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
127 // yf::Log::Impl::Impl()
130 // m_req_apdu = false;
131 // m_res_apdu = false;
132 // m_req_session = false;
133 // m_res_session = false;
134 // m_init_options = false;
138 yf::Log::Impl::Impl(const std::string &x)
141 m_user_access(false),
144 m_req_session(false),
145 m_res_session(false),
146 m_init_options(false)
152 yf::Log::Impl::~Impl()
157 void yf::Log::Impl::configure(const xmlNode *ptr)
159 for (ptr = ptr->children; ptr; ptr = ptr->next)
161 if (ptr->type != XML_ELEMENT_NODE)
163 if (!strcmp((const char *) ptr->name, "message"))
164 m_msg_config = mp::xml::get_text(ptr);
165 else if (!strcmp((const char *) ptr->name, "filename"))
167 std::string fname = mp::xml::get_text(ptr);
170 else if (!strcmp((const char *) ptr->name, "category"))
172 const struct _xmlAttr *attr;
173 for (attr = ptr->properties; attr; attr = attr->next)
175 if (!strcmp((const char *) attr->name, "access"))
176 m_access = mp::xml::get_bool(attr->children, true);
177 else if (!strcmp((const char *) attr->name, "user-access"))
178 m_user_access = mp::xml::get_bool(attr->children, true);
179 else if (!strcmp((const char *) attr->name, "request-apdu"))
180 m_req_apdu = mp::xml::get_bool(attr->children, true);
181 else if (!strcmp((const char *) attr->name, "response-apdu"))
182 m_res_apdu = mp::xml::get_bool(attr->children, true);
183 else if (!strcmp((const char *) attr->name, "apdu"))
185 m_req_apdu = mp::xml::get_bool(attr->children, true);
186 m_res_apdu = m_req_apdu;
188 else if (!strcmp((const char *) attr->name,
191 mp::xml::get_bool(attr->children, true);
192 else if (!strcmp((const char *) attr->name,
195 mp::xml::get_bool(attr->children, true);
196 else if (!strcmp((const char *) attr->name,
200 mp::xml::get_bool(attr->children, true);
201 m_res_session = m_req_session;
203 else if (!strcmp((const char *) attr->name,
206 mp::xml::get_bool(attr->children, true);
208 throw mp::filter::FilterException(
209 "Bad attribute " + std::string((const char *)
215 throw mp::filter::FilterException("Bad element "
216 + std::string((const char *)
222 void yf::Log::Impl::process(mp::Package &package)
224 Z_GDU *gdu = package.request().get();
225 std::string user("-");
227 // getting timestamp for receiving of package
228 boost::posix_time::ptime receive_time
229 = boost::posix_time::microsec_clock::local_time();
231 // scope for session lock
233 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
235 if (gdu && gdu->which == Z_GDU_Z3950)
237 Z_APDU *apdu_req = gdu->u.z3950;
238 if (apdu_req->which == Z_APDU_initRequest)
240 Z_InitRequest *req = apdu_req->u.initRequest;
241 Z_IdAuthentication *a = req->idAuthentication;
244 if (a->which == Z_IdAuthentication_idPass)
245 user = a->u.idPass->userId;
246 else if (a->which == Z_IdAuthentication_open)
249 m_sessions[package.session()] = user;
253 std::map<mp::Session,std::string>::iterator it =
254 m_sessions.find(package.session());
255 if (it != m_sessions.end())
258 if (package.session().is_closed())
259 m_sessions.erase(package.session());
261 // scope for locking Ostream
263 std::ostringstream os;
264 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
270 os << to_iso_extended_string(receive_time) << " "
271 << m_msg_config << " "
273 << "000000.000000" << " "
283 os << to_iso_extended_string(receive_time) << " "
284 << m_msg_config << " " << user << " "
286 << "000000.000000" << " "
294 os << receive_time << " " << m_msg_config;
295 os << " request id=" << package.session().id();
297 << (package.session().is_closed() ? "yes" : "no")
303 if (gdu && gdu->which == Z_GDU_Z3950 &&
304 gdu->u.z3950->which == Z_APDU_initRequest)
306 os << receive_time << " " << m_msg_config;
307 os << " init options:";
308 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
309 option_write, m_file->fhandle);
318 mp::odr odr(ODR_PRINT);
319 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
320 z_GDU(odr, &gdu, 0, 0);
323 fputs(os.str().c_str(), m_file->fhandle);
324 fflush(m_file->fhandle);
327 // unlocked during move
330 gdu = package.response().get();
332 // getting timestamp for sending of package
333 boost::posix_time::ptime send_time
334 = boost::posix_time::microsec_clock::local_time();
336 boost::posix_time::time_duration duration = send_time - receive_time;
338 // scope for locking Ostream
340 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
341 std::ostringstream os;
347 os << to_iso_extended_string(send_time) << " "
348 << m_msg_config << " "
350 << to_iso_string(duration) << " "
359 os << to_iso_extended_string(send_time) << " "
360 << m_msg_config << " " << user << " "
362 << to_iso_string(duration) << " "
370 os << send_time << " " << m_msg_config;
371 os << " response id=" << package.session().id();
373 << (package.session().is_closed() ? "yes " : "no ")
374 << "duration=" << duration
380 if (gdu && gdu->which == Z_GDU_Z3950 &&
381 gdu->u.z3950->which == Z_APDU_initResponse)
383 os << receive_time << " " << m_msg_config;
384 os << " init options:";
385 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
386 option_write, m_file->fhandle);
395 mp::odr odr(ODR_PRINT);
396 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
397 z_GDU(odr, &gdu, 0, 0);
400 fputs(os.str().c_str(), m_file->fhandle);
401 fflush(m_file->fhandle);
406 void yf::Log::Impl::openfile(const std::string &fname)
408 std::list<LFilePtr>::const_iterator it
409 = filter_log_files.begin();
410 for (; it != filter_log_files.end(); it++)
412 if ((*it)->m_fname == fname)
418 // open stdout for empty file
419 LFilePtr newfile(fname.length() == 0
420 ? new LFile(fname, yaz_log_file())
422 filter_log_files.push_back(newfile);
427 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
429 FILE *fhandle = (FILE*) handle;
430 fwrite(buf, len, 1, fhandle);
433 void yf::Log::Impl::option_write(const char *name, void *handle)
435 FILE *fhandle = (FILE*) handle;
436 fprintf(fhandle, " %s", name);
440 yf::Log::Impl::LFile::LFile(std::string fname) :
444 fhandle = fopen(fname.c_str(), "a");
447 yf::Log::Impl::LFile::LFile(std::string fname, FILE *outf) :
453 yf::Log::Impl::LFile::~LFile()
458 static mp::filter::Base* filter_creator()
460 return new mp::filter::Log;
464 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
474 * indent-tabs-mode: nil
475 * c-file-style: "stroustrup"
477 * vim: shiftwidth=4 tabstop=8 expandtab