1 /* $Id: filter_log.cpp,v 1.26 2006-08-31 12:56:40 marc Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
7 #include "filter_log.hpp"
13 #include <boost/thread/mutex.hpp>
14 #include <boost/date_time/posix_time/posix_time.hpp>
16 #include "gduutil.hpp"
18 #include "xmlutil.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;
64 m_req_session = false;
65 m_res_session = false;
66 m_init_options = false;
70 yf::Log::Log(const std::string &x) : m_p(new Rep)
72 m_p->m_msg_config = x;
75 yf::Log::Log() : m_p(new Rep)
81 void stream_write(ODR o, void *handle, int type, const char *buf, int len)
83 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
84 lfile->out.write(buf, len);
87 void option_write(const char *name, void *handle)
89 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
90 lfile->out << " " << name;
93 void yf::Log::process(mp::Package &package) const
97 // getting timestamp for receiving of package
98 boost::posix_time::ptime receive_time
99 = boost::posix_time::microsec_clock::local_time();
103 // scope for locking Ostream
105 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
110 gdu = package.request().get();
114 //<< receive_time << " "
115 //<< to_iso_string(receive_time) << " "
116 << to_iso_extended_string(receive_time) << " "
117 << m_p->m_msg_config << " "
119 << "000000.000000" << " "
125 if (m_p->m_req_session)
127 m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
128 m_p->m_file->out << " request id=" << package.session().id();
129 m_p->m_file->out << " close="
130 << (package.session().is_closed() ? "yes" : "no")
134 if (m_p->m_init_options)
136 gdu = package.request().get();
137 if (gdu && gdu->which == Z_GDU_Z3950 &&
138 gdu->u.z3950->which == Z_APDU_initRequest)
140 m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
141 m_p->m_file->out << " init options:";
142 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
143 option_write, m_p->m_file.get());
144 m_p->m_file->out << "\n";
150 gdu = package.request().get();
153 mp::odr odr(ODR_PRINT);
154 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
155 z_GDU(odr, &gdu, 0, 0);
158 m_p->m_file->out.flush();
161 // unlocked during move
164 // getting timestamp for sending of package
165 boost::posix_time::ptime send_time
166 = boost::posix_time::microsec_clock::local_time();
168 boost::posix_time::time_duration duration = send_time - receive_time;
170 // scope for locking Ostream
172 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
176 gdu = package.response().get();
180 //<< send_time << " "
181 //<< to_iso_string(send_time) << " "
182 << to_iso_extended_string(send_time) << " "
183 << m_p->m_msg_config << " "
185 << to_iso_string(duration) << " "
191 if (m_p->m_res_session)
193 m_p->m_file->out << send_time << " " << m_p->m_msg_config;
194 m_p->m_file->out << " response id=" << package.session().id();
195 m_p->m_file->out << " close="
196 << (package.session().is_closed() ? "yes " : "no ")
197 << "duration=" << duration
201 if (m_p->m_init_options)
203 gdu = package.response().get();
204 if (gdu && gdu->which == Z_GDU_Z3950 &&
205 gdu->u.z3950->which == Z_APDU_initResponse)
207 m_p->m_file->out << receive_time << " " << m_p->m_msg_config;
208 m_p->m_file->out << " init options:";
209 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
210 option_write, m_p->m_file.get());
211 m_p->m_file->out << "\n";
217 gdu = package.response().get();
220 mp::odr odr(ODR_PRINT);
221 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
222 z_GDU(odr, &gdu, 0, 0);
226 m_p->m_file->out.flush();
230 yf::Log::LFile::LFile(std::string fname) :
231 m_fname(fname), fout(fname.c_str()), out(fout)
235 yf::Log::LFile::LFile(std::string fname, std::ostream &use_this) :
236 m_fname(fname), out(use_this)
240 void yf::Log::Rep::openfile(const std::string &fname)
242 std::list<LFilePtr>::const_iterator it = filter_log_files.begin();
243 for (; it != filter_log_files.end(); it++)
245 if ((*it)->m_fname == fname)
251 // open stdout for empty file
252 LFilePtr newfile(fname.length() == 0
253 ? new LFile(fname, std::cout)
255 filter_log_files.push_back(newfile);
259 void yf::Log::configure(const xmlNode *ptr)
261 for (ptr = ptr->children; ptr; ptr = ptr->next)
263 if (ptr->type != XML_ELEMENT_NODE)
265 if (!strcmp((const char *) ptr->name, "message"))
266 m_p->m_msg_config = mp::xml::get_text(ptr);
267 else if (!strcmp((const char *) ptr->name, "filename"))
269 std::string fname = mp::xml::get_text(ptr);
270 m_p->openfile(fname);
272 else if (!strcmp((const char *) ptr->name, "category"))
274 const struct _xmlAttr *attr;
275 for (attr = ptr->properties; attr; attr = attr->next)
277 if (!strcmp((const char *) attr->name,
280 mp::xml::get_bool(attr->children, true);
281 else if (!strcmp((const char *) attr->name, "request-apdu"))
282 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
283 else if (!strcmp((const char *) attr->name, "response-apdu"))
284 m_p->m_res_apdu = mp::xml::get_bool(attr->children, true);
285 else if (!strcmp((const char *) attr->name, "apdu"))
287 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
288 m_p->m_res_apdu = m_p->m_req_apdu;
290 else if (!strcmp((const char *) attr->name,
293 mp::xml::get_bool(attr->children, true);
294 else if (!strcmp((const char *) attr->name,
297 mp::xml::get_bool(attr->children, true);
298 else if (!strcmp((const char *) attr->name,
302 mp::xml::get_bool(attr->children, true);
303 m_p->m_res_session = m_p->m_req_session;
305 else if (!strcmp((const char *) attr->name,
307 m_p->m_init_options =
308 mp::xml::get_bool(attr->children, true);
310 throw mp::filter::FilterException(
311 "Bad attribute " + std::string((const char *)
317 throw mp::filter::FilterException("Bad element "
318 + std::string((const char *)
324 static mp::filter::Base* filter_creator()
326 return new mp::filter::Log;
330 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
340 * indent-tabs-mode: nil
341 * c-file-style: "stroustrup"
343 * vim: shiftwidth=4 tabstop=8 expandtab