Pārlūkot izejas kodu

Add --stderr option to redirect all stdout log output to stderr

Tatsuhiro Tsujikawa 9 gadi atpakaļ
vecāks
revīzija
db239c2853
7 mainītis faili ar 27 papildinājumiem un 0 dzēšanām
  1. 7 0
      src/OptionHandlerFactory.cc
  2. 7 0
      src/console.cc
  3. 3 0
      src/console.h
  4. 3 0
      src/option_processing.cc
  5. 2 0
      src/prefs.cc
  6. 2 0
      src/prefs.h
  7. 3 0
      src/usage_text.h

+ 7 - 0
src/OptionHandlerFactory.cc

@@ -679,6 +679,13 @@ std::vector<OptionHandler*> 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));

+ 7 - 0
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; }

+ 3 - 0
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();
 

+ 3 - 0
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) &&

+ 2 - 0
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

+ 2 - 0
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

+ 3 - 0
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