1 /* $Id: filter_log.cpp,v 1.24 2006-08-30 08:35:47 marc Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
12 #include <boost/thread/mutex.hpp>
13 #include <boost/date_time/posix_time/posix_time.hpp>
15 #include "gduutil.hpp"
17 #include "xmlutil.hpp"
18 #include "filter_log.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 {
37 LFile(std::string fname);
38 LFile(std::string fname, std::ostream &use_this);
40 typedef boost::shared_ptr<Log::LFile> LFilePtr;
44 void openfile(const std::string &fname);
45 std::string m_msg_config;
54 // Only used during configure stage (no threading)
55 static std::list<LFilePtr> filter_log_files;
65 m_init_options = false;
69 yf::Log::Log(const std::string &x) : m_p(new Rep)
71 m_p->m_msg_config = x;
74 yf::Log::Log() : m_p(new Rep)
80 void stream_write(ODR o, void *handle, int type, const char *buf, int len)
82 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
83 lfile->out.write(buf, len);
86 void option_write(const char *name, void *handle)
88 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
89 lfile->out << " " << name;
92 void yf::Log::process(mp::Package &package) const
96 // getting timestamp for receiving of package
97 boost::posix_time::ptime receive_time
98 = boost::posix_time::microsec_clock::local_time();
101 //std::ostringstream msg_request;
102 //std::ostringstream msg_request_2;
103 //std::ostringstream msg_response;
104 //std::ostringstream msg_response_2;
106 // scope for locking Ostream
108 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
113 gdu = package.request().get();
117 << m_p->m_msg_config << " "
118 << receive_time << " "
120 << "00:00:00.000000" << " "
126 if (m_p->m_req_session)
128 m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
129 m_p->m_file->out << " request id=" << package.session().id();
130 m_p->m_file->out << " close="
131 << (package.session().is_closed() ? "yes" : "no")
135 if (m_p->m_init_options)
137 gdu = package.request().get();
138 if (gdu && gdu->which == Z_GDU_Z3950 &&
139 gdu->u.z3950->which == Z_APDU_initRequest)
141 m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
142 m_p->m_file->out << " init options:";
143 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
144 option_write, m_p->m_file.get());
145 m_p->m_file->out << "\n";
151 gdu = package.request().get();
154 mp::odr odr(ODR_PRINT);
155 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
156 z_GDU(odr, &gdu, 0, 0);
159 m_p->m_file->out.flush();
162 // unlocked during move
165 // getting timestamp for sending of package
166 boost::posix_time::ptime send_time
167 = boost::posix_time::microsec_clock::local_time();
169 boost::posix_time::time_duration duration = send_time - receive_time;
171 // scope for locking Ostream
173 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
177 gdu = package.response().get();
181 << m_p->m_msg_config << " "
190 if (m_p->m_res_session)
192 m_p->m_file->out << send_time << " " << m_p->m_msg_config;
193 m_p->m_file->out << " response id=" << package.session().id();
194 m_p->m_file->out << " close="
195 << (package.session().is_closed() ? "yes " : "no ")
196 << "duration=" << duration
200 if (m_p->m_init_options)
202 gdu = package.response().get();
203 if (gdu && gdu->which == Z_GDU_Z3950 &&
204 gdu->u.z3950->which == Z_APDU_initResponse)
206 m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
207 m_p->m_file->out << " init options:";
208 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
209 option_write, m_p->m_file.get());
210 m_p->m_file->out << "\n";
216 gdu = package.response().get();
219 mp::odr odr(ODR_PRINT);
220 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
221 z_GDU(odr, &gdu, 0, 0);
225 m_p->m_file->out.flush();
229 yf::Log::LFile::LFile(std::string fname) :
230 m_fname(fname), fout(fname.c_str()), out(fout)
234 yf::Log::LFile::LFile(std::string fname, std::ostream &use_this) :
235 m_fname(fname), out(use_this)
239 void yf::Log::Rep::openfile(const std::string &fname)
241 std::list<LFilePtr>::const_iterator it = filter_log_files.begin();
242 for (; it != filter_log_files.end(); it++)
244 if ((*it)->m_fname == fname)
250 // open stdout for empty file
251 LFilePtr newfile(fname.length() == 0
252 ? new LFile(fname, std::cout)
254 filter_log_files.push_back(newfile);
258 void yf::Log::configure(const xmlNode *ptr)
260 for (ptr = ptr->children; ptr; ptr = ptr->next)
262 if (ptr->type != XML_ELEMENT_NODE)
264 if (!strcmp((const char *) ptr->name, "message"))
265 m_p->m_msg_config = mp::xml::get_text(ptr);
266 else if (!strcmp((const char *) ptr->name, "filename"))
268 std::string fname = mp::xml::get_text(ptr);
269 m_p->openfile(fname);
271 else if (!strcmp((const char *) ptr->name, "category"))
273 const struct _xmlAttr *attr;
274 for (attr = ptr->properties; attr; attr = attr->next)
276 if (!strcmp((const char *) attr->name,
279 mp::xml::get_bool(attr->children, true);
280 else if (!strcmp((const char *) attr->name, "request-apdu"))
281 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
282 else if (!strcmp((const char *) attr->name, "response-apdu"))
283 m_p->m_res_apdu = mp::xml::get_bool(attr->children, true);
284 else if (!strcmp((const char *) attr->name, "apdu"))
286 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
287 m_p->m_res_apdu = m_p->m_req_apdu;
289 else if (!strcmp((const char *) attr->name,
292 mp::xml::get_bool(attr->children, true);
293 else if (!strcmp((const char *) attr->name,
296 mp::xml::get_bool(attr->children, true);
297 else if (!strcmp((const char *) attr->name,
301 mp::xml::get_bool(attr->children, true);
302 m_p->m_res_session = m_p->m_req_session;
304 else if (!strcmp((const char *) attr->name,
306 m_p->m_init_options =
307 mp::xml::get_bool(attr->children, true);
309 throw mp::filter::FilterException(
310 "Bad attribute " + std::string((const char *)
316 throw mp::filter::FilterException("Bad element "
317 + std::string((const char *)
323 static mp::filter::Base* filter_creator()
325 return new mp::filter::Log;
329 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
339 * indent-tabs-mode: nil
340 * c-file-style: "stroustrup"
342 * vim: shiftwidth=4 tabstop=8 expandtab