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