Implemented yp2::odr which is a noncopyable wrapper for YAZ' ODR.
[metaproxy-moved-to-github.git] / src / filter_log.cpp
1 /* $Id: filter_log.cpp,v 1.8 2005-10-30 17:13:36 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 "util.hpp"
15 #include "filter_log.hpp"
16
17 #include <yaz/zgdu.h>
18 #include <yaz/log.h>
19
20 #include <boost/date_time/posix_time/posix_time.hpp>
21 #include <iostream>
22
23
24 yp2::filter::Log::Log() {}
25 yp2::filter::Log::Log(const std::string &msg) : m_msg(msg) {}
26
27 void yp2::filter::Log::process(Package &package) const {
28
29     Z_GDU *gdu;
30
31     // getting timestamp for receiving of package
32     boost::posix_time::ptime receive_time
33         = boost::posix_time::microsec_clock::local_time();
34
35     // scope for locking Ostream 
36     { 
37         boost::mutex::scoped_lock scoped_lock(m_log_mutex);
38         std::cout << receive_time << " " << m_msg;
39         std::cout << " request id=" << package.session().id();
40         std::cout << " close=" 
41                   << (package.session().is_closed() ? "yes" : "no")
42                   << "\n";
43         gdu = package.request().get();
44         if (gdu)
45         {
46             yp2::odr odr(ODR_PRINT);
47             z_GDU(odr, &gdu, 0, 0);
48         }
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 << " " << m_msg;
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         gdu = package.response().get();
73         if (gdu)
74         {
75             yp2::odr odr(ODR_PRINT);
76             z_GDU(odr, &gdu, 0, 0);
77         }
78     }
79 }
80
81 // defining and initializing static members
82 boost::mutex yp2::filter::Log::m_log_mutex;
83
84 /*
85  * Local variables:
86  * c-basic-offset: 4
87  * indent-tabs-mode: nil
88  * c-file-style: "stroustrup"
89  * End:
90  * vim: shiftwidth=4 tabstop=8 expandtab
91  */