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