fixed debug logging to not use up one extra file descriptor per connection
This commit is contained in:
parent
62eef91338
commit
fb12950098
|
@ -46,48 +46,52 @@ namespace libtorrent
|
||||||
{
|
{
|
||||||
// DEBUG API
|
// 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
|
struct logger
|
||||||
{
|
{
|
||||||
logger(std::string const& logpath, std::string const& filename
|
logger(std::string const& logpath, std::string const& filename
|
||||||
, int instance, bool append = true)
|
, int instance, bool append)
|
||||||
{
|
{
|
||||||
#if TORRENT_USE_IOSTREAM
|
char log_name[512];
|
||||||
|
snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance);
|
||||||
#ifndef BOOST_NO_EXCEPTIONS
|
std::string dir(complete(combine_path(logpath, log_name)));
|
||||||
try
|
error_code ec;
|
||||||
{
|
if (!exists(dir)) create_directories(dir, ec);
|
||||||
#endif
|
m_filename = combine_path(dir, filename);
|
||||||
char log_name[256];
|
m_append = append;
|
||||||
snprintf(log_name, sizeof(log_name), "libtorrent_logs%d", instance);
|
*this << "\n\n\n*** starting log ***\n";
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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 <class T>
|
template <class T>
|
||||||
logger& operator<<(T const& v)
|
logger& operator<<(T const& v)
|
||||||
{
|
{
|
||||||
#if TORRENT_USE_IOSTREAM
|
#if TORRENT_USE_IOSTREAM
|
||||||
m_file << v;
|
open();
|
||||||
m_file.flush();
|
log_file << v;
|
||||||
#endif
|
#endif
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if TORRENT_USE_IOSTREAM
|
std::string m_filename;
|
||||||
std::ofstream m_file;
|
bool m_append;
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue