1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "filter_log.hpp"
21 #include <metaproxy/package.hpp>
26 #include <boost/thread/mutex.hpp>
28 #include "gduutil.hpp"
29 #include <metaproxy/util.hpp>
30 #include <metaproxy/xmlutil.hpp>
33 #include <yaz/wrbuf.h>
35 #include <yaz/querytowrbuf.h>
36 #include <yaz/timing.h>
40 namespace mp = metaproxy_1;
41 namespace yf = metaproxy_1::filter;
43 namespace metaproxy_1 {
48 typedef boost::shared_ptr<Log::Impl::LFile> LFilePtr;
51 Impl(const std::string &x = "-");
53 void process(metaproxy_1::Package & package);
54 void configure(const xmlNode * ptr);
56 void openfile(const std::string &fname);
57 // needs to be static to be called by C pointer-to-function-syntax
58 static void stream_write(ODR o, void *handle, int type,
59 const char *buf, int len);
60 // needs to be static to be called by C pointer-to-function-syntax
61 static void option_write(const char *name, void *handle);
63 std::string m_msg_config;
72 std::string m_time_format;
73 // Only used during confiqgure 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, FILE *outf);
90 void log(const std::string &date_format,
91 std::ostringstream &os);
98 // define Pimpl wrapper forwarding to Impl
100 yf::Log::Log() : m_p(new Impl)
104 yf::Log::Log(const std::string &x) : m_p(new Impl(x))
109 { // must have a destructor because of boost::scoped_ptr
112 void yf::Log::configure(const xmlNode *xmlnode, bool test_only)
114 m_p->configure(xmlnode);
117 void yf::Log::process(mp::Package &package) const
119 m_p->process(package);
123 // static initialization
124 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
127 yf::Log::Impl::Impl(const std::string &x)
130 m_user_access(false),
133 m_req_session(false),
134 m_res_session(false),
135 m_init_options(false),
136 m_time_format("%H:%M:%S-%d/%m")
142 yf::Log::Impl::~Impl()
147 void yf::Log::Impl::configure(const xmlNode *ptr)
149 for (ptr = ptr->children; ptr; ptr = ptr->next)
151 if (ptr->type != XML_ELEMENT_NODE)
153 if (!strcmp((const char *) ptr->name, "message"))
154 m_msg_config = mp::xml::get_text(ptr);
155 else if (!strcmp((const char *) ptr->name, "filename"))
157 std::string fname = mp::xml::get_text(ptr);
160 else if (!strcmp((const char *) ptr->name, "time-format"))
162 m_time_format = mp::xml::get_text(ptr);
164 else if (!strcmp((const char *) ptr->name, "category"))
166 const struct _xmlAttr *attr;
167 for (attr = ptr->properties; attr; attr = attr->next)
169 if (!strcmp((const char *) attr->name, "access"))
170 m_access = mp::xml::get_bool(attr->children, true);
171 else if (!strcmp((const char *) attr->name, "user-access"))
172 m_user_access = mp::xml::get_bool(attr->children, true);
173 else if (!strcmp((const char *) attr->name, "request-apdu"))
174 m_req_apdu = mp::xml::get_bool(attr->children, true);
175 else if (!strcmp((const char *) attr->name, "response-apdu"))
176 m_res_apdu = mp::xml::get_bool(attr->children, true);
177 else if (!strcmp((const char *) attr->name, "apdu"))
179 m_req_apdu = mp::xml::get_bool(attr->children, true);
180 m_res_apdu = m_req_apdu;
182 else if (!strcmp((const char *) attr->name,
185 mp::xml::get_bool(attr->children, true);
186 else if (!strcmp((const char *) attr->name,
189 mp::xml::get_bool(attr->children, true);
190 else if (!strcmp((const char *) attr->name,
194 mp::xml::get_bool(attr->children, true);
195 m_res_session = m_req_session;
197 else if (!strcmp((const char *) attr->name,
200 mp::xml::get_bool(attr->children, true);
201 else if (!strcmp((const char *) attr->name,
204 mp::xml::get_bool(attr->children, true);
206 throw mp::filter::FilterException(
207 "Bad attribute " + std::string((const char *)
213 throw mp::filter::FilterException("Bad element "
214 + std::string((const char *)
220 void yf::Log::Impl::process(mp::Package &package)
222 Z_GDU *gdu = package.request().get();
223 std::string user("-");
225 yaz_timing_t timer = yaz_timing_create();
227 // scope for session lock
229 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
231 if (gdu && gdu->which == Z_GDU_Z3950)
233 Z_APDU *apdu_req = gdu->u.z3950;
234 if (apdu_req->which == Z_APDU_initRequest)
236 Z_InitRequest *req = apdu_req->u.initRequest;
237 Z_IdAuthentication *a = req->idAuthentication;
240 if (a->which == Z_IdAuthentication_idPass)
241 user = a->u.idPass->userId;
242 else if (a->which == Z_IdAuthentication_open)
245 m_sessions[package.session()] = user;
249 std::map<mp::Session,std::string>::iterator it =
250 m_sessions.find(package.session());
251 if (it != m_sessions.end())
254 if (package.session().is_closed())
255 m_sessions.erase(package.session());
257 // scope for locking Ostream
259 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
265 std::ostringstream os;
266 os << m_msg_config << " "
270 m_file->log(m_time_format, os);
278 std::ostringstream os;
279 os << m_msg_config << " " << user << " "
283 m_file->log(m_time_format, os);
289 std::ostringstream os;
291 os << " request id=" << package.session().id();
293 << (package.session().is_closed() ? "yes" : "no");
294 m_file->log(m_time_format, os);
299 if (gdu && gdu->which == Z_GDU_Z3950 &&
300 gdu->u.z3950->which == Z_APDU_initRequest)
302 std::ostringstream os;
303 os << m_msg_config << " init options:";
304 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
306 m_file->log(m_time_format, os);
314 mp::odr odr(ODR_PRINT);
315 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
316 z_GDU(odr, &gdu, 0, 0);
321 // unlocked during move
324 gdu = package.response().get();
326 yaz_timing_stop(timer);
327 double duration = yaz_timing_get_real(timer);
329 // scope for locking Ostream
331 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
337 std::ostringstream os;
338 os << m_msg_config << " "
340 << std::fixed << std::setprecision (6) << duration
343 m_file->log(m_time_format, os);
350 std::ostringstream os;
351 os << m_msg_config << " " << user << " "
353 << std::fixed << std::setprecision (6) << duration << " "
355 m_file->log(m_time_format, os);
361 std::ostringstream os;
363 os << " response id=" << package.session().id();
365 << (package.session().is_closed() ? "yes " : "no ")
367 << std::fixed << std::setprecision (6) << duration;
368 m_file->log(m_time_format, os);
373 if (gdu && gdu->which == Z_GDU_Z3950 &&
374 gdu->u.z3950->which == Z_APDU_initResponse)
376 std::ostringstream os;
378 os << " init options:";
379 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
381 m_file->log(m_time_format, os);
389 mp::odr odr(ODR_PRINT);
390 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
391 z_GDU(odr, &gdu, 0, 0);
396 yaz_timing_destroy(&timer);
400 void yf::Log::Impl::openfile(const std::string &fname)
402 std::list<LFilePtr>::const_iterator it
403 = filter_log_files.begin();
404 for (; it != filter_log_files.end(); it++)
406 if ((*it)->m_fname == fname)
412 LFilePtr newfile(new LFile(fname));
413 filter_log_files.push_back(newfile);
418 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
420 FILE *f = (FILE*) handle;
421 fwrite(buf, len, 1, f ? f : yaz_log_file());
424 void yf::Log::Impl::option_write(const char *name, void *handle)
426 std::ostringstream *os = (std::ostringstream *) handle;
431 yf::Log::Impl::LFile::LFile(std::string fname) :
436 fhandle = fopen(fname.c_str(), "a");
441 yf::Log::Impl::LFile::~LFile()
445 void yf::Log::Impl::LFile::log(const std::string &date_format,
446 std::ostringstream &os)
453 struct tm tm0, *tm = &tm0;
454 localtime_r(&ti, tm);
456 struct tm *tm = localtime(&ti);
458 if (strftime(datestr, sizeof(datestr)-1, date_format.c_str(), tm))
460 fputs(datestr, fhandle);
463 fputs(os.str().c_str(), fhandle);
464 fputc('\n', fhandle);
467 yaz_log(YLOG_LOG, "%s", os.str().c_str());
470 void yf::Log::Impl::LFile::flush()
476 static mp::filter::Base* filter_creator()
478 return new mp::filter::Log;
482 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
492 * c-file-style: "Stroustrup"
493 * indent-tabs-mode: nil
495 * vim: shiftwidth=4 tabstop=8 expandtab