merge verbose logging feature from libtorrent_aio

This commit is contained in:
Arvid Norberg 2012-10-06 23:45:36 +00:00
parent b81d1bfe08
commit 35032a6950
4 changed files with 54 additions and 9 deletions

View File

@ -1121,6 +1121,9 @@ namespace libtorrent
// whe shutting down process // whe shutting down process
std::list<boost::shared_ptr<tracker_logger> > m_tracker_loggers; std::list<boost::shared_ptr<tracker_logger> > m_tracker_loggers;
std::string get_log_path() const
{ return m_logpath; }
std::string m_logpath; std::string m_logpath;
private: private:

View File

@ -145,16 +145,45 @@ namespace libtorrent
{ {
char log_name[512]; char log_name[512];
snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance); 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; error_code ec;
if (!exists(dir)) create_directories(dir, ec); if (!exists(parent_path(dir)))
m_filename = combine_path(dir, filename); create_directories(parent_path(dir), ec);
m_filename = dir;
mutex::scoped_lock l(file_mutex); mutex::scoped_lock l(file_mutex);
open(!append); open(!append);
log_file << "\n\n\n*** starting log ***\n"; 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 #if TORRENT_USE_IOSTREAM
void open(bool truncate) void open(bool truncate)
{ {

View File

@ -242,12 +242,13 @@ namespace libtorrent
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING
error_code ec; error_code ec;
TORRENT_ASSERT(m_socket->remote_endpoint(ec) == m_remote || ec); TORRENT_ASSERT(m_socket->remote_endpoint(ec) == m_remote || ec);
std::string torrent_name; std::string log_name = m_remote.address().to_string(ec) + "_"
if (t) torrent_name = to_hex(t->info_hash().to_string()); + to_string(m_remote.port()).elems;
m_logger = m_ses.create_log(torrent_name + '_' if (t) log_name = combine_path(to_hex(t->info_hash().to_string())
+ m_remote.address().to_string(ec) + "_" , log_name);
+ to_string(m_remote.port()).elems, m_ses.listen_port());
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]" peer_log("%s [ ep: %s type: %s seed: %d p: %p local: %s]"
, m_outgoing ? ">>> OUTGOING_CONNECTION" : "<<< INCOMING CONNECTION" , m_outgoing ? ">>> OUTGOING_CONNECTION" : "<<< INCOMING CONNECTION"
, print_endpoint(m_remote).c_str() , print_endpoint(m_remote).c_str()
@ -991,6 +992,17 @@ namespace libtorrent
boost::weak_ptr<torrent> wpt = m_ses.find_torrent(ih); boost::weak_ptr<torrent> wpt = m_ses.find_torrent(ih);
boost::shared_ptr<torrent> t = wpt.lock(); boost::shared_ptr<torrent> 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 (t && t->is_aborted())
{ {
#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_ERROR_LOGGING

View File

@ -4754,8 +4754,9 @@ namespace aux {
boost::shared_ptr<logger> session_impl::create_log(std::string const& name boost::shared_ptr<logger> session_impl::create_log(std::string const& name
, int instance, bool append) , int instance, bool append)
{ {
error_code ec;
// current options are file_logger, cout_logger and null_logger // current options are file_logger, cout_logger and null_logger
return boost::shared_ptr<logger>(new logger(m_logpath, name + ".log", instance, append)); return boost::shared_ptr<logger>(new logger(m_logpath, name, instance, append));
} }
#endif #endif