~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to src/logger.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2006, Arvid Norberg
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
10
    * Redistributions of source code must retain the above copyright
 
11
      notice, this list of conditions and the following disclaimer.
 
12
    * Redistributions in binary form must reproduce the above copyright
 
13
      notice, this list of conditions and the following disclaimer in
 
14
      the documentation and/or other materials provided with the distribution.
 
15
    * Neither the name of the author nor the names of its
 
16
      contributors may be used to endorse or promote products derived
 
17
      from this software without specific prior written permission.
 
18
 
 
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
29
POSSIBILITY OF SUCH DAMAGE.
 
30
 
 
31
*/
 
32
 
 
33
#include "libtorrent/pch.hpp"
 
34
 
 
35
#ifdef _MSC_VER
 
36
#pragma warning(push, 1)
 
37
#endif
 
38
 
 
39
#include <boost/shared_ptr.hpp>
 
40
#include <boost/lexical_cast.hpp>
 
41
#include <boost/filesystem/fstream.hpp>
 
42
#include <boost/filesystem/convenience.hpp>
 
43
 
 
44
#ifdef _MSC_VER
 
45
#pragma warning(pop)
 
46
#endif
 
47
 
 
48
#include <vector>
 
49
 
 
50
#include "libtorrent/extensions/logger.hpp"
 
51
#include "libtorrent/extensions.hpp"
 
52
#include "libtorrent/entry.hpp"
 
53
#include "libtorrent/peer_request.hpp"
 
54
#include "libtorrent/peer_connection.hpp"
 
