summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-04-27 00:16:55 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2016-04-27 00:16:55 +0900
commitdb239c2853cb2982260be39a521fb5672c7e8245 (patch)
tree5de4ff82ab1c95bc64db305987c18641d8690b2f
parentaa863fa4d11e93f8363e70cda0e558776485a7b9 (diff)
Add --stderr option to redirect all stdout log output to stderr
-rw-r--r--src/OptionHandlerFactory.cc7
-rw-r--r--src/console.cc7
-rw-r--r--src/console.h3
-rw-r--r--src/option_processing.cc3
-rw-r--r--src/prefs.cc2
-rw-r--r--src/prefs.h2
-rw-r--r--src/usage_text.h3
7 files changed, 27 insertions, 0 deletions
diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc
index fa467c25..595f9cd9 100644
--- a/src/OptionHandlerFactory.cc
+++ b/src/OptionHandlerFactory.cc
@@ -680,6 +680,13 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
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));
op->addTag(TAG_ADVANCED);
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