diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 978d0adbb..aeebdfd10 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -1121,6 +1121,9 @@ namespace libtorrent // whe shutting down process std::list > m_tracker_loggers; + std::string get_log_path() const + { return m_logpath; } + std::string m_logpath; private: diff --git a/include/libtorrent/debug.hpp b/include/libtorrent/debug.hpp index ddaf8d03d..eef2862b6 100644 --- a/include/libtorrent/debug.hpp +++ b/include/libtorrent/debug.hpp @@ -145,16 +145,45 @@ namespace libtorrent { char log_name[512]; snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance); - std::string dir(complete(combine_path(logpath, log_name))); + std::string dir(complete(combine_path(combine_path(logpath, log_name), filename)) + ".log"); error_code ec; - if (!exists(dir)) create_directories(dir, ec); - m_filename = combine_path(dir, filename); + if (!exists(parent_path(dir))) + create_directories(parent_path(dir), ec); + m_filename = dir; mutex::scoped_lock l(file_mutex); open(!append); log_file << "\n\n\n*** starting log ***\n"; } + void move_log_file(std::string const& logpath, std::string const& new_name, int instance) + { + mutex::scoped_lock l(file_mutex); + if (open_filename == m_filename) + { + log_file.close(); + open_filename.clear(); + } + + char log_name[512]; + snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance); + std::string dir(combine_path(combine_path(complete(logpath), log_name), new_name) + ".log"); + + error_code ec; + create_directories(parent_path(dir), ec); + + if (ec) + fprintf(stderr, "Failed to create logfile directory %s: %s\n" + , parent_path(dir).c_str(), ec.message().c_str()); + ec.clear(); + rename(m_filename, dir, ec); + if (ec) + fprintf(stderr, "Failed to move logfile %s: %s\n" + , parent_path(dir).c_str(), ec.message().c_str()); + + m_filename = dir; + } + #if TORRENT_USE_IOSTREAM void open(bool truncate) { diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 603c179fc..0d1366919 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -242,12 +242,13 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING error_code ec; TORRENT_ASSERT(m_socket->remote_endpoint(ec) == m_remote || ec); - std::string torrent_name; - if (t) torrent_name = to_hex(t->info_hash().to_string()); + std::string log_name = m_remote.address().to_string(ec) + "_" + + to_string(m_remote.port()).elems; - m_logger = m_ses.create_log(torrent_name + '_' - + m_remote.address().to_string(ec) + "_" - + to_string(m_remote.port()).elems, m_ses.listen_port()); + if (t) log_name = combine_path(to_hex(t->info_hash().to_string()) + , log_name); + + m_logger = m_ses.create_log(log_name, m_ses.listen_port()); peer_log("%s [ ep: %s type: %s seed: %d p: %p local: %s]" , m_outgoing ? ">>> OUTGOING_CONNECTION" : "<<< INCOMING CONNECTION" , print_endpoint(m_remote).c_str() @@ -991,6 +992,17 @@ namespace libtorrent boost::weak_ptr wpt = m_ses.find_torrent(ih); boost::shared_ptr t = wpt.lock(); +#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING + // now that we know which torrent this peer belongs + // to. Move the log file into its directory + + error_code ec; + std::string log_name = combine_path(to_hex(ih.to_string()) + , m_remote.address().to_string(ec) + "_" + + to_string(m_remote.port()).elems); + m_logger->move_log_file(m_ses.get_log_path(), log_name, m_ses.listen_port()); +#endif + if (t && t->is_aborted()) { #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 7080bb3f6..586039673 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4754,8 +4754,9 @@ namespace aux { boost::shared_ptr session_impl::create_log(std::string const& name , int instance, bool append) { + error_code ec; // current options are file_logger, cout_logger and null_logger - return boost::shared_ptr(new logger(m_logpath, name + ".log", instance, append)); + return boost::shared_ptr(new logger(m_logpath, name, instance, append)); } #endif