1 /* $Id: filter_log.cpp,v 1.21 2006-06-21 12:50:35 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 {
33 LFile(std::string fname);
34 LFile(std::string fname, std::ostream &use_this);
36 typedef boost::shared_ptr<Log::LFile> LFilePtr;
40 void openfile(const std::string &fname);
49 // Only used during configure stage (no threading)
50 static std::list<LFilePtr> filter_log_files;
60 m_init_options = false;
64 yf::Log::Log(const std::string &x) : m_p(new Rep)
69 yf::Log::Log() : m_p(new Rep)
75 void stream_write(ODR o, void *handle, int type, const char *buf, int len)
77 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
78 lfile->out.write(buf, len);
81 void option_write(const char *name, void *handle)
83 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
84 lfile->out << " " << name;
87 void yf::Log::process(mp::Package &package) const
91 // getting timestamp for receiving of package
92 boost::posix_time::ptime receive_time
93 = boost::posix_time::microsec_clock::local_time();
95 // scope for locking Ostream
97 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
99 if (m_p->m_req_session)
101 m_p->m_file->out << receive_time << " " << m_p->m_msg;
102 m_p->m_file->out << " request id=" << package.session().id();
103 m_p->m_file->out << " close="
104 << (package.session().is_closed() ? "yes" : "no")
107 if (m_p->m_init_options)
109 gdu = package.request().get();
110 if (gdu && gdu->which == Z_GDU_Z3950 &&
111 gdu->u.z3950->which == Z_APDU_initRequest)
113 m_p->m_file->out << receive_time << " " << m_p->m_msg;
114 m_p->m_file->out << " init options:";
115 yaz_init_opt_decode(gdu->u.z3950->u.initRequest->options,
116 option_write, m_p->m_file.get());
117 m_p->m_file->out << "\n";
123 gdu = package.request().get();
126 mp::odr odr(ODR_PRINT);
127 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
128 z_GDU(odr, &gdu, 0, 0);
131 m_p->m_file->out.flush();
134 // unlocked during move
137 // getting timestamp for sending of package
138 boost::posix_time::ptime send_time
139 = boost::posix_time::microsec_clock::local_time();
141 boost::posix_time::time_duration duration = send_time - receive_time;
143 // scope for locking Ostream
145 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
146 if (m_p->m_res_session)
148 m_p->m_file->out << send_time << " " << m_p->m_msg;
149 m_p->m_file->out << " response id=" << package.session().id();
150 m_p->m_file->out << " close="
151 << (package.session().is_closed() ? "yes " : "no ")
152 << "duration=" << duration
155 if (m_p->m_init_options)
157 gdu = package.response().get();
158 if (gdu && gdu->which == Z_GDU_Z3950 &&
159 gdu->u.z3950->which == Z_APDU_initResponse)
161 m_p->m_file->out << receive_time << " " << m_p->m_msg;
162 m_p->m_file->out << " init options:";
163 yaz_init_opt_decode(gdu->u.z3950->u.initResponse->options,
164 option_write, m_p->m_file.get());
165 m_p->m_file->out << "\n";
170 gdu = package.response().get();
173 mp::odr odr(ODR_PRINT);
174 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
175 z_GDU(odr, &gdu, 0, 0);
178 m_p->m_file->out.flush();
182 yf::Log::LFile::LFile(std::string fname) :
183 m_fname(fname), fout(fname.c_str()), out(fout)
187 yf::Log::LFile::LFile(std::string fname, std::ostream &use_this) :
188 m_fname(fname), out(use_this)
192 void yf::Log::Rep::openfile(const std::string &fname)
194 std::list<LFilePtr>::const_iterator it = filter_log_files.begin();
195 for (; it != filter_log_files.end(); it++)
197 if ((*it)->m_fname == fname)
203 // open stdout for empty file
204 LFilePtr newfile(fname.length() == 0
205 ? new LFile(fname, std::cout)
207 filter_log_files.push_back(newfile);
211 void yf::Log::configure(const xmlNode *ptr)
213 for (ptr = ptr->children; ptr; ptr = ptr->next)
215 if (ptr->type != XML_ELEMENT_NODE)
217 if (!strcmp((const char *) ptr->name, "message"))
218 m_p->m_msg = mp::xml::get_text(ptr);
219 else if (!strcmp((const char *) ptr->name, "filename"))
221 std::string fname = mp::xml::get_text(ptr);
222 m_p->openfile(fname);
224 else if (!strcmp((const char *) ptr->name, "category"))
226 const struct _xmlAttr *attr;
227 for (attr = ptr->properties; attr; attr = attr->next)
229 if (!strcmp((const char *) attr->name, "request-apdu"))
230 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
231 else if (!strcmp((const char *) attr->name, "response-apdu"))
232 m_p->m_res_apdu = mp::xml::get_bool(attr->children, true);
233 else if (!strcmp((const char *) attr->name, "apdu"))
235 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
236 m_p->m_res_apdu = m_p->m_req_apdu;
238 else if (!strcmp((const char *) attr->name,
241 mp::xml::get_bool(attr->children, true);
242 else if (!strcmp((const char *) attr->name,
245 mp::xml::get_bool(attr->children, true);
246 else if (!strcmp((const char *) attr->name,
250 mp::xml::get_bool(attr->children, true);
251 m_p->m_res_session = m_p->m_req_session;
253 else if (!strcmp((const char *) attr->name,
255 m_p->m_init_options =
256 mp::xml::get_bool(attr->children, true);
258 throw mp::filter::FilterException(
259 "Bad attribute " + std::string((const char *)
265 throw mp::filter::FilterException("Bad element "
266 + std::string((const char *)
272 static mp::filter::Base* filter_creator()
274 return new mp::filter::Log;
278 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
288 * indent-tabs-mode: nil
289 * c-file-style: "stroustrup"
291 * vim: shiftwidth=4 tabstop=8 expandtab