1 /* $Id: filter_log.cpp,v 1.19 2006-06-19 13:08:00 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);
46 // Only used during configure stage (no threading)
47 static std::list<LFilePtr> filter_log_files;
59 yf::Log::Log(const std::string &x) : m_p(new Rep)
64 yf::Log::Log() : m_p(new Rep)
70 void stream_write(ODR o, void *handle, int type, const char *buf, int len)
72 yf::Log::LFile *lfile = (yf::Log::LFile*) handle;
73 lfile->out.write(buf, len);
76 void yf::Log::process(mp::Package &package) const
80 // getting timestamp for receiving of package
81 boost::posix_time::ptime receive_time
82 = boost::posix_time::microsec_clock::local_time();
84 // scope for locking Ostream
86 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
88 if (m_p->m_req_session)
90 m_p->m_file->out << receive_time << " " << m_p->m_msg;
91 m_p->m_file->out << " request id=" << package.session().id();
92 m_p->m_file->out << " close="
93 << (package.session().is_closed() ? "yes" : "no")
98 gdu = package.request().get();
101 mp::odr odr(ODR_PRINT);
102 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
103 z_GDU(odr, &gdu, 0, 0);
106 m_p->m_file->out.flush();
109 // unlocked during move
112 // getting timestamp for sending of package
113 boost::posix_time::ptime send_time
114 = boost::posix_time::microsec_clock::local_time();
116 boost::posix_time::time_duration duration = send_time - receive_time;
118 // scope for locking Ostream
120 boost::mutex::scoped_lock scoped_lock(m_p->m_file->m_mutex);
121 if (m_p->m_res_session)
123 m_p->m_file->out << send_time << " " << m_p->m_msg;
124 m_p->m_file->out << " response id=" << package.session().id();
125 m_p->m_file->out << " close="
126 << (package.session().is_closed() ? "yes " : "no ")
127 << "duration=" << duration
132 gdu = package.response().get();
135 mp::odr odr(ODR_PRINT);
136 odr_set_stream(odr, m_p->m_file.get(), stream_write, 0);
137 z_GDU(odr, &gdu, 0, 0);
140 m_p->m_file->out.flush();
144 yf::Log::LFile::LFile(std::string fname) : m_fname(fname)
146 out.open(fname.c_str());
149 void yf::Log::Rep::openfile(const std::string &fname)
151 std::list<LFilePtr>::const_iterator it = filter_log_files.begin();
152 for (; it != filter_log_files.end(); it++)
154 if ((*it)->m_fname == fname)
160 LFilePtr newfile(new LFile(fname));
161 filter_log_files.push_back(newfile);
165 void yf::Log::configure(const xmlNode *ptr)
167 for (ptr = ptr->children; ptr; ptr = ptr->next)
169 if (ptr->type != XML_ELEMENT_NODE)
171 if (!strcmp((const char *) ptr->name, "message"))
172 m_p->m_msg = mp::xml::get_text(ptr);
173 else if (!strcmp((const char *) ptr->name, "filename"))
175 std::string fname = mp::xml::get_text(ptr);
176 m_p->openfile(fname);
178 else if (!strcmp((const char *) ptr->name, "category"))
180 const struct _xmlAttr *attr;
181 for (attr = ptr->properties; attr; attr = attr->next)
183 if (!strcmp((const char *) attr->name, "request-apdu"))
184 m_p->m_req_apdu = mp::xml::get_bool(attr->children, true);
185 else if (!strcmp((const char *) attr->name, "response-apdu"))
186 m_p->m_res_apdu = 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,
194 mp::xml::get_bool(attr->children, true);
196 throw mp::filter::FilterException(
197 "Bad attribute " + std::string((const char *)
203 throw mp::filter::FilterException("Bad element "
204 + std::string((const char *)
209 m_p->openfile("metaproxy.log");
212 static mp::filter::Base* filter_creator()
214 return new mp::filter::Log;
218 struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
228 * indent-tabs-mode: nil
229 * c-file-style: "stroustrup"
231 * vim: shiftwidth=4 tabstop=8 expandtab