1 /* $Id: filter_log.cpp,v 1.20 2006-06-20 22:29:39 adam 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>
16 #include "xmlutil.hpp"
17 #include "filter_log.hpp"
22 namespace mp = metaproxy_1;
23 namespace yf = metaproxy_1::filter;
25 namespace metaproxy_1 {
32 LFile(std::string fname);
34 typedef boost::shared_ptr<Log::LFile> LFilePtr;
38 void openfile(const std::string &fname);
47 // Only used during configure stage (no threading)
48 static std::list<LFilePtr> filter_log_files;
58 m_init_options = false;
61 yf::Log::Log(const std::string &x) : m_p(new Rep)
66 yf::Log::Log() : m_p(new Rep)
72 void stream_write(ODR o, void *handle, int type, const char *buf, int len)
74 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
75 lfile->out.write(buf, len);
78 void option_write(const char *name, void *handle)
80 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
81 lfile->out << " " << name;
84 void yf::Log::process(mp::Package &package) const
88 // getting timestamp for receiving of package
89 boost::posix_time::ptime receive_time
90 = boost::posix_time::microsec_clock::local_time();
92 // scope for locking Ostream
94 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
96 if (m_p->m_req_session)
98 m_p->m_file->out << receive_time << " " << m_p->m_msg;
99 m_p->m_file->out << " request id=" << package.session().id();
100 m_p->m_file->out << " close="
101 << (package.session().is_closed() ? "yes" : "no")
104 if (m_p->m_init_options)
106 gdu = package.request().get();
107 if (gdu && gdu->which == Z_GDU_Z3950 &&
108 gdu->u.z3950->which == Z_APDU_initRequest)
110 m_p->m_file->out << receive_time << " " << m_p->m_msg;
111 m_p->m_file->out << " init options:";
112 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
113 option_write, m_p->m_file.get());
114 m_p->m_file->out << "\n";
120 gdu = package.request().get();
123 mp::odr odr(ODR_PRINT);
124 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
125 z_GDU(odr, &gdu, 0, 0);
128 m_p->m_file->out.flush();
131 // unlocked during move
134 // getting timestamp for sending of package
135 boost::posix_time::ptime send_time
136 = boost::posix_time::microsec_clock::local_time();
138 boost::posix_time::time_duration duration = send_time - receive_time;
140 // scope for locking Ostream
142 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
143 if (m_p->m_res_session)
145 m_p->m_file->out << send_time << " " << m_p->m_msg;
146 m_p->m_file->out << " response id=" << package.session().id();
147 m_p->m_file->out << " close="
148 << (package.session().is_closed() ? "yes " : "no ")
149 << "duration=" << duration
152 if (m_p->m_init_options)
154 gdu = package.response().get();
155 if (gdu && gdu->which == Z_GDU_Z3950 &&
156 gdu->u.z3950->which == Z_APDU_initResponse)
158 m_p->m_file->out << receive_time << " " << m_p->m_msg;
159 m_p->m_file->out << " init options:";
160 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
161 option_write, m_p->m_file.get());
162 m_p->m_file->out << "\n";
167 gdu = package.response().get();
170 mp::odr odr(ODR_PRINT);
171 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
172 z_GDU(odr, &gdu, 0, 0);
175 m_p->m_file->out.flush();
179 yf::Log::LFile::LFile(std::string fname) : m_fname(fname)
181 out.open(fname.c_str());
184 void yf::Log::Rep::openfile(const std::string &fname)
186 std::list<LFilePtr>::const_iterator it = filter_log_files.begin();
187 for (; it != filter_log_files.end(); it++)
189 if ((*it)->m_fname == fname)
195 LFilePtr newfile(new LFile(fname));
196 filter_log_files.push_back(newfile);
200 void yf::Log::configure(const xmlNode *ptr)
202 for (ptr = ptr->children; ptr; ptr = ptr->next)
204 if (ptr->type != XML_ELEMENT_NODE)
206 if (!strcmp((const char *) ptr->name, "message"))
207 m_p->m_msg = mp::xml::get_text(ptr);
208 else if (!strcmp((const char *) ptr->name, "filename"))
210 std::string fname = mp::xml::get_text(ptr);
211 m_p->openfile(fname);
213 else if (!strcmp((const char *) ptr->name, "category"))
215 const struct _xmlAttr *attr;
216 for (attr = ptr->properties; attr; attr = attr->next)
218 if (!strcmp((const char *) attr->name, "request-apdu"))
219 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
220 else if (!strcmp((const char *) attr->name, "response-apdu"))
221 m_p->m_res_apdu = mp::xml::get_bool(attr->children, true);
222 else if (!strcmp((const char *) attr->name, "apdu"))
224 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
225 m_p->m_res_apdu = m_p->m_req_apdu;
227 else if (!strcmp((const char *) attr->name,
230 mp::xml::get_bool(attr->children, true);
231 else if (!strcmp((const char *) attr->name,
234 mp::xml::get_bool(attr->children, true);
235 else if (!strcmp((const char *) attr->name,
239 mp::xml::get_bool(attr->children, true);
240 m_p->m_res_session = m_p->m_req_session;
242 else if (!strcmp((const char *) attr->name,
244 m_p->m_init_options =
245 mp::xml::get_bool(attr->children, true);
247 throw mp::filter::FilterException(
248 "Bad attribute " + std::string((const char *)
254 throw mp::filter::FilterException("Bad element "
255 + std::string((const char *)
260 m_p->openfile("metaproxy.log");
263 static mp::filter::Base* filter_creator()
265 return new mp::filter::Log;
269 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
279 * indent-tabs-mode: nil
280 * c-file-style: "stroustrup"
282 * vim: shiftwidth=4 tabstop=8 expandtab