mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-30 05:09:06 +02:00
NEW: Replace DebugView’s writeX() functions with renderX() functions.
DebugView writes straight to STDOUT. It would be much more flexible if it returns a string instead. It’s possible that DebugView should be scuttled in favour of a 3rd party library, but in the meantime, this change makes it more useful for the logging changes.
This commit is contained in:
parent
8b88789c82
commit
ebc3900c4a
@ -9,49 +9,60 @@
|
|||||||
* @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView?
|
* @todo Perhaps DebugView should be an interface / ABC, implemented by HTMLDebugView and CliDebugView?
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class CliDebugView extends DebugView {
|
class CliDebugView extends DebugView
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render HTML header for development views
|
* Render HTML header for development views
|
||||||
*/
|
*/
|
||||||
public function writeHeader($httpRequest = null) {
|
public function renderHeader($httpRequest = null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render HTML footer for development views
|
* Render HTML footer for development views
|
||||||
*/
|
*/
|
||||||
public function writeFooter() {
|
public function renderFooter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write information about the error to the screen
|
* Write information about the error to the screen
|
||||||
*/
|
*/
|
||||||
public function writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext) {
|
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) {
|
||||||
$errorType = self::$error_types[$errno];
|
if(!isset(self::$error_types[$errno])) {
|
||||||
echo SS_Cli::text("ERROR [" . $errorType['title'] . "]: $errstr\nIN $httpRequest\n", "red", null, true);
|
$errorTypeTitle = "UNKNOWN TYPE, ERRNO $errno";
|
||||||
echo SS_Cli::text("Line $errline in $errfile\n\n", "red");
|
} else {
|
||||||
|
$errorTypeTitle = self::$error_types[$errno]['title'];
|
||||||
|
}
|
||||||
|
$output = SS_Cli::text("ERROR [" . $errorTypeTitle . "]: $errstr\nIN $httpRequest\n", "red", null, true);
|
||||||
|
$output .= SS_Cli::text("Line $errline in $errfile\n\n", "red");
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a fragment of the a source file
|
* Write a fragment of the a source file
|
||||||
* @param $lines An array of file lines; the keys should be the original line numbers
|
* @param $lines An array of file lines; the keys should be the original line numbers
|
||||||
*/
|
*/
|
||||||
public function writeSourceFragment($lines, $errline) {
|
public function renderSourceFragment($lines, $errline) {
|
||||||
echo "Source\n======\n";
|
$output = "Source\n======\n";
|
||||||
foreach($lines as $offset => $line) {
|
foreach($lines as $offset => $line) {
|
||||||
echo ($offset == $errline) ? "* " : " ";
|
$output .= ($offset == $errline) ? "* " : " ";
|
||||||
echo str_pad("$offset:",5);
|
$output .= str_pad("$offset:", 5);
|
||||||
echo wordwrap($line, self::config()->columns, "\n ");
|
$output .= wordwrap($line, self::config()->columns, "\n ");
|
||||||
}
|
}
|
||||||
echo "\n";
|
$output .= "\n";
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a backtrace
|
* Write a backtrace
|
||||||
*/
|
*/
|
||||||
public function writeTrace($trace = null) {
|
public function renderTrace($trace = null) {
|
||||||
echo "Trace\n=====\n";
|
$output = "Trace\n=====\n";
|
||||||
echo SS_Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true);
|
$output .= SS_Backtrace::get_rendered_backtrace($trace ? $trace : debug_backtrace(), true);
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -60,26 +71,30 @@ class CliDebugView extends DebugView {
|
|||||||
* @param string $title
|
* @param string $title
|
||||||
* @param string $title
|
* @param string $title
|
||||||
*/
|
*/
|
||||||
public function writeInfo($title, $subtitle, $description=false) {
|
public function renderInfo($title, $subtitle, $description=false) {
|
||||||
echo wordwrap(strtoupper($title),self::config()->columns) . "\n";
|
$output = wordwrap(strtoupper($title), self::config()->columns) . "\n";
|
||||||
echo wordwrap($subtitle,self::config()->columns) . "\n";
|
$output .= wordwrap($subtitle, self::config()->columns) . "\n";
|
||||||
echo str_repeat('-',min(self::config()->columns,max(strlen($title),strlen($subtitle)))) . "\n";
|
$output .= str_repeat('-', min(self::config()->columns, max(strlen($title), strlen($subtitle)))) . "\n";
|
||||||
echo wordwrap($description,self::config()->columns) . "\n\n";
|
$output .= wordwrap($description, self::config()->columns) . "\n\n";
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writeVariable($val, $caller) {
|
public function renderVariable($val, $caller) {
|
||||||
echo PHP_EOL;
|
$output = PHP_EOL;
|
||||||
echo SS_Cli::text(str_repeat('=', self::config()->columns), 'green');
|
$output .= SS_Cli::text(str_repeat('=', self::config()->columns), 'green');
|
||||||
echo PHP_EOL;
|
$output .= PHP_EOL;
|
||||||
echo SS_Cli::text($this->formatCaller($caller), 'blue', null, true);
|
$output .= SS_Cli::text($this->formatCaller($caller), 'blue', null, true);
|
||||||
echo PHP_EOL.PHP_EOL;
|
$output .= PHP_EOL.PHP_EOL;
|
||||||
if (is_string($val)) {
|
if (is_string($val)) {
|
||||||
print_r(wordwrap($val, self::config()->columns));
|
$output .= wordwrap($val, self::config()->columns);
|
||||||
} else {
|
} else {
|
||||||
print_r($val);
|
$output .= var_export($val, true);
|
||||||
}
|
}
|
||||||
echo PHP_EOL;
|
$output .= PHP_EOL;
|
||||||
echo SS_Cli::text(str_repeat('=', self::config()->columns), 'green');
|
$output .= SS_Cli::text(str_repeat('=', self::config()->columns), 'green');
|
||||||
echo PHP_EOL;
|
$output .= PHP_EOL;
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@
|
|||||||
* @package framework
|
* @package framework
|
||||||
* @subpackage dev
|
* @subpackage dev
|
||||||
*/
|
*/
|
||||||
class DebugView extends Object {
|
class DebugView extends Object
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Column size to wrap long strings to
|
* Column size to wrap long strings to
|
||||||
@ -100,9 +101,66 @@ class DebugView extends Object {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render HTML header for development views
|
* @deprecated 4.0.0:5.0.0 Use renderHeader() instead
|
||||||
*/
|
*/
|
||||||
public function writeHeader() {
|
public function writeHeader() {
|
||||||
|
Deprecation::notice('4.0', 'Use renderHeader() instead');
|
||||||
|
echo $this->renderHeader();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 4.0.0:5.0.0 Use renderInfo() instead
|
||||||
|
*/
|
||||||
|
public function writeInfo($title, $subtitle, $description=false) {
|
||||||
|
Deprecation::notice('4.0', 'Use renderInfo() instead');
|
||||||
|
echo $this->renderInfo($title, $subtitle, $description);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 4.0.0:5.0.0 Use renderFooter() instead
|
||||||
|
*/
|
||||||
|
public function writeFooter() {
|
||||||
|
Deprecation::notice('4.0', 'Use renderFooter() instead');
|
||||||
|
echo $this->renderFooter();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 4.0.0:5.0.0 Use renderError() instead
|
||||||
|
*/
|
||||||
|
public function writeError($httpRequest, $errno, $errstr, $errfile, $errline) {
|
||||||
|
Deprecation::notice('4.0', 'Use renderError() instead');
|
||||||
|
echo $this->renderError($httpRequest, $errno, $errstr, $errfile, $errline);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 4.0.0:5.0.0 Use renderSourceFragment() instead
|
||||||
|
*/
|
||||||
|
public function writeSourceFragment($lines, $errline) {
|
||||||
|
Deprecation::notice('4.0', 'Use renderSourceFragment() instead');
|
||||||
|
echo $this->renderSourceFragment($lines, $errline);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 4.0.0:5.0.0 Use renderTrace() instead
|
||||||
|
*/
|
||||||
|
public function writeTrace($trace) {
|
||||||
|
Deprecation::notice('4.0', 'Use renderTrace() instead');
|
||||||
|
echo $this->renderTrace($trace);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated 4.0.0:5.0.0 Use renderVariable() instead
|
||||||
|
*/
|
||||||
|
public function writeVariable($val, $caller) {
|
||||||
|
Deprecation::notice('4.0', 'Use renderVariable() instead');
|
||||||
|
echo $this->renderVariable($val, $caller);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render HTML header for development views
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function renderHeader() {
|
||||||
$url = htmlentities(
|
$url = htmlentities(
|
||||||
$_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'],
|
$_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'],
|
||||||
ENT_COMPAT,
|
ENT_COMPAT,
|
||||||
@ -115,41 +173,55 @@ class DebugView extends Object {
|
|||||||
'css/debug.css'
|
'css/debug.css'
|
||||||
);
|
);
|
||||||
|
|
||||||
echo '<!DOCTYPE html><html><head><title>' . $url . '</title>';
|
$output = '<!DOCTYPE html><html><head><title>' . $url . '</title>';
|
||||||
echo '<link rel="stylesheet" type="text/css" href="'. $debugCSS .'" />';
|
$output .= '<link rel="stylesheet" type="text/css" href="'. $debugCSS .'" />';
|
||||||
echo '</head>';
|
$output .= '</head>';
|
||||||
echo '<body>';
|
$output .= '<body>';
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render the information header for the view
|
* Render the information header for the view
|
||||||
*
|
*
|
||||||
* @param string $title
|
* @param string $title The main title
|
||||||
* @param string $title
|
* @param string $subtitle The subtitle
|
||||||
|
* @param string|false $description The description to show
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeInfo($title, $subtitle, $description=false) {
|
public function renderInfo($title, $subtitle, $description=false) {
|
||||||
echo '<div class="info">';
|
$output = '<div class="info">';
|
||||||
echo "<h1>" . Convert::raw2xml($title) . "</h1>";
|
$output .= "<h1>" . Convert::raw2xml($title) . "</h1>";
|
||||||
if($subtitle) echo "<h3>" . Convert::raw2xml($subtitle) . "</h3>";
|
if($subtitle) $output .= "<h3>" . Convert::raw2xml($subtitle) . "</h3>";
|
||||||
if ($description) {
|
if ($description) {
|
||||||
echo "<p>$description</p>";
|
$output .= "<p>$description</p>";
|
||||||
} else {
|
} else {
|
||||||
echo $this->Breadcrumbs();
|
$output .= $this->Breadcrumbs();
|
||||||
}
|
}
|
||||||
echo '</div>';
|
$output .= '</div>';
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render HTML footer for development views
|
* Render HTML footer for development views
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeFooter() {
|
public function renderFooter() {
|
||||||
echo "</body></html>";
|
return "</body></html>";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write information about the error to the screen
|
* Render an error.
|
||||||
|
*
|
||||||
|
* @param string $httpRequest the kind of request
|
||||||
|
* @param int $errno Codenumber of the error
|
||||||
|
* @param string $errstr The error message
|
||||||
|
* @param string $errfile The name of the soruce code file where the error occurred
|
||||||
|
* @param int $errline The line number on which the error occured
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext) {
|
public function renderError($httpRequest, $errno, $errstr, $errfile, $errline) {
|
||||||
$errorType = isset(self::$error_types[$errno]) ? self::$error_types[$errno] : self::$unknown_error;
|
$errorType = isset(self::$error_types[$errno]) ? self::$error_types[$errno] : self::$unknown_error;
|
||||||
$httpRequestEnt = htmlentities($httpRequest, ENT_COMPAT, 'UTF-8');
|
$httpRequestEnt = htmlentities($httpRequest, ENT_COMPAT, 'UTF-8');
|
||||||
if (ini_get('html_errors')) {
|
if (ini_get('html_errors')) {
|
||||||
@ -157,45 +229,60 @@ class DebugView extends Object {
|
|||||||
} else {
|
} else {
|
||||||
$errstr = Convert::raw2xml($errstr);
|
$errstr = Convert::raw2xml($errstr);
|
||||||
}
|
}
|
||||||
echo '<div class="info ' . $errorType['class'] . '">';
|
$output = '<div class="info ' . $errorType['class'] . '">';
|
||||||
echo "<h1>[" . $errorType['title'] . '] ' . $errstr . "</h1>";
|
$output .= "<h1>[" . $errorType['title'] . '] ' . $errstr . "</h1>";
|
||||||
echo "<h3>$httpRequestEnt</h3>";
|
$output .= "<h3>$httpRequestEnt</h3>";
|
||||||
echo "<p>Line <strong>$errline</strong> in <strong>$errfile</strong></p>";
|
$output .= "<p>Line <strong>$errline</strong> in <strong>$errfile</strong></p>";
|
||||||
echo '</div>';
|
$output .= '</div>';
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a fragment of the a source file
|
* Render a fragment of the a source file
|
||||||
* @param $lines An array of file lines; the keys should be the original line numbers
|
*
|
||||||
|
* @param array $lines An array of file lines; the keys should be the original line numbers
|
||||||
|
* @param int errLine The line of the error
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeSourceFragment($lines, $errline) {
|
public function renderSourceFragment($lines, $errline) {
|
||||||
echo '<div class="trace"><h3>Source</h3>';
|
$output = '<div class="trace"><h3>Source</h3>';
|
||||||
echo '<pre>';
|
$output .= '<pre>';
|
||||||
foreach($lines as $offset => $line) {
|
foreach($lines as $offset => $line) {
|
||||||
$line = htmlentities($line, ENT_COMPAT, 'UTF-8');
|
$line = htmlentities($line, ENT_COMPAT, 'UTF-8');
|
||||||
if ($offset == $errline) {
|
if ($offset == $errline) {
|
||||||
echo "<span>$offset</span> <span class=\"error\">$line</span>";
|
$output .= "<span>$offset</span> <span class=\"error\">$line</span>";
|
||||||
} else {
|
} else {
|
||||||
echo "<span>$offset</span> $line";
|
$output .= "<span>$offset</span> $line";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
echo '</pre>';
|
$output .= '</pre>';
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a backtrace
|
* Render a call track
|
||||||
|
*
|
||||||
|
* @param array $trace The debug_backtrace() array
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeTrace($trace) {
|
public function renderTrace($trace) {
|
||||||
echo '<h3>Trace</h3>';
|
$output = '<h3>Trace</h3>';
|
||||||
echo SS_Backtrace::get_rendered_backtrace($trace);
|
$output .= SS_Backtrace::get_rendered_backtrace($trace);
|
||||||
echo '</div>';
|
$output .= '</div>';
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $text
|
* Render an arbitrary paragraph.
|
||||||
|
*
|
||||||
|
* @param string $text The HTML-escaped text to render
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeParagraph($text) {
|
public function renderParagraph($text) {
|
||||||
echo '<p class="info">' . $text . '</p>';
|
return '<p class="info">' . $text . '</p>';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -217,12 +304,15 @@ class DebugView extends Object {
|
|||||||
*
|
*
|
||||||
* @param object $val
|
* @param object $val
|
||||||
* @param array $caller Caller information
|
* @param array $caller Caller information
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function writeVariable($val, $caller) {
|
public function renderVariable($val, $caller) {
|
||||||
echo '<pre style="background-color:#ccc;padding:5px;font-size:14px;line-height:18px;">';
|
$output = '<pre style="background-color:#ccc;padding:5px;font-size:14px;line-height:18px;">';
|
||||||
echo "<span style=\"font-size: 12px;color:#666;\">" . $this->formatCaller($caller). " - </span>\n";
|
$output .= "<span style=\"font-size: 12px;color:#666;\">" . $this->formatCaller($caller). " - </span>\n";
|
||||||
if (is_string($val)) print_r(wordwrap($val, self::config()->columns));
|
if (is_string($val)) $output .= wordwrap($val, self::config()->columns);
|
||||||
else print_r($val);
|
else $output .= var_export($val, true);
|
||||||
echo '</pre>';
|
$output .= '</pre>';
|
||||||
|
|
||||||
|
return $output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user