00001
00002
00003
00004
00005
00006
00007
00008 #ifdef _MSC_VER
00009 #pragma warning(push, 1)
00010 #endif
00011
00012 #include <boost/shared_ptr.hpp>
00013 #include <boost/lexical_cast.hpp>
00014 #include <boost/filesystem/fstream.hpp>
00015 #include <boost/filesystem/convenience.hpp>
00016 #include <boost/date_time/posix_time/posix_time.hpp>
00017
00018 #ifdef _MSC_VER
00019 #pragma warning(pop)
00020 #endif
00021
00022 #include <vector>
00023
00024 #include "libtorrent/extensions/logger.hpp"
00025 #include "libtorrent/extensions.hpp"
00026 #include "libtorrent/entry.hpp"
00027 #include "libtorrent/peer_request.hpp"
00028 #include "libtorrent/peer_connection.hpp"
00029
00030 namespace libtorrent { namespace
00031 {
00032
00033 struct logger_peer_plugin : peer_plugin
00034 {
00035 logger_peer_plugin(std::string const& filename)
00036 {
00037 using namespace boost::filesystem;
00038 path dir(complete("libtorrent_ext_logs"));
00039 if (!exists(dir)) create_directories(dir);
00040 m_file.open(dir / filename, std::ios_base::out | std::ios_base::out);
00041 m_file << "\n\n\n";
00042 log_timestamp();
00043 m_file << "*** starting log ***\n";
00044 }
00045
00046 void log_timestamp()
00047 {
00048 using namespace boost::posix_time;
00049 std::string now(to_simple_string(second_clock::universal_time()));
00050 m_file << now << ": ";
00051 }
00052
00053
00054 virtual void add_handshake(entry&) {}
00055
00056
00057 virtual bool on_extension_handshake(entry const& h)
00058 {
00059 log_timestamp();
00060 m_file << "<== EXTENSION_HANDSHAKE\n";
00061 h.print(m_file);
00062 return true;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 virtual bool on_choke()
00072 {
00073 log_timestamp();
00074 m_file << "<== CHOKE\n";
00075 m_file.flush();
00076 return false;
00077 }
00078
00079 virtual bool on_unchoke()
00080 {
00081 log_timestamp();
00082 m_file << "<== UNCHOKE\n";
00083 m_file.flush();
00084 return false;
00085 }
00086
00087 virtual bool on_interested()
00088 {
00089 log_timestamp();
00090 m_file << "<== INTERESTED\n";
00091 m_file.flush();
00092 return false;
00093 }
00094
00095 virtual bool on_not_interested()
00096 {
00097 log_timestamp();
00098 m_file << "<== NOT_INTERESTED\n";
00099 m_file.flush();
00100 return false;
00101 }
00102
00103 virtual bool on_have(int index)
00104 {
00105 log_timestamp();
00106 m_file << "<== HAVE [" << index << "]\n";
00107 m_file.flush();
00108 return false;
00109 }
00110
00111 virtual bool on_bitfield(std::vector<bool> const& bitfield)
00112 {
00113 log_timestamp();
00114 m_file << "<== BITFIELD\n";
00115 m_file.flush();
00116 return false;
00117 }
00118
00119 virtual bool on_request(peer_request const& r)
00120 {
00121 log_timestamp();
00122 m_file << "<== REQUEST [ piece: " << r.piece << " | s: " << r.start
00123 << " | l: " << r.length << " ]\n";
00124 m_file.flush();
00125 return false;
00126 }
00127
00128 virtual bool on_piece(peer_request const& r, char const*)
00129 {
00130 log_timestamp();
00131 m_file << "<== PIECE [ piece: " << r.piece << " | s: " << r.start
00132 << " | l: " << r.length << " ]\n";
00133 m_file.flush();
00134 return false;
00135 }
00136
00137 virtual bool on_cancel(peer_request const& r)
00138 {
00139 log_timestamp();
00140 m_file << "<== CANCEL [ piece: " << r.piece << " | s: " << r.start
00141 << " | l: " << r.length << " ]\n";
00142 m_file.flush();
00143 return false;
00144 }
00145
00146
00147
00148
00149
00150 virtual bool on_extended(int length
00151 , int msg, buffer::const_interval body)
00152 { return false; }
00153
00154 virtual bool on_unknown_message(int length, int msg
00155 , buffer::const_interval body)
00156 {
00157 if (body.left() < length) return false;
00158 log_timestamp();
00159 m_file << "<== UNKNOWN [ msg: " << msg
00160 << " | l: " << length << " ]\n";
00161 m_file.flush();
00162 return false;
00163 }
00164
00165 virtual void on_piece_pass(int index)
00166 {
00167 log_timestamp();
00168 m_file << "*** HASH PASSED *** [ piece: " << index << " ]\n";
00169 m_file.flush();
00170 }
00171
00172 virtual void on_piece_failed(int index)
00173 {
00174 log_timestamp();
00175 m_file << "*** HASH FAILED *** [ piece: " << index << " ]\n";
00176 m_file.flush();
00177 }
00178
00179 private:
00180 boost::filesystem::ofstream m_file;
00181 };
00182
00183 struct logger_plugin : torrent_plugin
00184 {
00185 virtual boost::shared_ptr<peer_plugin> new_connection(
00186 peer_connection* pc)
00187 {
00188 return boost::shared_ptr<peer_plugin>(new logger_peer_plugin(
00189 pc->remote().address().to_string() + "_"
00190 + boost::lexical_cast<std::string>(pc->remote().port()) + ".log"));
00191 }
00192 };
00193
00194 } }
00195
00196 namespace libtorrent
00197 {
00198
00199 boost::shared_ptr<torrent_plugin> create_logger_plugin(torrent*)
00200 {
00201 return boost::shared_ptr<torrent_plugin>(new logger_plugin());
00202 }
00203
00204 }
00205