From 45781e8dbf4732cceb057629d02c788594618b32 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Mon, 17 Oct 2005 19:19:48 +0000 Subject: [PATCH] * Got rid of some additional internal sigc++ calls. * Changed print_hhmmss/ddmmyyyy to use a char buffer. * Added error messages for failed torrent http/file downloads which includes the url/filename. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@581 e378c898-3ddf-0310-93e7-cc216c733640 --- rak/functional.h | 23 ++++++++++++++++++ src/core/download_factory.cc | 2 ++ src/display/utils.cc | 38 ++++++++++++++++-------------- src/display/utils.h | 6 +++-- src/display/window_log.cc | 10 ++++---- src/display/window_log_complete.cc | 10 ++++---- src/display/window_peer_info.cc | 9 +++++-- 7 files changed, 68 insertions(+), 30 deletions(-) diff --git a/rak/functional.h b/rak/functional.h index 31c09c0a..1646ab19 100644 --- a/rak/functional.h +++ b/rak/functional.h @@ -393,6 +393,23 @@ private: Function m_function; }; +template +class const_mem_fn1 { +public: + typedef Ret (Object::*Function)(Arg1) const; + + const_mem_fn1() : m_object(NULL) {} + const_mem_fn1(Object* o, Function f) : m_object(o), m_function(f) {} + + bool is_valid() const { return m_object; } + + Ret operator () (Arg1 a1) { return (m_object->*m_function)(a1); } + +private: + Object* m_object; + Function m_function; +}; + template class mem_fn2 { public: @@ -445,6 +462,12 @@ make_mem_fn(Object* o, Ret (Object::*f)(Arg1)) { return mem_fn1(o, f); } +template +inline const_mem_fn1 +make_mem_fn(Object* o, Ret (Object::*f)(Arg1) const) { + return const_mem_fn1(o, f); +} + template inline mem_fn2 make_mem_fn(Object* o, Ret (Object::*f)(Arg1, Arg2)) { diff --git a/src/core/download_factory.cc b/src/core/download_factory.cc index 37955f7e..7425fd6b 100644 --- a/src/core/download_factory.cc +++ b/src/core/download_factory.cc @@ -160,6 +160,8 @@ DownloadFactory::receive_failed(const std::string& msg) { throw std::logic_error("DownloadFactory::receive_success() called on an object with m_stream == NULL"); // Add message to log. + m_manager->get_log_important().push_front(msg + ": \"" + m_uri + "\""); + m_manager->get_log_complete().push_front(msg + ": \"" + m_uri + "\""); m_slotFinished(); } diff --git a/src/display/utils.cc b/src/display/utils.cc index 40ab09f9..083e1f7f 100644 --- a/src/display/utils.cc +++ b/src/display/utils.cc @@ -36,6 +36,7 @@ #include "config.h" +#include #include #include #include @@ -48,6 +49,16 @@ namespace display { +char* +print_string(char* buf, unsigned int length, char* str) { + // We don't have any nice simple functions for copying strings that + // return the end address. + while (length-- != 0 && *str != '\0') + *(buf++) = *(str++); + + return buf; +} + char* print_download_title(char* buf, unsigned int length, core::Download* d) { return buf + std::max(0, snprintf(buf, length, "%s", @@ -102,37 +113,28 @@ print_download_status(char* buf, unsigned int length, core::Download* d) { return buf; } -std::string -print_hhmmss(utils::Timer t) { - time_t tv_sec = static_cast(t.tval().tv_sec); - std::tm *u = std::localtime(&tv_sec); +char* +print_hhmmss(char* buf, unsigned int length, time_t t) { + std::tm *u = std::localtime(&t); if (u == NULL) return "inv_time"; - std::stringstream str; - str.fill('0'); - - str << std::setw(2) << u->tm_hour << ':' << std::setw(2) << u->tm_min << ':' << std::setw(2) << u->tm_sec; + unsigned int s = snprintf(buf, length, "%02u:%02u:%02u", u->tm_hour, u->tm_min, u->tm_sec); - return str.str(); + return buf + std::min(s, length); } -std::string -print_ddmmyyyy(time_t t) { +char* +print_ddmmyyyy(char* buf, unsigned int length, time_t t) { std::tm *u = std::gmtime(&t); if (u == NULL) return "inv_time"; - std::stringstream str; - str.fill('0'); - - str << std::setw(2) << u->tm_mday << '/' - << std::setw(2) << (u->tm_mon + 1) << '/' - << std::setw(4) << (1900 + u->tm_year); + unsigned int s = snprintf(buf, length, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)); - return str.str(); + return buf + std::min(s, length); } } diff --git a/src/display/utils.h b/src/display/utils.h index 0293a682..ba674837 100644 --- a/src/display/utils.h +++ b/src/display/utils.h @@ -50,12 +50,14 @@ namespace utils { namespace display { +char* print_string(char* buf, unsigned int length, char* str); + char* print_download_title(char* buf, unsigned int length, core::Download* d); char* print_download_info(char* buf, unsigned int length, core::Download* d); char* print_download_status(char* buf, unsigned int length, core::Download* d); -std::string print_hhmmss(utils::Timer t); -std::string print_ddmmyyyy(time_t t); +char* print_hhmmss(char* buf, unsigned int length, time_t t); +char* print_ddmmyyyy(char* buf, unsigned int length, time_t t); } diff --git a/src/display/window_log.cc b/src/display/window_log.cc index e26d1459..cdd9f383 100644 --- a/src/display/window_log.cc +++ b/src/display/window_log.cc @@ -69,10 +69,12 @@ WindowLog::redraw() { int pos = 0; - for (core::Log::iterator itr = m_log->begin(), end = find_older(); itr != end && pos < m_canvas->get_height(); ++itr) - m_canvas->print(0, pos++, "(%s) %s", - print_hhmmss(itr->first).c_str(), - itr->second.c_str()); + for (core::Log::iterator itr = m_log->begin(), end = find_older(); itr != end && pos < m_canvas->get_height(); ++itr) { + char buffer[16]; + print_hhmmss(buffer, 16, static_cast(itr->first.seconds())); + + m_canvas->print(0, pos++, "(%s) %s", buffer, itr->second.c_str()); + } } void diff --git a/src/display/window_log_complete.cc b/src/display/window_log_complete.cc index ceed3a6b..df8482cb 100644 --- a/src/display/window_log_complete.cc +++ b/src/display/window_log_complete.cc @@ -69,10 +69,12 @@ WindowLogComplete::redraw() { m_canvas->print(std::max(0, (int)m_canvas->get_width() / 2 - 5), pos++, "*** Log ***"); - for (core::Log::iterator itr = m_log->begin(), e = m_log->end(); itr != e && pos < m_canvas->get_height(); ++itr) - m_canvas->print(0, pos++, "(%s) %s", - print_hhmmss(itr->first).c_str(), - itr->second.c_str()); + for (core::Log::iterator itr = m_log->begin(), e = m_log->end(); itr != e && pos < m_canvas->get_height(); ++itr) { + char buffer[16]; + print_hhmmss(buffer, 16, static_cast(itr->first.seconds())); + + m_canvas->print(0, pos++, "(%s) %s", buffer, itr->second.c_str()); + } } void diff --git a/src/display/window_peer_info.cc b/src/display/window_peer_info.cc index 70ddea5b..cab2c52a 100644 --- a/src/display/window_peer_info.cc +++ b/src/display/window_peer_info.cc @@ -69,8 +69,13 @@ WindowPeerInfo::redraw() { d.get_chunks_done(), d.get_chunks_total(), d.get_chunks_size()); - m_canvas->print(0, y++, "Created: %s", - print_ddmmyyyy(static_cast(d.get_creation_date())).c_str()); + + char buffer[32], *position; + position = print_ddmmyyyy(buffer, 32, static_cast(d.get_creation_date())); + position = print_string(position, buffer + 32 - position, " "); + position = print_hhmmss(position, buffer + 32 - position, static_cast(d.get_creation_date())); + + m_canvas->print(0, y++, "Created: %s", buffer); y++;