From db239c2853cb2982260be39a521fb5672c7e8245 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 27 Apr 2016 00:16:55 +0900 Subject: [PATCH] Add --stderr option to redirect all stdout log output to stderr --- src/OptionHandlerFactory.cc | 7 +++++++ src/console.cc | 7 +++++++ src/console.h | 3 +++ src/option_processing.cc | 3 +++ src/prefs.cc | 2 ++ src/prefs.h | 2 ++ src/usage_text.h | 3 +++ 7 files changed, 27 insertions(+) diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index fa467c25..595f9cd9 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -679,6 +679,13 @@ std::vector OptionHandlerFactory::createOptionHandlers() op->addTag(TAG_ADVANCED); handlers.push_back(op); } + { + OptionHandler* op(new BooleanOptionHandler( + PREF_STDERR, TEXT_STDERR, A2_V_FALSE, OptionHandler::OPT_ARG)); + + op->addTag(TAG_ADVANCED); + handlers.push_back(op); + } { OptionHandler* op( new NumberOptionHandler(PREF_STOP, TEXT_STOP, "0", 0, INT32_MAX)); diff --git a/src/console.cc b/src/console.cc index f203cd32..98d05278 100644 --- a/src/console.cc +++ b/src/console.cc @@ -47,6 +47,7 @@ namespace global { namespace { Console consoleCout; Console consoleCerr; +Console consoleCoutBackup; }; void initConsole(bool suppress) @@ -65,6 +66,12 @@ void initConsole(bool suppress) } } +void redirectStdoutToStderr() +{ + consoleCoutBackup = std::move(consoleCout); + consoleCout = consoleCerr; +} + const Console& cout() { return consoleCout; } const Console& cerr() { return consoleCerr; } diff --git a/src/console.h b/src/console.h index ac138765..710450eb 100644 --- a/src/console.h +++ b/src/console.h @@ -51,6 +51,9 @@ namespace global { // output sent to the console objects are discarded. void initConsole(bool suppress); +// stdout is redirected to stderr. +void redirectStdoutToStderr(); + const Console& cout(); const Console& cerr(); diff --git a/src/option_processing.cc b/src/option_processing.cc index 89ea9cf3..f6b12358 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -300,6 +300,9 @@ error_code::Value option_processing(Option& op, bool standalone, showUsage("", oparser, global::cerr()); return e.getErrorCode(); } + if (standalone && op.getAsBool(PREF_STDERR)) { + global::redirectStdoutToStderr(); + } if (standalone && !op.getAsBool(PREF_ENABLE_RPC) && #ifdef ENABLE_BITTORRENT op.blank(PREF_TORRENT_FILE) && diff --git a/src/prefs.cc b/src/prefs.cc index e3b439eb..eb1a1079 100644 --- a/src/prefs.cc +++ b/src/prefs.cc @@ -370,6 +370,8 @@ PrefPtr PREF_MIN_TLS_VERSION = makePref("min-tls-version"); PrefPtr PREF_SOCKET_RECV_BUFFER_SIZE = makePref("socket-recv-buffer-size"); // value: 1*digit PrefPtr PREF_MAX_MMAP_LIMIT = makePref("max-mmap-limit"); +// value: true | false +PrefPtr PREF_STDERR = makePref("stderr"); /** * FTP related preferences diff --git a/src/prefs.h b/src/prefs.h index d85fb6bb..68eb408f 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -324,6 +324,8 @@ extern PrefPtr PREF_MIN_TLS_VERSION; extern PrefPtr PREF_SOCKET_RECV_BUFFER_SIZE; // value: 1*digit extern PrefPtr PREF_MAX_MMAP_LIMIT; +// value: true | false +extern PrefPtr PREF_STDERR; /** * FTP related preferences diff --git a/src/usage_text.h b/src/usage_text.h index 808ff001..d87e909f 100644 --- a/src/usage_text.h +++ b/src/usage_text.h @@ -1085,5 +1085,8 @@ " size of those files. If file size is strictly\n" \ " greater than the size specified in this option,\n" \ " mmap will be disabled.") +#define TEXT_STDERR \ + _(" --stderr[=true|false] Redirect all console output that would be\n" \ + " otherwise printed in stdout to stderr.") // clang-format on