Do not include router.hpp in filters
[metaproxy-moved-to-github.git] / src / filter_log.cpp
1 /* $Id: filter_log.cpp,v 1.13 2006-01-09 21:20:15 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8
9 #include "filter.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 static yp2::filter::Base* filter_creator()
101 {
102     return new yp2::filter::Log;
103 }
104
105 extern "C" {
106     struct yp2_filter_struct yp2_filter_log = {
107         0,
108         "log",
109         filter_creator
110     };
111 }
112
113
114 /*
115  * Local variables:
116  * c-basic-offset: 4
117  * indent-tabs-mode: nil
118  * c-file-style: "stroustrup"
119  * End:
120  * vim: shiftwidth=4 tabstop=8 expandtab
121  */