~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/src/logger.cpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

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/filesystem/fstream.hpp>
41
 
#include <boost/filesystem/convenience.hpp>
42
 
 
43
 
#ifdef _MSC_VER
44
 
#pragma warning(pop)
45
 
#endif
46
 
 
47
 
#include <vector>
48
 
 
49
 
#include "libtorrent/extensions/logger.hpp"
50
 
#include "libtorrent/extensions.hpp"
51
 
#include "libtorrent/entry.hpp"
52
 
#include "libtorrent/peer_request.hpp"
53
 
#include "libtorrent/peer_connection.hpp"
54
 
 
55
 
namespace libtorrent {
56
 
 
57
 
namespace fs = boost::filesystem;
58
 
 
59
 
namespace
60
 
{
61
 
 
62
 
        struct logger_peer_plugin : peer_plugin
63
 
        {
64
 
                logger_peer_plugin(std::string const& filename)
65
 
                {
66
 
                        fs::path dir(fs::complete("libtorrent_ext_logs"));
67
 
                        if (!fs::exists(dir)) fs::create_directories(dir);
68
 
                        m_file.open((dir / filename).string().c_str(), std::ios_base::out | std::ios_base::out);
69
 
                        m_file << "\n\n\n";
70
 
                        log_timestamp();
71
 
                        m_file << "*** starting log ***\n";
72
 
                }
73
 
 
74
 
                void log_timestamp()
75
 
                {
76
 
                        m_file << time_now_string() << ": ";
77
 
                }
78
 
 
79
 
                // can add entries to the extension handshake
80
 
                virtual void add_handshake(entry&) {}
81
 
                
82
 
                // called when the extension handshake from the other end is received
83
 
                virtual bool on_extension_handshake(lazy_entry const& h)
84
 
                {
85
 
                        log_timestamp();
86
 
                        m_file << "<== EXTENSION_HANDSHAKE\n" << h;
87
 
                        return true;
88
 
                }
89
 
 
90
 
                // returning true from any of the message handlers
91
 
                // indicates that the plugin has handeled the message.
92
 
                // it will break the plugin chain traversing and not let
93
 
                // anyone else handle the message, including the default
94
 
                // handler.
95
 
 
96
 
                virtual bool on_choke()
97
 
                {
98
 
                        log_timestamp();
99
 
                        m_file << "<== CHOKE\n";
100
 
                        m_file.flush();
101
 
                        return false;
102
 
                }
103
 
 
104
 
                virtual bool on_unchoke()
105
 
                {
106
 
                        log_timestamp();
107
 
                        m_file << "<== UNCHOKE\n";
108
 
                        m_file.flush();
109
 
                        return false;
110
 
                }
111
 
 
112
 
                virtual bool on_interested()
113
 
                {
114
 
                        log_timestamp();
115
 
                        m_file << "<== INTERESTED\n";
116
 
                        m_file.flush();
117
 
                        return false;
118
 
                }
119
 
 
120
 
                virtual bool on_not_interested()
121
 
                {
122
 
                        log_timestamp();
123
 
                        m_file << "<== NOT_INTERESTED\n";
124
 
                        m_file.flush();
125
 
                        return false;
126
 
                }
127
 
 
128
 
                virtual bool on_have(int index)
129
 
                {
130
 
                        log_timestamp();
131
 
                        m_file << "<== HAVE [" << index << "]\n";
132
 
                        m_file.flush();
133
 
                        return false;
134
 
                }
135
 
 
136
 
                virtual bool on_bitfield(std::vector<bool> const& bitfield)
137
 
                {
138
 
                        log_timestamp();
139
 
                        m_file << "<== BITFIELD\n";
140
 
                        m_file.flush();
141
 
                        return false;
142
 
                }
143
 
 
144
 
                virtual bool on_request(peer_request const& r)
145
 
                {
146
 
                        log_timestamp();
147
 
                        m_file << "<== REQUEST [ piece: " << r.piece << " | s: " << r.start
148
 
                                << " | l: " << r.length << " ]\n";
149
 
                        m_file.flush();
150
 
                        return false;
151
 
                }
152
 
 
153
 
                virtual bool on_piece(peer_request const& r, char const*)
154
 
                {
155
 
                        log_timestamp();
156
 
                        m_file << "<== PIECE [ piece: " << r.piece << " | s: " << r.start
157
 
                                << " | l: " << r.length << " ]\n";
158
 
                        m_file.flush();
159
 
                        return false;
160
 
                }
161
 
 
162
 
                virtual bool on_cancel(peer_request const& r)
163
 
                {
164
 
                        log_timestamp();
165
 
                        m_file << "<== CANCEL [ piece: " << r.piece << " | s: " << r.start
166
 
                                << " | l: " << r.length << " ]\n";
167
 
                        m_file.flush();
168
 
                        return false;
169
 
                }
170
 
        
171
 
                // called when an extended message is received. If returning true,
172
 
                // the message is not processed by any other plugin and if false
173
 
                // is returned the next plugin in the chain will receive it to
174
 
                // be able to handle it
175
 
                virtual bool on_extended(int length
176
 
                        , int msg, buffer::const_interval body)
177
 
                { return false; }
178
 
 
179
 
                virtual bool on_unknown_message(int length, int msg
180
 
                        , buffer::const_interval body)
181
 
                {
182
 
                        if (body.left() < length) return false;
183
 
                        log_timestamp();
184
 
                        m_file << "<== UNKNOWN [ msg: " << msg
185
 
                                << " | l: " << length << " ]\n";
186
 
                        m_file.flush();
187
 
                        return false;
188
 
                }
189
 
 
190
 
                virtual void on_piece_pass(int index)
191
 
                {
192
 
                        log_timestamp();
193
 
                        m_file << "*** HASH PASSED *** [ piece: " << index << " ]\n";
194
 
                        m_file.flush();
195
 
                }
196
 
 
197
 
                virtual void on_piece_failed(int index)
198
 
                {
199
 
                        log_timestamp();
200
 
                        m_file << "*** HASH FAILED *** [ piece: " << index << " ]\n";
201
 
                        m_file.flush();
202
 
                }
203
 
 
204
 
        private:
205
 
                std::ofstream m_file;
206
 
        };
207
 
 
208
 
        struct logger_plugin : torrent_plugin
209
 
        {
210
 
                virtual boost::shared_ptr<peer_plugin> new_connection(
211
 
                        peer_connection* pc)
212
 
                {
213
 
                        error_code ec;
214
 
                        return boost::shared_ptr<peer_plugin>(new logger_peer_plugin(
215
 
                                pc->remote().address().to_string(ec) + "_"
216
 
                                + to_string(pc->remote().port()).elems + ".log"));
217
 
                }
218
 
        };
219
 
 
220
 
} }
221
 
 
222
 
namespace libtorrent
223
 
{
224
 
 
225
 
        boost::shared_ptr<torrent_plugin> create_logger_plugin(torrent*)
226
 
        {
227
 
                return boost::shared_ptr<torrent_plugin>(new logger_plugin());
228
 
        }
229
 
 
230
 
}
231
 
 
232