Add LICENSE file and Refer to it from the source. Include license material
[metaproxy-moved-to-github.git] / src / filter_log.cpp
1 /* $Id: filter_log.cpp,v 1.18 2006-06-10 14:29:12 adam Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8
9 #include "package.hpp"
10
11 #include <string>
12 #include <boost/thread/mutex.hpp>
13 #include <boost/date_time/posix_time/posix_time.hpp>
14
15 #include "util.hpp"
16 #include "filter_log.hpp"
17
18 #include <yaz/zgdu.h>
19
20 namespace mp = metaproxy_1;
21 namespace yf = mp::filter;
22
23 namespace metaproxy_1 {
24     namespace filter {
25         class Log::Rep {
26             friend class Log;
27             boost::mutex m_log_mutex;
28             std::string m_msg;
29         };
30     }
31 }
32
33 yf::Log::Log(const std::string &x) : m_p(new Rep)
34 {
35     m_p->m_msg = x;
36 }
37
38 yf::Log::Log() : m_p(new Rep)
39 {
40 }
41
42 yf::Log::~Log() {}
43
44 void yf::Log::process(mp::Package &package) const
45 {
46     Z_GDU *gdu;
47
48     // getting timestamp for receiving of package
49     boost::posix_time::ptime receive_time
50         = boost::posix_time::microsec_clock::local_time();
51
52     // scope for locking Ostream 
53     { 
54         boost::mutex::scoped_lock scoped_lock(m_p->m_log_mutex);
55         std::cout << receive_time << " " << m_p->m_msg;
56         std::cout << " request id=" << package.session().id();
57         std::cout << " close=" 
58                   << (package.session().is_closed() ? "yes" : "no")
59                   << "\n";
60         gdu = package.request().get();
61         if (gdu)
62         {
63             mp::odr odr(ODR_PRINT);
64             z_GDU(odr, &gdu, 0, 0);
65         }
66     }
67
68     // unlocked during move
69     package.move();
70
71     // getting timestamp for sending of package
72     boost::posix_time::ptime send_time
73         = boost::posix_time::microsec_clock::local_time();
74
75     boost::posix_time::time_duration duration = send_time - receive_time;
76
77     // scope for locking Ostream 
78     { 
79         boost::mutex::scoped_lock scoped_lock(m_p->m_log_mutex);
80         std::cout << send_time << " " << m_p->m_msg;
81         std::cout << " response id=" << package.session().id();
82         std::cout << " close=" 
83                   << (package.session().is_closed() ? "yes " : "no ")
84                   << "duration=" << duration      
85                   << "\n";
86             //<< "duration=" << duration.total_seconds() 
87             //    << "." << duration.fractional_seconds()
88             //      << "\n";
89         gdu = package.response().get();
90         if (gdu)
91         {
92             mp::odr odr(ODR_PRINT);
93             z_GDU(odr, &gdu, 0, 0);
94         }
95     }
96 }
97
98 void yf::Log::configure(const xmlNode *ptr)
99 {
100     for (ptr = ptr->children; ptr; ptr = ptr->next)
101     {
102         if (ptr->type != XML_ELEMENT_NODE)
103             continue;
104         if (!strcmp((const char *) ptr->name, "message"))
105             m_p->m_msg = mp::xml::get_text(ptr);
106         else
107         {
108             throw mp::filter::FilterException("Bad element " 
109                                                + std::string((const char *)
110                                                              ptr->name));
111         }
112     }
113 }
114
115 static mp::filter::Base* filter_creator()
116 {
117     return new mp::filter::Log;
118 }
119
120 extern "C" {
121     struct metaproxy_1_filter_struct metaproxy_1_filter_log = {
122         0,
123         "log",
124         filter_creator
125     };
126 }
127
128
129 /*
130  * Local variables:
131  * c-basic-offset: 4
132  * indent-tabs-mode: nil
133  * c-file-style: "stroustrup"
134  * End:
135  * vim: shiftwidth=4 tabstop=8 expandtab
136  */