1 /* $Id: filter_log.cpp,v 1.35 2008-02-27 11:08:49 adam Exp $
2 Copyright (c) 2005-2008, 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"
29 #include <boost/thread/mutex.hpp>
31 #include "gduutil.hpp"
33 #include "xmlutil.hpp"
36 #include <yaz/wrbuf.h>
38 #include <yaz/querytowrbuf.h>
39 #include <yaz/timing.h>
43 namespace mp = metaproxy_1;
44 namespace yf = metaproxy_1::filter;
46 namespace metaproxy_1 {
51 typedef boost::shared_ptr<Log::Impl::LFile> LFilePtr;
54 Impl(const std::string &x = "-");
56 void process(metaproxy_1::Package & package);
57 void configure(const xmlNode * ptr);
59 void openfile(const std::string &fname);
60 // needs to be static to be called by C pointer-to-function-syntax
61 static void stream_write(ODR o, void *handle, int type,
62 const char *buf, int len);
63 // needs to be static to be called by C pointer-to-function-syntax
64 static void option_write(const char *name, void *handle);
66 std::string m_msg_config;
75 std::string m_time_format;
76 // Only used during confiqgure stage (no threading),
77 // for performance avoid opening files which other log filter
78 // instances already have opened
79 static std::list<LFilePtr> filter_log_files;
81 boost::mutex m_session_mutex;
82 std::map<mp::Session, std::string> m_sessions;
85 class Log::Impl::LFile {
91 LFile(std::string fname);
92 LFile(std::string fname, FILE *outf);
93 void log(const std::string &date_format,
94 std::ostringstream &os);
101 // define Pimpl wrapper forwarding to Impl
103 yf::Log::Log() : m_p(new Impl)
107 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
112 { // must have a destructor because of boost::scoped_ptr
115 void yf::Log::configure(const xmlNode *xmlnode, bool test_only)
117 m_p->configure(xmlnode);
120 void yf::Log::process(mp::Package &package) const
122 m_p->process(package);
126 // static initialization
127 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
130 yf::Log::Impl::Impl(const std::string &x)
133 m_user_access(false),
136 m_req_session(false),
137 m_res_session(false),
138 m_init_options(false),
139 m_time_format("%H:%M:%S-%d/%m")
145 yf::Log::Impl::~Impl()
150 void yf::Log::Impl::configure(const xmlNode *ptr)
152 for (ptr = ptr->children; ptr; ptr = ptr->next)
154 if (ptr->type != XML_ELEMENT_NODE)
156 if (!strcmp((const char *) ptr->name, "message"))
157 m_msg_config = mp::xml::get_text(ptr);
158 else if (!strcmp((const char *) ptr->name, "filename"))
160 std::string fname = mp::xml::get_text(ptr);
163 else if (!strcmp((const char *) ptr->name, "time-format"))
165 m_time_format = mp::xml::get_text(ptr);
167 else if (!strcmp((const char *) ptr->name, "category"))
169 const struct _xmlAttr *attr;
170 for (attr = ptr->properties; attr; attr = attr->next)
172 if (!strcmp((const char *) attr->name, "access"))
173 m_access = mp::xml::get_bool(attr->children, true);
174 else if (!strcmp((const char *) attr->name, "user-access"))
175 m_user_access = mp::xml::get_bool(attr->children, true);
176 else if (!strcmp((const char *) attr->name, "request-apdu"))
177 m_req_apdu = mp::xml::get_bool(attr->children, true);
178 else if (!strcmp((const char *) attr->name, "response-apdu"))
179 m_res_apdu = mp::xml::get_bool(attr->children, true);
180 else if (!strcmp((const char *) attr->name, "apdu"))
182 m_req_apdu = mp::xml::get_bool(attr->children, true);
183 m_res_apdu = m_req_apdu;
185 else if (!strcmp((const char *) attr->name,
188 mp::xml::get_bool(attr->children, true);
189 else if (!strcmp((const char *) attr->name,
192 mp::xml::get_bool(attr->children, true);
193 else if (!strcmp((const char *) attr->name,
197 mp::xml::get_bool(attr->children, true);
198 m_res_session = m_req_session;
200 else if (!strcmp((const char *) attr->name,
203 mp::xml::get_bool(attr->children, true);
204 else if (!strcmp((const char *) attr->name,
207 mp::xml::get_bool(attr->children, true);
209 throw mp::filter::FilterException(
210 "Bad attribute " + std::string((const char *)
216 throw mp::filter::FilterException("Bad element "
217 + std::string((const char *)
223 void yf::Log::Impl::process(mp::Package &package)
225 Z_GDU *gdu = package.request().get();
226 std::string user("-");
228 yaz_timing_t timer = yaz_timing_create();
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);
268 std::ostringstream os;
269 os << m_msg_config << " "
273 m_file->log(m_time_format, os);
281 std::ostringstream os;
282 os << m_msg_config << " " << user << " "
286 m_file->log(m_time_format, os);
292 std::ostringstream os;
294 os << " request id=" << package.session().id();
296 << (package.session().is_closed() ? "yes" : "no");
297 m_file->log(m_time_format, os);
302 if (gdu && gdu->which == Z_GDU_Z3950 &&
303 gdu->u.z3950->which == Z_APDU_initRequest)
305 std::ostringstream os;
306 os << m_msg_config << " init options:";
307 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
309 m_file->log(m_time_format, os);
317 mp::odr odr(ODR_PRINT);
318 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
319 z_GDU(odr, &gdu, 0, 0);
324 // unlocked during move
327 gdu = package.response().get();
329 yaz_timing_stop(timer);
330 double duration = yaz_timing_get_real(timer);
332 // scope for locking Ostream
334 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
340 std::ostringstream os;
341 os << m_msg_config << " "
343 << std::fixed << std::setprecision (6) << duration
346 m_file->log(m_time_format, os);
353 std::ostringstream os;
354 os << m_msg_config << " " << user << " "
356 << std::fixed << std::setprecision (6) << duration << " "
358 m_file->log(m_time_format, os);
364 std::ostringstream os;
366 os << " response id=" << package.session().id();
368 << (package.session().is_closed() ? "yes " : "no ")
370 << std::fixed << std::setprecision (6) << duration;
371 m_file->log(m_time_format, os);
376 if (gdu && gdu->which == Z_GDU_Z3950 &&
377 gdu->u.z3950->which == Z_APDU_initResponse)
379 std::ostringstream os;
381 os << " init options:";
382 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
384 m_file->log(m_time_format, os);
392 mp::odr odr(ODR_PRINT);
393 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
394 z_GDU(odr, &gdu, 0, 0);
399 yaz_timing_destroy(&timer);
403 void yf::Log::Impl::openfile(const std::string &fname)
405 std::list<LFilePtr>::const_iterator it
406 = filter_log_files.begin();
407 for (; it != filter_log_files.end(); it++)
409 if ((*it)->m_fname == fname)
415 LFilePtr newfile(new LFile(fname));
416 filter_log_files.push_back(newfile);
421 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
423 FILE *f = (FILE*) handle;
424 fwrite(buf, len, 1, f ? f : yaz_log_file());
427 void yf::Log::Impl::option_write(const char *name, void *handle)
429 std::ostringstream *os = (std::ostringstream *) handle;
434 yf::Log::Impl::LFile::LFile(std::string fname) :
439 fhandle = fopen(fname.c_str(), "a");
444 yf::Log::Impl::LFile::~LFile()
448 void yf::Log::Impl::LFile::log(const std::string &date_format,
449 std::ostringstream &os)
456 struct tm tm0, *tm = &tm0;
457 localtime_r(&ti, tm);
459 struct tm *tm = localtime(&ti);
461 if (strftime(datestr, sizeof(datestr)-1, date_format.c_str(), tm))
463 fputs(datestr, fhandle);
466 fputs(os.str().c_str(), fhandle);
467 fputc('\n', fhandle);
470 yaz_log(YLOG_LOG, "%s", os.str().c_str());
473 void yf::Log::Impl::LFile::flush()
479 static mp::filter::Base* filter_creator()
481 return new mp::filter::Log;
485 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
495 * indent-tabs-mode: nil
496 * c-file-style: "stroustrup"
498 * vim: shiftwidth=4 tabstop=8 expandtab