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