added test case for yp2::filter::Log, added time stamping inside log filter, added...
[metaproxy-moved-to-github.git] / src / filter_log.cpp
1 /* $Id: filter_log.cpp,v 1.5 2005-10-19 22:45:59 marc Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7
8 #include "config.hpp"
9
10 #include "filter.hpp"
11 #include "router.hpp"
12 #include "package.hpp"
13
14 #include "filter_log.hpp"
15
16 #include <yaz/zgdu.h>
17 #include <yaz/log.h>
18
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 #include <iostream>
21
22
23 yp2::filter::Log::Log() {}
24
25 void yp2::filter::Log::process(Package &package) const {
26
27     Z_GDU *gdu;
28
29     // getting timestamp for receiving of package
30     boost::posix_time::ptime receive_time
31         = boost::posix_time::microsec_clock::local_time();
32
33     // scope for locking Ostream 
34     { 
35         boost::mutex::scoped_lock scoped_lock(m_log_mutex);
36         std::cout << receive_time << " ";
37         std::cout << "request id=" << package.session().id();
38         std::cout << " close=" 
39                   << (package.session().is_closed() ? "yes" : "no")
40                   << "\n";
41     }
42     
43     gdu = package.request().get();
44     if (gdu)
45     {
46         ODR odr = odr_createmem(ODR_PRINT);
47         z_GDU(odr, &gdu, 0, 0);
48         odr_destroy(odr);
49     }
50
51     // unlocked during move
52     package.move();
53
54     // getting timestamp for sending of package
55     boost::posix_time::ptime send_time
56         = boost::posix_time::microsec_clock::local_time();
57
58     boost::posix_time::time_duration duration = send_time - receive_time;
59
60     // scope for locking Ostream 
61     { 
62         boost::mutex::scoped_lock scoped_lock(m_log_mutex);
63         std::cout << send_time << " ";
64         std::cout << "response id=" << package.session().id();
65         std::cout << " close=" 
66                   << (package.session().is_closed() ? "yes " : "no ")
67                   << "duration=" << duration      
68                   << "\n";
69             //<< "duration=" << duration.total_seconds() 
70             //    << "." << duration.fractional_seconds()
71             //      << "\n";
72     }
73     
74     gdu = package.response().get();
75     if (gdu)
76     {
77         ODR odr = odr_createmem(ODR_PRINT);
78         z_GDU(odr, &gdu, 0, 0);
79         odr_destroy(odr);
80     }
81 }
82
83 // defining and initializing static members
84 boost::mutex yp2::filter::Log::m_log_mutex;
85
86 /*
87  * Local variables:
88  * c-basic-offset: 4
89  * indent-tabs-mode: nil
90  * c-file-style: "stroustrup"
91  * End:
92  * vim: shiftwidth=4 tabstop=8 expandtab
93  */