/** * Renders an exception using ansi format for console output. * @param \Exception $exception the exception to be rendered. */ protected function renderException($exception) { if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) { $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage(); } elseif (YII_DEBUG) { if ($exception instanceof Exception) { $message = $this->formatMessage("Exception ({$exception->getName()})"); } elseif ($exception instanceof ErrorException) { $message = $this->formatMessage($exception->getName()); } else { $message = $this->formatMessage('Exception'); } $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE]) . " with message " . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) . "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD]) . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n"; if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) { $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true); } $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString(); } else { $message = $this->formatMessage('Error: ') . $exception->getMessage(); } if (PHP_SAPI === 'cli') { Console::stderr($message . "\n"); } else { echo $message . "\n"; } }
/** * Renders an exception using ansi format for console output. * @param \Exception $exception the exception to be rendered. */ protected function renderException($exception) { if ($exception instanceof UnknownCommandException) { // display message and suggest alternatives in case of unknown command $message = $this->formatMessage($exception->getName() . ': ') . $exception->command; $alternatives = $exception->getSuggestedAlternatives(); if (count($alternatives) === 1) { $message .= "\n\nDid you mean \"" . reset($alternatives) . "\"?"; } elseif (count($alternatives) > 1) { $message .= "\n\nDid you mean one of these?\n - " . implode("\n - ", $alternatives); } } elseif ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) { $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage(); } elseif (YII_DEBUG) { if ($exception instanceof Exception) { $message = $this->formatMessage("Exception ({$exception->getName()})"); } elseif ($exception instanceof ErrorException) { $message = $this->formatMessage($exception->getName()); } else { $message = $this->formatMessage('Exception'); } $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE]) . ' with message ' . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) . "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD]) . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n"; if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) { $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true); } $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString(); } else { $message = $this->formatMessage('Error: ') . $exception->getMessage(); } if (PHP_SAPI === 'cli') { Console::stderr($message . "\n"); } else { echo $message . "\n"; } }
public function startHead($controller, $headType, $headId, $restarting) { if (!$restarting) { // Console::output(Console::ansiFormat('Starting ' . $headId, [Console::FG_CYAN])); } else { // Console::output(Console::ansiFormat('Restarting ' . $headId, [Console::FG_CYAN])); } $_this = $this; $process = new \React\ChildProcess\Process($this->getSubCommand($controller, [$headType, $headId])); $process->on('exit', function ($exitCode, $termSignal) use(&$_this, &$controller, $headType, $headId) { if ($exitCode !== 0) { Console::stderr(Console::ansiFormat("Broadcast head {$headType}:{$headId} exited with error code {$exitCode}", [Console::FG_RED])); sleep(10); } if (static::isPaused()) { Yii::$app->end(0); } $_this->_heads[$headId] = $_this->startHead($controller, $headType, $headId, true); }); $this->loop->addTimer(0.0001, function ($timer) use($process, &$_this) { $process->start($timer->getLoop()); $process->stdout->on('data', function ($output) use($_this) { $stdout = fopen('php://stdout', 'w+'); fwrite($stdout, $output); }); $process->stderr->on('data', function ($output) use($_this) { $stderr = fopen('php://stderr', 'w+'); fwrite($stderr, $output); }); }); sleep(5); return $process; }
/** * Prints a string to STDERR. * * @param string $string the string to print * @return int|boolean Number of bytes printed or false on error */ public static function stderr($string) { $args = func_get_args(); array_shift($args); $string = parent::ansiFormat($string, $args) . "\n"; return parent::stderr($string); }
/** * Prints a string to STDERR. * * @param string $string the string to print * @return int|boolean Number of bytes printed or false on error */ public function stderr($string) { if ($this->isColorEnabled(\STDERR)) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args) . "\n"; } return Console::stderr($string); }
/** * @inheritdoc */ public function export() { foreach ($this->messages as $message) { $string = $this->formatMessage($message) . "\n"; $level = $message[1]; if ($this->stderrIsNotStdout && in_array($level, $this->stderrLevels)) { if ($this->stderrSupportsColors) { Console::stderr(Console::ansiFormat($string, $this->colorMap[$level])); } else { Console::stderr($string); } } if ($this->stdoutSupportsColors) { Console::stdout(Console::ansiFormat($string, $this->colorMap[$level])); } else { Console::stdout($string); } } }
/** * Exports log [[messages]] to a specific destination. * Child classes must implement this method. */ public function export() { foreach ($this->messages as $message) { list($text, $level, $category, $timestamp) = $message; if (!is_string($text)) { // exceptions may not be serializable if in the call stack somewhere is a Closure if ($text instanceof \Throwable || $text instanceof \Exception) { $text = (string) $text; } else { $text = VarDumper::export($text); } } $string = "[{$level}][{$category}] {$text}"; if ($level == Logger::LEVEL_ERROR) { Console::stderr($string); } else { Console::stdout($string); } } }
public function export() { foreach ($this->messages as $message) { $string = $this->formatMessage($message) . "\n"; $level = $message[1]; if ($level == Logger::LEVEL_INFO) { if (strncmp('BEGIN ', $message[0], 6) == 0) { $ansiColor = $this->_levelAnsiColorMap[Logger::LEVEL_PROFILE_BEGIN]; } elseif (strncmp('END ', $message[0], 4) == 0) { $ansiColor = $this->_levelAnsiColorMap[Logger::LEVEL_PROFILE_END]; } else { $ansiColor = $this->_levelAnsiColorMap[Logger::LEVEL_INFO]; } } elseif (array_key_exists($level, $this->_levelAnsiColorMap)) { $ansiColor = $this->_levelAnsiColorMap[$level]; } else { $ansiColor = $this->defaultAnsiColor; } if ($this->_stdoutIsTerminal) { if ($this->_stdoutSupportsAnsiColors && $ansiColor) { Console::stdout(Console::ansiFormat($string, $ansiColor)); } else { Console::stdout($string); } } else { Console::stdout($string); if ($this->_stderrIsTerminal && ($level == Logger::LEVEL_ERROR || $level == Logger::LEVEL_WARNING)) { if ($this->_stderrSupportsAnsiColors && $ansiColor) { Console::stderr(Console::ansiFormat($string, $ansiColor)); } else { Console::stderr($string); } } } } }