55
 
 
56
namespace libtorrent {
 
57
 
 
58
namespace fs = boost::filesystem;
 
59
 
 
60
namespace
 
61
{
 
62
 
 
63
        struct logger_peer_plugin : peer_plugin
 
64
        {
 
65
                logger_peer_plugin(std::string const& filename)
 
66
                {
 
67
                        fs::path dir(fs::complete("libtorrent_ext_logs"));
 
68
                        if (!fs::exists(dir)) fs::create_directories(dir);
 
69
                        m_file.open((dir / filename).string().c_str(), std::ios_base::out | std::ios_base::out);
 
70
                        m_file << "\n\n\n";
 
71
                        log_timestamp();
 
72
                        m_file << "*** starting log ***\n";
 
73
                }
 
74
 
 
75
                void log_timestamp()
 
76
                {
 
77
                        m_file << time_now_string() << ": ";
 
78
                }
 
79
 
 
80
                // can add entries to the extension handshake
 
81
                virtual void add_handshake(entry&) {}
 
82
                
 
83
                // called when the extension handshake from the other end is received
 
84
                virtual bool on_extension_handshake(entry const& h)
 
85
                {
 
86
                        log_timestamp();
 
87
                        m_file << "<== EXTENSION_HANDSHAKE\n";
 
88
                        h.print(m_file);
 
89
                        return true;
 
90
                }
 
91
 
 
92
                // returning true from any of the message handlers
 
93
                // indicates that the plugin has handeled the message.
 
94
                // it will break the plugin chain traversing and not let
 
95
                // anyone else handle the message, including the default
 
96
                // handler.
 
97
 
 
98
                virtual bool on_choke()
 
99
                {
 
100
                        log_timestamp();
 
101
                        m_file << "<== CHOKE\n";
 
102
                        m_file.flush();
 
103
                        return false;
 
104
                }
 
105
 
 
106
                virtual bool on_unchoke()
 
107
                {
 
108
                        log_timestamp();
 
109
                        m_file << "<== UNCHOKE\n";
 
110
                        m_file.flush();
 
111
                        return false;
 
112
                }
 
113
 
 
114
                virtual bool on_interested()
 
115
                {
 
116
                        log_timestamp();
 
117
                        m_file << "<== INTERESTED\n";
 
118
                        m_file.flush();
 
119
                        return false;
 
120
                }
 
121
 
 
122
                virtual bool on_not_interested()
 
123
                {
 
124
                        log_timestamp();
 
125
                        m_file << "<== NOT_INTERESTED\n";
 
126
                        m_file.flush();
 
127
                        return false;
 
128
                }
 
129
 
 
130
                virtual bool on_have(int index)
 
131
                {
 
132
                        log_timestamp();
 
133
                        m_file << "<== HAVE [" << index << "]\n";
 
134
                        m_file.flush();
 
135
                        return false;
 
136
                }
 
137
 
 
138
                virtual bool on_bitfield(std::vector<bool> const& bitfield)
 
139
                {
 
140
                        log_timestamp();
 
141
                        m_file << "<== BITFIELD\n";
 
142
                        m_file.flush();
 
143
                        return false;
 
144
                }
 
145
 
 
146
                virtual bool on_request(peer_request const& r)
 
147
                {
 
148
                        log_timestamp();
 
149
                        m_file << "<== REQUEST [ piece: " << r.piece << " | s: " << r.start
 
150
                                << " | l: " << r.length << " ]\n";
 
151
                        m_file.flush();
 
152
                        return false;
 
153
                }
 
154
 
 
155
                virtual bool on_piece(peer_request const& r, char const*)
 
156
                {
 
157
                        log_timestamp();
 
158
                        m_file << "<== PIECE [ piece: " << r.piece << " | s: " << r.start
 
159
                                << " | l: " << r.length << " ]\n";
 
160
                        m_file.flush();
 
161
                        return false;
 
162
                }
 
163
 
 
164
                virtual bool on_cancel(peer_request const& r)
 
165
                {
 
166
                        log_timestamp();
 
167
                        m_file << "<== CANCEL [ piece: " << r.piece << " | s: " << r.start
 
168
                                << " | l: " << r.length << " ]\n";
 
169
                        m_file.flush();
 
170
                        return false;
 
171
                }
 
172
        
 
173
                // called when an extended message is received. If returning true,
 
174
                // the message is not processed by any other plugin and if false
 
175
                // is returned the next plugin in the chain will receive it to
 
176
                // be able to handle it
 
177
                virtual bool on_extended(int length
 
178
                        , int msg, buffer::const_interval body)
 
179
                { return false; }
 
180
 
 
181
                virtual bool on_unknown_message(int length, int msg
 
182
                        , buffer::const_interval body)
 
183
                {
 
184
                        if (body.left() < length) return false;
 
185
                        log_timestamp();
 
186
                        m_file << "<== UNKNOWN [ msg: " << msg
 
187
                                << " | l: " << length << " ]\n";
 
188
                        m_file.flush();
 
189
                        return false;
 
190
                }
 
191
 
 
192
                virtual void on_piece_pass(int index)
 
193
                {
 
194
                        log_timestamp();
 
195
                        m_file << "*** HASH PASSED *** [ piece: " << index << " ]\n";
 
196
                        m_file.flush();
 
197
                }
 
198
 
 
199
                virtual void on_piece_failed(int index)
 
200
                {
 
201
                        log_timestamp();
 
202
                        m_file << "*** HASH FAILED *** [ piece: " << index << " ]\n";
 
203
                        m_file.flush();
 
204
                }
 
205
 
 
206
        private:
 
207
                std::ofstream m_file;
 
208
        };
 
209
 
 
210
        struct logger_plugin : torrent_plugin
 
211
        {
 
212
                virtual boost::shared_ptr<peer_plugin> new_connection(
 
213
                        peer_connection* pc)
 
214
                {
 
215
                        return boost::shared_ptr<peer_plugin>(new logger_peer_plugin(
 
216
                                pc->remote().address().to_string() + "_"
 
217
                                + boost::lexical_cast<std::string>(pc->remote().port()) + ".log"));
 
218
                }
 
219
        };
 
220
 
 
221
} }
 
222
 
 
223
namespace libtorrent
 
224
{
 
225
 
 
226
        boost::shared_ptr<torrent_plugin> create_logger_plugin(torrent*)
 
227
        {
 
228
                return boost::shared_ptr<torrent_plugin>(new logger_plugin());
 
229
        }
 
230
 
 
231
}
 
232
 
 
233