diff --git a/src/console/PhutilConsoleFormatter.php b/src/console/PhutilConsoleFormatter.php --- a/src/console/PhutilConsoleFormatter.php +++ b/src/console/PhutilConsoleFormatter.php @@ -22,23 +22,38 @@ public static function getDisableANSI() { if (self::$disableANSI === null) { - $term = phutil_utf8_strtolower(getenv('TERM')); - // ansicon enables ANSI support on Windows - if (!$term && getenv('ANSICON')) { - $term = 'ansi'; + self::$disableANSI = self::newShouldDisableAnsi(); + } + return self::$disableANSI; + } + + private static function newShouldDisableANSI() { + $term = phutil_utf8_strtolower(getenv('TERM')); + + // ansicon enables ANSI support on Windows + if (!$term && getenv('ANSICON')) { + $term = 'ansi'; + } + + + if (phutil_is_windows()) { + if ($term !== 'cygwin' && $term !== 'ansi') { + return true; } + } + + $stdout = PhutilSystem::getStdoutHandle(); + if ($stdout === null) { + return true; + } - if (phutil_is_windows() && $term !== 'cygwin' && $term !== 'ansi') { - self::$disableANSI = true; - } else if (!defined('STDOUT')) { - self::$disableANSI = true; - } else if (function_exists('posix_isatty') && !posix_isatty(STDOUT)) { - self::$disableANSI = true; - } else { - self::$disableANSI = false; + if (function_exists('posix_isatty')) { + if (!posix_isatty($stdout)) { + return true; } } - return self::$disableANSI; + + return false; } public static function formatString($format /* ... */) { diff --git a/src/filesystem/PhutilErrorLog.php b/src/filesystem/PhutilErrorLog.php --- a/src/filesystem/PhutilErrorLog.php +++ b/src/filesystem/PhutilErrorLog.php @@ -94,7 +94,7 @@ if (strlen($message)) { $message = tsprintf("%B\n", $message); - @fwrite(STDERR, $message); + PhutilSystem::writeStderr($message); } } diff --git a/src/future/exec/PhutilExecPassthru.php b/src/future/exec/PhutilExecPassthru.php --- a/src/future/exec/PhutilExecPassthru.php +++ b/src/future/exec/PhutilExecPassthru.php @@ -53,10 +53,14 @@ if ($is_write) { $stdin_spec = array('pipe', 'r'); } else { - $stdin_spec = STDIN; + $stdin_spec = PhutilSystem::getStdinHandle(); } - $spec = array($stdin_spec, STDOUT, STDERR); + $spec = array( + $stdin_spec, + PhutilSystem::getStdoutHandle(), + PhutilSystem::getStderrHandle(), + ); $pipes = array(); $unmasked_command = $command->getUnmaskedString(); diff --git a/src/log/ArcanistLogEngine.php b/src/log/ArcanistLogEngine.php --- a/src/log/ArcanistLogEngine.php +++ b/src/log/ArcanistLogEngine.php @@ -19,7 +19,7 @@ } private function writeBytes($bytes) { - fprintf(STDERR, '%s', $bytes); + PhutilSystem::writeStderr($bytes); return $this; } diff --git a/src/parser/ArcanistBaseCommitParser.php b/src/parser/ArcanistBaseCommitParser.php --- a/src/parser/ArcanistBaseCommitParser.php +++ b/src/parser/ArcanistBaseCommitParser.php @@ -33,7 +33,7 @@ private function log($message) { if ($this->verbose) { - fwrite(STDERR, $message."\n"); + PhutilSystem::writeStderr($message."\n"); } } diff --git a/src/parser/argument/PhutilArgumentParser.php b/src/parser/argument/PhutilArgumentParser.php --- a/src/parser/argument/PhutilArgumentParser.php +++ b/src/parser/argument/PhutilArgumentParser.php @@ -805,7 +805,7 @@ private function logMessage($message) { - fwrite(STDERR, $message); + PhutilSystem::writeStderr($message); } diff --git a/src/progress/PhutilConsoleProgressSink.php b/src/progress/PhutilConsoleProgressSink.php --- a/src/progress/PhutilConsoleProgressSink.php +++ b/src/progress/PhutilConsoleProgressSink.php @@ -110,6 +110,6 @@ } private function printLine($line) { - fprintf(STDERR, '%s', $line); + PhutilSystem::writeStderr($line); } } diff --git a/src/symbols/PhutilSymbolLoader.php b/src/symbols/PhutilSymbolLoader.php --- a/src/symbols/PhutilSymbolLoader.php +++ b/src/symbols/PhutilSymbolLoader.php @@ -296,11 +296,12 @@ // library without breaking library startup. if ($should_continue) { // We may not have `pht()` yet. - fprintf( - STDERR, + $message = sprintf( "%s: %s\n", 'IGNORING CLASS LOAD FAILURE', $caught->getMessage()); + + @file_put_contents('php://stderr', $message); } else { throw $caught; } diff --git a/src/upload/ArcanistFileUploader.php b/src/upload/ArcanistFileUploader.php --- a/src/upload/ArcanistFileUploader.php +++ b/src/upload/ArcanistFileUploader.php @@ -313,7 +313,7 @@ * @task internal */ private function writeStatus($message) { - fwrite(STDERR, $message."\n"); + PhutilSystem::writeStderr($message."\n"); } } diff --git a/src/utils/PhutilSystem.php b/src/utils/PhutilSystem.php --- a/src/utils/PhutilSystem.php +++ b/src/utils/PhutilSystem.php @@ -3,10 +3,76 @@ /** * Interact with the operating system. * + * @task stdio Interacting with Standard I/O * @task memory Interacting with System Memory */ final class PhutilSystem extends Phobject { + private static $stdin = false; + private static $stderr = false; + private static $stdout = false; + + /** + * @task stdio + */ + public static function getStdinHandle() { + if (self::$stdin === false) { + self::$stdin = self::newStdioHandle('php://stdin'); + } + + return self::$stdin; + } + + /** + * @task stdio + */ + public static function getStdoutHandle() { + if (self::$stdout === false) { + self::$stdout = self::newStdioHandle('php://stdout'); + } + + return self::$stdout; + } + + /** + * @task stdio + */ + public static function getStderrHandle() { + if (self::$stderr === false) { + self::$stderr = self::newStdioHandle('php://stderr'); + } + + return self::$stderr; + } + + /** + * @task stdio + */ + public static function writeStderr($message) { + $stderr = self::getStderrHandle(); + + if ($stderr === null) { + return; + } + + $message = phutil_string_cast($message); + @fwrite($stderr, $message); + } + + + /** + * @task stdio + */ + private static function newStdioHandle($ref) { + $ignored_mode = ''; + + $handle = @fopen($ref, $ignored_mode); + if ($handle === false) { + return null; + } + + return $handle; + } /** * Get information about total and free memory on the system. diff --git a/src/workflow/ArcanistDiffWorkflow.php b/src/workflow/ArcanistDiffWorkflow.php --- a/src/workflow/ArcanistDiffWorkflow.php +++ b/src/workflow/ArcanistDiffWorkflow.php @@ -806,7 +806,10 @@ if ($is_raw) { if ($this->getArgument('raw')) { - fwrite(STDERR, pht('Reading diff from stdin...')."\n"); + PhutilSystem::writeStderr( + tsprintf( + "%s\n", + pht('Reading diff from stdin...'))); $raw_diff = file_get_contents('php://stdin'); } else if ($this->getArgument('raw-command')) { list($raw_diff) = execx('%C', $this->getArgument('raw-command')); diff --git a/src/workflow/ArcanistWorkflow.php b/src/workflow/ArcanistWorkflow.php --- a/src/workflow/ArcanistWorkflow.php +++ b/src/workflow/ArcanistWorkflow.php @@ -1565,7 +1565,7 @@ * @return void */ final protected function writeStatusMessage($msg) { - fwrite(STDERR, $msg); + PhutilSystem::writeStderr($msg); } final public function writeInfo($title, $message) {