From a186932c0311f819228717e7be1c491063289a63 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Mon, 12 Mar 2012 04:09:18 +0900 Subject: [PATCH] Use log_buffer for console logs. --- src/core/manager.cc | 9 +++++++-- src/core/manager.h | 7 +++++++ src/display/window_log.cc | 26 +++++++++++++++----------- src/display/window_log.h | 14 +++++--------- src/main.cc | 12 +++++------- src/ui/download_list.cc | 2 +- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/core/manager.cc b/src/core/manager.cc index 5ccf572a..417187c6 100644 --- a/src/core/manager.cc +++ b/src/core/manager.cc @@ -137,13 +137,18 @@ Manager::handshake_log(const sockaddr* sa, int msg, int err, const torrent::Hash void Manager::push_log(const char* msg) { + if (!pthread_equal(pthread_self(), torrent::main_thread()->pthread())) + throw torrent::internal_error("Manager::push_log(...): Cannot call this function from other threads than 'main'."); + m_logImportant.push_front(msg); m_logComplete.push_front(msg); + + m_log_important->lock_and_push_log(msg, strlen(msg), 0); } Manager::Manager() : - m_hashingView(NULL) -// m_pollManager(NULL) { + m_hashingView(NULL), + m_log_important(torrent::log_open_log_buffer("important")) { m_downloadStore = new DownloadStore(); m_downloadList = new DownloadList(); diff --git a/src/core/manager.h b/src/core/manager.h index 610e8f99..be2d03bc 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -40,6 +40,7 @@ #include #include +#include #include #include "download_list.h" @@ -88,6 +89,9 @@ public: Log& get_log_important() { return m_logImportant; } Log& get_log_complete() { return m_logComplete; } + torrent::log_buffer* log_important() { return m_log_important; } + // torrent::log_buffer* log_complete() { return m_log_complete; } + ThrottleMap& throttles() { return m_throttles; } torrent::ThrottlePair get_throttle(const std::string& name); @@ -154,6 +158,9 @@ private: Log m_logImportant; Log m_logComplete; + + torrent::log_buffer* m_log_important; + // torrent::log_buffer* m_log_complete; }; // Meh, cleanup. diff --git a/src/display/window_log.cc b/src/display/window_log.cc index 3b36dbe3..e6c15e0d 100644 --- a/src/display/window_log.cc +++ b/src/display/window_log.cc @@ -37,6 +37,8 @@ #include "config.h" #include +#include +#include #include "canvas.h" #include "utils.h" @@ -44,37 +46,38 @@ namespace display { -WindowLog::WindowLog(core::Log* l) : +WindowLog::WindowLog(torrent::log_buffer* l) : Window(new Canvas, 0, 0, 0, extent_full, extent_static), m_log(l) { m_taskUpdate.slot() = std::tr1::bind(&WindowLog::receive_update, this); - // We're trying out scheduled tasks instead. - m_connUpdate = l->signal_update().connect(sigc::mem_fun(*this, &WindowLog::receive_update)); + unsigned int signal_index = torrent::main_thread()->signal_bitfield()->add_signal(std::tr1::bind(&WindowLog::receive_update, this)); + + m_log->lock_and_set_update_slot(std::tr1::bind(&torrent::thread_base::send_event_signal, torrent::main_thread(), signal_index, false)); } WindowLog::~WindowLog() { priority_queue_erase(&taskScheduler, &m_taskUpdate); - m_connUpdate.disconnect(); } WindowLog::iterator WindowLog::find_older() { - return m_log->find_older(cachedTime - rak::timer::from_seconds(60)); + return m_log->find_older(cachedTime.seconds() - 60); + // return m_log->begin(); } void WindowLog::redraw() { m_canvas->erase(); - int pos = m_canvas->height(); + int pos = 0; - for (core::Log::iterator itr = m_log->begin(), last = find_older(); itr != last && pos > 0; ++itr, --pos) { + for (iterator itr = find_older(), last = m_log->end(); itr != last && pos < m_canvas->height(); ++itr, ++pos) { char buffer[16]; - print_hhmmss_local(buffer, buffer + 16, static_cast(itr->first.seconds())); + print_hhmmss_local(buffer, buffer + 16, static_cast(itr->timestamp)); - m_canvas->print(0, pos - 1, "(%s) %s", buffer, itr->second.c_str()); + m_canvas->print(0, pos, "(%s) %s", buffer, itr->message.c_str()); } } @@ -86,11 +89,12 @@ WindowLog::receive_update() { return; iterator itr = find_older(); - extent_type height = std::min(std::distance(m_log->begin(), itr), (std::iterator_traits::difference_type)10); + extent_type height = std::min(std::distance(itr, (iterator)m_log->end()), (std::iterator_traits::difference_type)10); if (height != m_maxHeight) { m_minHeight = height != 0 ? 1 : 0; m_maxHeight = height; + mark_dirty(); m_slotAdjust(); } else { @@ -100,7 +104,7 @@ WindowLog::receive_update() { priority_queue_erase(&taskScheduler, &m_taskUpdate); if (height != 0) - priority_queue_insert(&taskScheduler, &m_taskUpdate, (cachedTime + rak::timer::from_seconds(30)).round_seconds()); + priority_queue_insert(&taskScheduler, &m_taskUpdate, (cachedTime + rak::timer::from_seconds(5)).round_seconds()); } } diff --git a/src/display/window_log.h b/src/display/window_log.h index 35769800..4b5dba14 100644 --- a/src/display/window_log.h +++ b/src/display/window_log.h @@ -37,9 +37,7 @@ #ifndef RTORRENT_DISPLAY_WINDOW_LOG_H #define RTORRENT_DISPLAY_WINDOW_LOG_H -#include - -#include "core/log.h" +#include #include "window.h" @@ -47,9 +45,9 @@ namespace display { class WindowLog : public Window { public: - typedef core::Log::iterator iterator; + typedef torrent::log_buffer::const_iterator iterator; - WindowLog(core::Log* l); + WindowLog(torrent::log_buffer* l); ~WindowLog(); virtual void redraw(); @@ -59,10 +57,8 @@ public: private: inline iterator find_older(); - core::Log* m_log; - sigc::connection m_connUpdate; - - rak::priority_item m_taskUpdate; + torrent::log_buffer* m_log; + rak::priority_item m_taskUpdate; }; } diff --git a/src/main.cc b/src/main.cc index 773a6264..0af31ce9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -183,6 +183,9 @@ main(int argc, char** argv) { cachedTime = rak::timer::current(); + // Initialize logging: + torrent::log_initialize(); + control = new Control; worker_thread = new ThreadWorker(); @@ -205,6 +208,8 @@ main(int argc, char** argv) { // to process new non-socket events. SignalHandler::set_handler(SIGUSR1, sigc::ptr_fun(&do_nothing)); + torrent::log_add_group_output(torrent::LOG_INFO, "important"); + torrent::Poll::slot_create_poll() = std::tr1::bind(&core::create_poll); torrent::initialize(); @@ -215,13 +220,6 @@ main(int argc, char** argv) { // torrent::ConnectionManager* are valid etc. initialize_commands(); - // Initialize logging: - torrent::log_initialize(); - torrent::log_open_output("console", std::bind(&core::Manager::push_log, control->core(), std::placeholders::_1)); - torrent::log_add_group_output(torrent::LOG_INFO, "console"); - - lt_log_print(torrent::LOG_INFO, "Started logging to 'console'."); - if (OptionParser::has_flag('D', argc, argv)) { rpc::call_command_set_value("method.use_deprecated.set", false); lt_log_print(torrent::LOG_WARN, "Disabled deprecated commands."); diff --git a/src/ui/download_list.cc b/src/ui/download_list.cc index 224d4d11..03642cdd 100644 --- a/src/ui/download_list.cc +++ b/src/ui/download_list.cc @@ -77,7 +77,7 @@ DownloadList::DownloadList() : m_uiArray[DISPLAY_LOG] = new ElementLogComplete(&control->core()->get_log_complete()); m_uiArray[DISPLAY_STRING_LIST] = new ElementStringList(); - m_windowLog = new WLog(&control->core()->get_log_important()); + m_windowLog = new WLog(control->core()->log_important()); setup_keys(); }