formatting fixes in client test, and made the test build when resolve countries is...
[libtorrent-kjk.git] / src / logger.cpp
blob6881c5e7b4ceba9e81f2b12c0ee0b63e6ad996ac
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 { namespace
59 struct logger_peer_plugin : peer_plugin
61 logger_peer_plugin(std::string const& filename)
63 using namespace boost::filesystem;
64 path dir(complete("libtorrent_ext_logs"));
65 if (!exists(dir)) create_directories(dir);
66 m_file.open(dir / filename, std::ios_base::out | std::ios_base::out);
67 m_file << "\n\n\n";
68 log_timestamp();
69 m_file << "*** starting log ***\n";
72 void log_timestamp()
74 m_file << time_now_string() << ": ";
77 // can add entries to the extension handshake
78 virtual void add_handshake(entry&) {}
80 // called when the extension handshake from the other end is received
81 virtual bool on_extension_handshake(entry const& h)
83 log_timestamp();
84 m_file << "<== EXTENSION_HANDSHAKE\n";
85 h.print(m_file);
86 return true;
89 // returning true from any of the message handlers
90 // indicates that the plugin has handeled the message.
91 // it will break the plugin chain traversing and not let
92 // anyone else handle the message, including the default
93 // handler.
95 virtual bool on_choke()
97 log_timestamp();
98 m_file << "<== CHOKE\n";
99 m_file.flush();
100 return false;
103 virtual bool on_unchoke()
105 log_timestamp();
106 m_file << "<== UNCHOKE\n";
107 m_file.flush();
108 return false;
111 virtual bool on_interested()
113 log_timestamp();
114 m_file << "<== INTERESTED\n";
115 m_file.flush();
116 return false;
119 virtual bool on_not_interested()
121 log_timestamp();
122 m_file << "<== NOT_INTERESTED\n";
123 m_file.flush();
124 return false;
127 virtual bool on_have(int index)
129 log_timestamp();
130 m_file << "<== HAVE [" << index << "]\n";
131 m_file.flush();
132 return false;
135 virtual bool on_bitfield(std::vector<bool> const& bitfield)
137 log_timestamp();
138 m_file << "<== BITFIELD\n";
139 m_file.flush();
140 return false;
143 virtual bool on_request(peer_request const& r)
145 log_timestamp();
146 m_file << "<== REQUEST [ piece: " << r.piece << " | s: " << r.start
147 << " | l: " << r.length << " ]\n";
148 m_file.flush();
149 return false;
152 virtual bool on_piece(peer_request const& r, char const*)
154 log_timestamp();
155 m_file << "<== PIECE [ piece: " << r.piece << " | s: " << r.start
156 << " | l: " << r.length << " ]\n";
157 m_file.flush();
158 return false;
161 virtual bool on_cancel(peer_request const& r)
163 log_timestamp();
164 m_file << "<== CANCEL [ piece: " << r.piece << " | s: " << r.start
165 << " | l: " << r.length << " ]\n";
166 m_file.flush();
167 return false;
170 // called when an extended message is received. If returning true,
171 // the message is not processed by any other plugin and if false
172 // is returned the next plugin in the chain will receive it to
173 // be able to handle it
174 virtual bool on_extended(int length
175 , int msg, buffer::const_interval body)
176 { return false; }
178 virtual bool on_unknown_message(int length, int msg
179 , buffer::const_interval body)
181 if (body.left() < length) return false;
182 log_timestamp();
183 m_file << "<== UNKNOWN [ msg: " << msg
184 << " | l: " << length << " ]\n";
185 m_file.flush();
186 return false;
189 virtual void on_piece_pass(int index)
191 log_timestamp();
192 m_file << "*** HASH PASSED *** [ piece: " << index << " ]\n";
193 m_file.flush();
196 virtual void on_piece_failed(int index)
198 log_timestamp();
199 m_file << "*** HASH FAILED *** [ piece: " << index << " ]\n";
200 m_file.flush();
203 private:
204 boost::filesystem::ofstream m_file;
207 struct logger_plugin : torrent_plugin
209 virtual boost::shared_ptr<peer_plugin> new_connection(
210 peer_connection* pc)
212 return boost::shared_ptr<peer_plugin>(new logger_peer_plugin(
213 pc->remote().address().to_string() + "_"
214 + boost::lexical_cast<std::string>(pc->remote().port()) + ".log"));
220 namespace libtorrent
223 boost::shared_ptr<torrent_plugin> create_logger_plugin(torrent*)
225 return boost::shared_ptr<torrent_plugin>(new logger_plugin());