From fb129500980411c36524fa67ce663aa4b7a1dfe6 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 11 Apr 2010 21:20:38 +0000 Subject: [PATCH] fixed debug logging to not use up one extra file descriptor per connection --- include/libtorrent/debug.hpp | 60 +++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/include/libtorrent/debug.hpp b/include/libtorrent/debug.hpp index f08c4acde..4bd51c390 100644 --- a/include/libtorrent/debug.hpp +++ b/include/libtorrent/debug.hpp @@ -46,48 +46,52 @@ namespace libtorrent { // DEBUG API +#if TORRENT_USE_IOSTREAM + // all log streams share a single file descriptor + // and re-opens the file for each log line + static std::ofstream log_file; + static std::string open_filename; +#endif + struct logger { logger(std::string const& logpath, std::string const& filename - , int instance, bool append = true) + , int instance, bool append) { -#if TORRENT_USE_IOSTREAM - -#ifndef BOOST_NO_EXCEPTIONS - try - { -#endif - char log_name[256]; - snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance); - std::string dir(complete(combine_path(logpath, log_name))); - error_code ec; - if (!exists(dir)) create_directories(dir, ec); - m_file.open(combine_path(dir, filename).c_str() - , std::ios_base::out | (append ? std::ios_base::app : std::ios_base::out)); - *this << "\n\n\n*** starting log ***\n"; -#ifndef BOOST_NO_EXCEPTIONS - } - catch (std::exception& e) - { - std::cerr << "failed to create log '" << filename << "': " << e.what() << std::endl; - } -#endif -#endif + char log_name[512]; + snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance); + std::string dir(complete(combine_path(logpath, log_name))); + error_code ec; + if (!exists(dir)) create_directories(dir, ec); + m_filename = combine_path(dir, filename); + m_append = append; + *this << "\n\n\n*** starting log ***\n"; } +#if TORRENT_USE_IOSTREAM + void open() + { + if (open_filename == m_filename) return; + log_file.close(); + log_file.open(m_filename.c_str(), m_append ? std::ios_base::app : std::ios_base::out); + open_filename = m_filename; + if (!log_file.good()) + fprintf(stderr, "Failed to open logfile %s: %s\n", m_filename.c_str(), strerror(errno)); + } +#endif + template logger& operator<<(T const& v) { #if TORRENT_USE_IOSTREAM - m_file << v; - m_file.flush(); + open(); + log_file << v; #endif return *this; } -#if TORRENT_USE_IOSTREAM - std::ofstream m_file; -#endif + std::string m_filename; + bool m_append; }; }