More functional virt_db filter. Removed p2_* source
[metaproxy-moved-to-github.git] / src / filter_log.cpp
1 /* $Id: filter_log.cpp,v 1.6 2005-10-25 11:48:30 adam 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 << " " << m_msg;
37         std::cout << " request id=" << package.session().id();
38         std::cout << " close=" 
39                   << (package.session().is_closed() ? "yes" : "no")
40                   << "\n";
41         gdu = package.request().get();
42         if (gdu)
43         {
44             ODR odr = odr_createmem(ODR_PRINT);
45             z_GDU(odr, &gdu, 0, 0);
46             odr_destroy(odr);
47         }
48     }
49
50     // unlocked during move
51     package.move();
52
53     // getting timestamp for sending of package
54     boost::posix_time::ptime send_time
55         = boost::posix_time::microsec_clock::local_time();
56
57     boost::posix_time::time_duration duration = send_time - receive_time;
58
59     // scope for locking Ostream 
60     { 
61         boost::mutex::scoped_lock scoped_lock(m_log_mutex);
62         std::cout << send_time << " " << m_msg;
63         std::cout << " response id=" << package.session().id();
64         std::cout << " close=" 
65                   << (package.session().is_closed() ? "yes " : "no ")
66                   << "duration=" << duration      
67                   << "\n";
68             //<< "duration=" << duration.total_seconds() 
69             //    << "." << duration.fractional_seconds()
70             //      << "\n";
71     }
72     
73     gdu = package.response().get();
74     if (gdu)
75     {
76         ODR odr = odr_createmem(ODR_PRINT);
77         z_GDU(odr, &gdu, 0, 0);
78         odr_destroy(odr);
79     }
80 }
81
82 void yp2::filter::Log::set_prefix(const std::string &msg)
83 {
84     m_msg = msg;
85 }
86
87 // defining and initializing static members
88 boost::mutex yp2::filter::Log::m_log_mutex;
89
90 /*
91  * Local variables:
92  * c-basic-offset: 4
93  * indent-tabs-mode: nil
94  * c-file-style: "stroustrup"
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */