From 7d41393e5e8c8493e4ee8e1befded0d92fb0f623 Mon Sep 17 00:00:00 2001 From: rakshasa Date: Fri, 22 Apr 2011 09:12:17 +0000 Subject: [PATCH] * Added 'file.append' command for easy writing of stats to a log file. * Cleaning up and prepearing the download / resource_manager for further improvements. git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@1214 e378c898-3ddf-0310-93e7-cc216c733640 --- doc/log_stats.plot | 24 ++-------------------- src/command_local.cc | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/doc/log_stats.plot b/doc/log_stats.plot index acd7a4a3..b1d3f1f4 100644 --- a/doc/log_stats.plot +++ b/doc/log_stats.plot @@ -1,25 +1,5 @@ #/bin/bash gnuplot << EOF -# 1) $system.time_seconds= -# -# 2) $throttle.global_up.rate= -# 3) $throttle.global_up.total= -# 4) $throttle.global_down.rate= -# 5) $throttle.global_down.total= -# -# 6) $throttle.unchoked_uploads= -# 7) $throttle.unchoked_downloads= -# 8) $view.size=active -# 9) $pieces.memory.current= -# 10) $pieces.memory.sync_queue= -# 11) $pieces.memory.block_count= -# 12) $pieces.stats.total_size= -# 13) $pieces.hash.queue_size= -# -# log.libtorrent = mincore_stats,(cat,"/foo/mincore_stats.",(system.pid)) -# -# schedule = log_bandwidth_stats,5,10,((execute,log_rtorrent.sh,((cat,/foo/bandwidth_stats.,((system.pid)))),((system.time_seconds)),((throttle.global_up.rate)),((throttle.global_up.total)),((throttle.global_down.rate)),((throttle.global_down.total)),((throttle.unchoked_uploads)),((throttle.unchoked_downloads)),((view.size,active)),((pieces.memory.current)),((pieces.memory.sync_queue)),((pieces.memory.block_count)),((pieces.stats.total_size)),((pieces.hash.queue_size)))) -# # 1) system.time_seconds= # # 2) throttle.global_up.rate @@ -33,7 +13,7 @@ gnuplot << EOF # 9) pieces.stats.total_size # 10) pieces.hash.queue_size # -# schedule = log_bandwidth_stats,5,10,((execute,log_rtorrent.sh,((cat,/foo/bandwidth_stats.,((system.pid)))),((system.time_seconds)),((throttle.global_up.rate)),((throttle.global_up.total)),((throttle.global_down.rate)),((throttle.global_down.total)),((pieces.memory.current)),((pieces.memory.sync_queue)),((pieces.memory.block_count)),((pieces.stats.total_size)),((pieces.hash.queue_size)))) +# schedule = log_bandwidth_stats,5,10,((file.append,((cat,/foo/bandwidth_stats.,((system.pid)))),((system.time_seconds)),((throttle.global_up.rate)),((throttle.global_up.total)),((throttle.global_down.rate)),((throttle.global_down.total)),((pieces.memory.current)),((pieces.memory.sync_queue)),((pieces.memory.block_count)),((pieces.stats.total_size)),((pieces.hash.queue_size)))) # # 1) system.time_seconds # @@ -45,7 +25,7 @@ gnuplot << EOF # 6) view.size,seeding # 7) view.size,active # -# schedule = log_peer_stats,5,10,((execute,log_rtorrent.sh,((cat,/foo/peer_stats.,((system.pid)))),((system.time_seconds)),((throttle.unchoked_uploads)),((throttle.unchoked_downloads)),((network.open_sockets)),((view.size,leeching)),((view.size,seeding)),((view.size,active)))) +# schedule = log_peer_stats,5,10,((file.append,((cat,/foo/peer_stats.,((system.pid)))),((system.time_seconds)),((throttle.unchoked_uploads)),((throttle.unchoked_downloads)),((network.open_sockets)),((view.size,leeching)),((view.size,seeding)),((view.size,active)))) set terminal png size 1024,600 diff --git a/src/command_local.cc b/src/command_local.cc index 26371709..96c29d7a 100644 --- a/src/command_local.cc +++ b/src/command_local.cc @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -252,6 +253,50 @@ log_vmmap_dump(const std::string& str) { return torrent::Object(); } +static const int file_print_use_space = 0x1; +static const int file_print_delim_space = 0x2; + +void +file_print_list(torrent::Object::list_const_iterator first, torrent::Object::list_const_iterator last, FILE* output, int flags) { + while (first != last) { + switch (first->type()) { + case torrent::Object::TYPE_STRING: + fprintf(output, " %s" + !(flags & file_print_use_space), first->as_string().c_str()); + break; + case torrent::Object::TYPE_VALUE: + fprintf(output, " %lli" + !(flags & file_print_use_space), first->as_value()); + break; + case torrent::Object::TYPE_LIST: + file_print_list(first->as_list().begin(), first->as_list().end(), output, 0); + break; + case torrent::Object::TYPE_NONE: + break; + default: + throw torrent::input_error("Invalid type."); + } + + flags |= (flags & file_print_delim_space) >> 1; + first++; + } +} + +torrent::Object +cmd_file_append(const torrent::Object::list_type& args) { + if (args.empty()) + throw torrent::input_error("Invalid number of arguments."); + + FILE* output = fopen(args.front().as_string().c_str(), "a"); + + if (output == NULL) + throw torrent::input_error("Could not append to file '" + args.front().as_string() + "': " + rak::error_number::current().c_str()); + + file_print_list(++args.begin(), args.end(), output, file_print_delim_space); + + fprintf(output, "\n"); + fclose(output); + return torrent::Object(); +} + void initialize_command_local() { torrent::ChunkManager* chunkManager = torrent::chunk_manager(); @@ -342,6 +387,8 @@ initialize_command_local() { CMD2_ANY_STRING_V("log.xmlrpc", std::bind(&ThreadWorker::set_xmlrpc_log, worker_thread, std::placeholders::_2)); CMD2_ANY_LIST ("log.libtorrent", std::bind(&apply_log_libtorrent, std::placeholders::_2)); + CMD2_ANY_LIST ("file.append", std::bind(&cmd_file_append, std::placeholders::_2)); + // TODO: Convert to new command types: *rpc::command_base::argument(0) = "placeholder.0"; *rpc::command_base::argument(1) = "placeholder.1";