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,
115 m_p->configure(xmlnode);
118 void yf::Log::process(mp::Package &package) const
120 m_p->process(package);
124 // static initialization
125 std::list<yf::Log::Impl::LFilePtr> yf::Log::Impl::filter_log_files;
128 yf::Log::Impl::Impl(const std::string &x)
131 m_user_access(false),
134 m_req_session(false),
135 m_res_session(false),
136 m_init_options(false),
137 m_time_format("%H:%M:%S-%d/%m")
143 yf::Log::Impl::~Impl()
148 void yf::Log::Impl::configure(const xmlNode *ptr)
150 for (ptr = ptr->children; ptr; ptr = ptr->next)
152 if (ptr->type != XML_ELEMENT_NODE)
154 if (!strcmp((const char *) ptr->name, "message"))
155 m_msg_config = mp::xml::get_text(ptr);
156 else if (!strcmp((const char *) ptr->name, "filename"))
158 std::string fname = mp::xml::get_text(ptr);
161 else if (!strcmp((const char *) ptr->name, "time-format"))
163 m_time_format = 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, "access"))
171 m_access = mp::xml::get_bool(attr->children, true);
172 else if (!strcmp((const char *) attr->name, "user-access"))
173 m_user_access = 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);
202 else if (!strcmp((const char *) attr->name,
205 mp::xml::get_bool(attr->children, true);
207 throw mp::filter::FilterException(
208 "Bad attribute " + std::string((const char *)
214 throw mp::filter::FilterException("Bad element "
215 + std::string((const char *)
221 void yf::Log::Impl::process(mp::Package &package)
223 Z_GDU *gdu = package.request().get();
224 std::string user("-");
226 yaz_timing_t timer = yaz_timing_create();
228 // scope for session lock
230 boost::mutex::scoped_lock scoped_lock(m_session_mutex);
232 if (gdu && gdu->which == Z_GDU_Z3950)
234 Z_APDU *apdu_req = gdu->u.z3950;
235 if (apdu_req->which == Z_APDU_initRequest)
237 Z_InitRequest *req = apdu_req->u.initRequest;
238 Z_IdAuthentication *a = req->idAuthentication;
241 if (a->which == Z_IdAuthentication_idPass)
242 user = a->u.idPass->userId;
243 else if (a->which == Z_IdAuthentication_open)
246 m_sessions[package.session()] = user;
250 std::map<mp::Session,std::string>::iterator it =
251 m_sessions.find(package.session());
252 if (it != m_sessions.end())
255 if (package.session().is_closed())
256 m_sessions.erase(package.session());
258 // scope for locking Ostream
260 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
266 std::ostringstream os;
267 os << m_msg_config << " "
271 m_file->log(m_time_format, os);
279 std::ostringstream os;
280 os << m_msg_config << " " << user << " "
284 m_file->log(m_time_format, os);
290 std::ostringstream os;
292 os << " request id=" << package.session().id();
294 << (package.session().is_closed() ? "yes" : "no");
295 m_file->log(m_time_format, os);
300 if (gdu && gdu->which == Z_GDU_Z3950 &&
301 gdu->u.z3950->which == Z_APDU_initRequest)
303 std::ostringstream os;
304 os << m_msg_config << " init options:";
305 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
307 m_file->log(m_time_format, os);
315 mp::odr odr(ODR_PRINT);
316 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
317 z_GDU(odr, &gdu, 0, 0);
322 // unlocked during move
325 gdu = package.response().get();
327 yaz_timing_stop(timer);
328 double duration = yaz_timing_get_real(timer);
330 // scope for locking Ostream
332 boost::mutex::scoped_lock scoped_lock(m_file->m_mutex);
338 std::ostringstream os;
339 os << m_msg_config << " "
341 << std::fixed << std::setprecision (6) << duration
344 m_file->log(m_time_format, os);
351 std::ostringstream os;
352 os << m_msg_config << " " << user << " "
354 << std::fixed << std::setprecision (6) << duration << " "
356 m_file->log(m_time_format, os);
362 std::ostringstream os;
364 os << " response id=" << package.session().id();
366 << (package.session().is_closed() ? "yes " : "no ")
368 << std::fixed << std::setprecision (6) << duration;
369 m_file->log(m_time_format, os);
374 if (gdu && gdu->which == Z_GDU_Z3950 &&
375 gdu->u.z3950->which == Z_APDU_initResponse)
377 std::ostringstream os;
379 os << " init options:";
380 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
382 m_file->log(m_time_format, os);
390 mp::odr odr(ODR_PRINT);
391 odr_set_stream(odr, m_file->fhandle, stream_write, 0);
392 z_GDU(odr, &gdu, 0, 0);
397 yaz_timing_destroy(&timer);
401 void yf::Log::Impl::openfile(const std::string &fname)
403 std::list<LFilePtr>::const_iterator it
404 = filter_log_files.begin();
405 for (; it != filter_log_files.end(); it++)
407 if ((*it)->m_fname == fname)
413 LFilePtr newfile(new LFile(fname));
414 filter_log_files.push_back(newfile);
419 void yf::Log::Impl::stream_write(ODR o, void *handle, int type, const char *buf, int len)
421 FILE *f = (FILE*) handle;
422 fwrite(buf, len, 1, f ? f : yaz_log_file());
425 void yf::Log::Impl::option_write(const char *name, void *handle)
427 std::ostringstream *os = (std::ostringstream *) handle;
432 yf::Log::Impl::LFile::LFile(std::string fname) :
437 fhandle = fopen(fname.c_str(), "a");
442 yf::Log::Impl::LFile::~LFile()
446 void yf::Log::Impl::LFile::log(const std::string &date_format,
447 std::ostringstream &os)
454 struct tm tm0, *tm = &tm0;
455 localtime_r(&ti, tm);
457 struct tm *tm = localtime(&ti);
459 if (strftime(datestr, sizeof(datestr)-1, date_format.c_str(), tm))
461 fputs(datestr, fhandle);
464 fputs(os.str().c_str(), fhandle);
465 fputc('\n', fhandle);
468 yaz_log(YLOG_LOG, "%s", os.str().c_str());
471 void yf::Log::Impl::LFile::flush()
477 static mp::filter::Base* filter_creator()
479 return new mp::filter::Log;
483 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
493 * c-file-style: "Stroustrup"
494 * indent-tabs-mode: nil
496 * vim: shiftwidth=4 tabstop=8 expandtab