file_progress fix
[libtorrent.git] / src / logger.cpp
blob1ecba5045be53ecc96562f0559af5150cf720931
1 /*
3 Copyright (c) 2006, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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.
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.
33 #include "libtorrent/pch.hpp"
35 #ifdef _MSC_VER
36 #pragma warning(push, 1)
37 #endif
39 #include <boost/shared_ptr.hpp>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/filesystem/fstream.hpp>
42 #include <boost/filesystem/convenience.hpp>
44 #ifdef _MSC_VER
45 #pragma warning(pop)
46 #endif
48 #include <vector>
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"
56 namespace libtorrent {
58 namespace fs = boost::filesystem;
60 namespace
63 struct logger_peer_plugin : peer_plugin
65 logger_peer_plugin(std::string const& filename)
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";
75 void log_timestamp()
77 m_file << time_now_string() << ": ";
80 // can add entries to the extension handshake
81 virtual void add_handshake(entry&) {}
83 // called when the extension handshake from the other end is received
84 virtual bool on_extension_handshake(lazy_entry const& h)
86 log_timestamp();
87 m_file << "<== EXTENSION_HANDSHAKE\n" << h;
88 return true;
91 // returning true from any of the message handlers
92 // indicates that the plugin has handeled the message.
93 // it will break the plugin chain traversing and not let
94 // anyone else handle the message, including the default
95 // handler.
97 virtual bool on_choke()
99 log_timestamp();
100 m_file << "<== CHOKE\n";
101 m_file.flush();
102 return false;
105 virtual bool on_unchoke()
107 log_timestamp();
108 m_file << "<== UNCHOKE\n";
109 m_file.flush();
110 return false;
113 virtual bool on_interested()
115 log_timestamp();
116 m_file << "<== INTERESTED\n";
117 m_file.flush();
118 return false;
121 virtual bool on_not_interested()
123 log_timestamp();
124 m_file << "<== NOT_INTERESTED\n";
125 m_file.flush();
126 return false;
129 virtual bool on_have(int index)
131 log_timestamp();
132 m_file << "<== HAVE [" << index << "]\n";
133 m_file.flush();
134 return false;
137 virtual bool on_bitfield(std::vector<bool> const& bitfield)
139 log_timestamp();
140 m_file << "<== BITFIELD\n";
141 m_file.flush();
142 return false;
145 virtual bool on_request(peer_request const& r)
147 log_timestamp();
148 m_file << "<== REQUEST [ piece: " << r.piece << " | s: " << r.start
149 << " | l: " << r.length << " ]\n";
150 m_file.flush();
151 return false;
154 virtual bool on_piece(peer_request const& r, char const*)
156 log_timestamp();
157 m_file << "<== PIECE [ piece: " << r.piece << " | s: " << r.start
158 << " | l: " << r.length << " ]\n";
159 m_file.flush();
160 return false;
163 virtual bool on_cancel(peer_request const& r)
165 log_timestamp();
166 m_file << "<== CANCEL [ piece: " << r.piece << " | s: " << r.start
167 << " | l: " << r.length << " ]\n";
168 m_file.flush();
169 return false;
172 // called when an extended message is received. If returning true,
173 // the message is not processed by any other plugin and if false
174 // is returned the next plugin in the chain will receive it to
175 // be able to handle it
176 virtual bool on_extended(int length
177 , int msg, buffer::const_interval body)
178 { return false; }
180 virtual bool on_unknown_message(int length, int msg
181 , buffer::const_interval body)
183 if (body.left() < length) return false;
184 log_timestamp();
185 m_file << "<== UNKNOWN [ msg: " << msg
186 << " | l: " << length << " ]\n";
187 m_file.flush();
188 return false;
191 virtual void on_piece_pass(int index)
193 log_timestamp();
194 m_file << "*** HASH PASSED *** [ piece: " << index << " ]\n";
195 m_file.flush();
198 virtual void on_piece_failed(int index)
200 log_timestamp();
201 m_file << "*** HASH FAILED *** [ piece: " << index << " ]\n";
202 m_file.flush();
205 private:
206 std::ofstream m_file;
209 struct logger_plugin : torrent_plugin
211 virtual boost::shared_ptr<peer_plugin> new_connection(
212 peer_connection* pc)
214 return boost::shared_ptr<peer_plugin>(new logger_peer_plugin(
215 pc->remote().address().to_string() + "_"
216 + boost::lexical_cast<std::string>(pc->remote().port()) + ".log"));
222 namespace libtorrent
225 boost::shared_ptr<torrent_plugin> create_logger_plugin(torrent*)
227 return boost::shared_ptr<torrent_plugin>(new logger_plugin());