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