From 3d2fb6afbc2bd7abd23f7e169b722947b94b63aa Mon Sep 17 00:00:00 2001 From: rakshasa Date: Tue, 4 Nov 2014 19:48:55 +0900 Subject: [PATCH] Moved logging commands to a separate file. Conflicts: src/command_local.cc --- src/Makefile.am | 1 + src/command_helpers.cc | 2 + src/command_local.cc | 96 ------------------------- src/command_logging.cc | 155 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+), 96 deletions(-) create mode 100644 src/command_logging.cc diff --git a/src/Makefile.am b/src/Makefile.am index e0808137..16ead459 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,7 @@ libsub_root_a_SOURCES = \ command_helpers.h \ command_groups.cc \ command_local.cc \ + command_logging.cc \ command_network.cc \ command_peer.cc \ command_throttle.cc \ diff --git a/src/command_helpers.cc b/src/command_helpers.cc index 59892f13..54c0b35e 100644 --- a/src/command_helpers.cc +++ b/src/command_helpers.cc @@ -49,6 +49,7 @@ void initialize_command_file(); void initialize_command_ip(); void initialize_command_peer(); void initialize_command_local(); +void initialize_command_logging(); void initialize_command_network(); void initialize_command_groups(); void initialize_command_throttle(); @@ -63,6 +64,7 @@ initialize_commands() { initialize_command_network(); initialize_command_groups(); initialize_command_local(); + initialize_command_logging(); initialize_command_ui(); initialize_command_download(); initialize_command_file(); diff --git a/src/command_local.cc b/src/command_local.cc index 45c63b89..9b043368 100644 --- a/src/command_local.cc +++ b/src/command_local.cc @@ -48,7 +48,6 @@ #include #include #include -#include #include #include "core/download.h" @@ -79,42 +78,6 @@ apply_pieces_stats_total_size() { return size; } -torrent::Object -apply_log(const torrent::Object::string_type& arg, int logType) { - if (rpc::execFile.log_fd() != -1) { - switch (logType) { - case 0: ::close(rpc::execFile.log_fd()); rpc::execFile.set_log_fd(-1); break; - case 1: -// if (control->scgi()) { -// ::close(control->scgi()->log_fd()); -// control->scgi()->set_log_fd(-1); -// } - break; - default: break; - } - } - - if (!arg.empty()) { - int logFd = open(rak::path_expand(arg).c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); - - if (logFd < 0) - throw torrent::input_error("Could not open execute log file."); - - switch (logType) { - case 0: rpc::execFile.set_log_fd(logFd); break; -// case 1: if (control->scgi()) control->scgi()->set_log_fd(logFd); break; - default: break; - } - - control->core()->push_log("Opened log file."); - - } else { - control->core()->push_log("Closed log file."); - } - - return torrent::Object(); -} - torrent::Object system_hostname() { char buffer[1024]; @@ -212,27 +175,6 @@ group_insert(const torrent::Object::list_type& args) { return name; } -torrent::Object -log_vmmap_dump(const std::string& str) { - core::DownloadList* d_list = control->core()->download_list(); - std::vector all_mappings; - - for (core::DownloadList::iterator itr = d_list->begin(), last = d_list->end(); itr != last; itr++) { - std::vector tmp_mappings = torrent::chunk_list_mapping((*itr)->download()); - - all_mappings.insert(all_mappings.end(), tmp_mappings.begin(), tmp_mappings.end()); - } - - FILE* log_file = fopen(str.c_str(), "w"); - - for (std::vector::iterator itr = all_mappings.begin(), last = all_mappings.end(); itr != last; itr++) { - fprintf(log_file, "%8p-%8p [%5llxk]\n", itr->ptr, (char*)itr->ptr + itr->length, (long long unsigned int)(itr->length / 1024)); - } - - fclose(log_file); - return torrent::Object(); -} - static const int file_print_use_space = 0x1; static const int file_print_delim_space = 0x2; @@ -277,36 +219,6 @@ cmd_file_append(const torrent::Object::list_type& args) { return torrent::Object(); } -torrent::Object -apply_log_open_file(const torrent::Object::list_type& args) { - if (args.size() != 2) - throw torrent::input_error("Invalid number of arguments."); - - torrent::log_open_file_output(args.front().as_string().c_str(), - rak::path_expand(args.back().as_string()).c_str()); - return torrent::Object(); -} - -torrent::Object -apply_log_open_gz_file(const torrent::Object::list_type& args) { - if (args.size() != 2) - throw torrent::input_error("Invalid number of arguments."); - - torrent::log_open_gz_file_output(args.front().as_string().c_str(), - rak::path_expand(args.back().as_string()).c_str()); - return torrent::Object(); -} - -torrent::Object -apply_log_add_output(const torrent::Object::list_type& args) { - if (args.size() != 2) - throw torrent::input_error("Invalid number of arguments."); - - torrent::log_add_group_output(torrent::option_find_string(torrent::OPTION_LOG_GROUP, args.front().as_string().c_str()), - args.back().as_string().c_str()); - return torrent::Object(); -} - void initialize_command_local() { torrent::ChunkManager* chunkManager = torrent::chunk_manager(); @@ -401,14 +313,6 @@ initialize_command_local() { CMD2_EXECUTE ("execute.capture", rpc::ExecFile::flag_throw | rpc::ExecFile::flag_expand_tilde | rpc::ExecFile::flag_capture); CMD2_EXECUTE ("execute.capture_nothrow", rpc::ExecFile::flag_expand_tilde | rpc::ExecFile::flag_capture); - CMD2_ANY_LIST ("log.open_file", tr1::bind(&apply_log_open_file, tr1::placeholders::_2)); - CMD2_ANY_LIST ("log.open_gz_file", tr1::bind(&apply_log_open_gz_file, tr1::placeholders::_2)); - CMD2_ANY_LIST ("log.add_output", tr1::bind(&apply_log_add_output, tr1::placeholders::_2)); - - CMD2_ANY_STRING ("log.execute", tr1::bind(&apply_log, tr1::placeholders::_2, 0)); - CMD2_ANY_STRING ("log.vmmap.dump", tr1::bind(&log_vmmap_dump, tr1::placeholders::_2)); - CMD2_ANY_STRING_V("log.xmlrpc", tr1::bind(&ThreadWorker::set_xmlrpc_log, worker_thread, tr1::placeholders::_2)); - CMD2_ANY_LIST ("file.append", tr1::bind(&cmd_file_append, tr1::placeholders::_2)); // TODO: Convert to new command types: diff --git a/src/command_logging.cc b/src/command_logging.cc new file mode 100644 index 00000000..e84d1264 --- /dev/null +++ b/src/command_logging.cc @@ -0,0 +1,155 @@ +// rTorrent - BitTorrent client +// Copyright (C) 2005-2011, Jari Sundell +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// In addition, as a special exception, the copyright holders give +// permission to link the code of portions of this program with the +// OpenSSL library under certain conditions as described in each +// individual source file, and distribute linked combinations +// including the two. +// +// You must obey the GNU General Public License in all respects for +// all of the code used other than OpenSSL. If you modify file(s) +// with this exception, you may extend this exception to your version +// of the file(s), but you are not obligated to do so. If you do not +// wish to do so, delete this exception statement from your version. +// If you delete this exception statement from all source files in the +// program, then also delete it here. +// +// Contact: Jari Sundell +// +// Skomakerveien 33 +// 3185 Skoppum, NORWAY + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "core/download.h" +#include "core/download_list.h" +#include "core/manager.h" +#include "rak/path.h" +#include "rpc/parse_commands.h" + +#include "globals.h" +#include "control.h" +#include "command_helpers.h" + +torrent::Object +apply_log_open_file(const torrent::Object::list_type& args) { + if (args.size() != 2) + throw torrent::input_error("Invalid number of arguments."); + + torrent::log_open_file_output(args.front().as_string().c_str(), + rak::path_expand(args.back().as_string()).c_str()); + return torrent::Object(); +} + +torrent::Object +apply_log_open_gz_file(const torrent::Object::list_type& args) { + if (args.size() != 2) + throw torrent::input_error("Invalid number of arguments."); + + torrent::log_open_gz_file_output(args.front().as_string().c_str(), + rak::path_expand(args.back().as_string()).c_str()); + return torrent::Object(); +} + +torrent::Object +apply_log_add_output(const torrent::Object::list_type& args) { + if (args.size() != 2) + throw torrent::input_error("Invalid number of arguments."); + + torrent::log_add_group_output(torrent::option_find_string(torrent::OPTION_LOG_GROUP, args.front().as_string().c_str()), + args.back().as_string().c_str()); + return torrent::Object(); +} + +// TODO: Deprecated. +torrent::Object +apply_log(const torrent::Object::string_type& arg, int logType) { + if (rpc::execFile.log_fd() != -1) { + switch (logType) { + case 0: ::close(rpc::execFile.log_fd()); rpc::execFile.set_log_fd(-1); break; + case 1: +// if (control->scgi()) { +// ::close(control->scgi()->log_fd()); +// control->scgi()->set_log_fd(-1); +// } + break; + default: break; + } + } + + if (!arg.empty()) { + int logFd = open(rak::path_expand(arg).c_str(), O_WRONLY | O_APPEND | O_CREAT, 0644); + + if (logFd < 0) + throw torrent::input_error("Could not open execute log file."); + + switch (logType) { + case 0: rpc::execFile.set_log_fd(logFd); break; +// case 1: if (control->scgi()) control->scgi()->set_log_fd(logFd); break; + default: break; + } + + control->core()->push_log("Opened log file."); + + } else { + control->core()->push_log("Closed log file."); + } + + return torrent::Object(); +} + +torrent::Object +log_vmmap_dump(const std::string& str) { + core::DownloadList* d_list = control->core()->download_list(); + std::vector all_mappings; + + for (core::DownloadList::iterator itr = d_list->begin(), last = d_list->end(); itr != last; itr++) { + std::vector tmp_mappings = torrent::chunk_list_mapping((*itr)->download()); + + all_mappings.insert(all_mappings.end(), tmp_mappings.begin(), tmp_mappings.end()); + } + + FILE* log_file = fopen(str.c_str(), "w"); + + for (std::vector::iterator itr = all_mappings.begin(), last = all_mappings.end(); itr != last; itr++) { + fprintf(log_file, "%8p-%8p [%5llxk]\n", itr->ptr, (char*)itr->ptr + itr->length, (long long unsigned int)(itr->length / 1024)); + } + + fclose(log_file); + return torrent::Object(); +} + +void +initialize_command_logging() { + // CMD2_ANY_LIST ("log.open", std::bind(&apply_log_open, std::placeholders::_2)); + CMD2_ANY_LIST ("log.open_file", std::bind(&apply_log_open_file, std::placeholders::_2)); + CMD2_ANY_LIST ("log.open_gz_file", std::bind(&apply_log_open_gz_file, std::placeholders::_2)); + CMD2_ANY_LIST ("log.add_output", std::bind(&apply_log_add_output, std::placeholders::_2)); + + CMD2_ANY_STRING ("log.execute", std::bind(&apply_log, std::placeholders::_2, 0)); + CMD2_ANY_STRING ("log.vmmap.dump", std::bind(&log_vmmap_dump, std::placeholders::_2)); + CMD2_ANY_STRING_V("log.xmlrpc", std::bind(&ThreadWorker::set_xmlrpc_log, worker_thread, std::placeholders::_2)); +}