Browse Source

debug tool bar WIP

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
e96fd0ea59
  1. 11
      apps/bootstrap/protected/config/main.php
  2. 2
      yii/assets/yii.debug.js
  3. 5
      yii/debug/Toolbar.php
  4. 12
      yii/debug/controllers/DefaultController.php
  5. 39
      yii/debug/views/default/toolbar.php
  6. 40
      yii/logging/DebugTarget.php

11
apps/bootstrap/protected/config/main.php

@ -5,9 +5,9 @@ return array(
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => array('log'), 'preload' => array('log'),
'modules' => array( 'modules' => array(
'debug' => array( // 'debug' => array(
'class' => 'yii\debug\Module', // 'class' => 'yii\debug\Module',
) // )
), ),
'components' => array( 'components' => array(
'cache' => array( 'cache' => array(
@ -23,10 +23,13 @@ return array(
'log' => array( 'log' => array(
'class' => 'yii\logging\Router', 'class' => 'yii\logging\Router',
'targets' => array( 'targets' => array(
'file' => array( array(
'class' => 'yii\logging\FileTarget', 'class' => 'yii\logging\FileTarget',
'levels' => array('error', 'warning'), 'levels' => array('error', 'warning'),
), ),
// array(
// 'class' => 'yii\logging\DebugTarget',
// )
), ),
), ),
), ),

2
yii/assets/yii.debug.js

@ -18,7 +18,7 @@ yii.debug = (function ($) {
//dataType: 'json', //dataType: 'json',
success: function(data) { success: function(data) {
var $e = $('#' + id); var $e = $('#' + id);
$e.html(data); $e.html(data).show();
} }
}); });
} }

5
yii/debug/Toolbar.php

@ -17,12 +17,11 @@ use yii\helpers\Html;
*/ */
class Toolbar extends Widget class Toolbar extends Widget
{ {
public $debugAction = 'debug'; public $debugAction = 'debug/default/toolbar';
public $enabled = YII_DEBUG;
public function run() public function run()
{ {
if ($this->enabled) { if (Yii::$app->hasModule('debug')) {
$id = 'yii-debug-toolbar'; $id = 'yii-debug-toolbar';
$url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array( $url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array(
'tag' => Yii::getLogger()->tag, 'tag' => Yii::getLogger()->tag,

12
yii/debug/controllers/DefaultController.php

@ -7,6 +7,7 @@
namespace yii\debug\controllers; namespace yii\debug\controllers;
use Yii;
use yii\web\Controller; use yii\web\Controller;
/** /**
@ -19,4 +20,15 @@ class DefaultController extends Controller
{ {
echo $tag; echo $tag;
} }
public function actionToolbar($tag)
{
$file = Yii::$app->getRuntimePath() . "/debug/$tag.log";
if (preg_match('/^[\w\-]+$/', $tag) && is_file($file)) {
$data = json_decode(file_get_contents($file), true);
echo $this->renderPartial('toolbar', $data);
} else {
echo "Unable to find debug data tagged with '$tag'.";
}
}
} }

39
yii/debug/views/default/toolbar.php

@ -0,0 +1,39 @@
<?php use yii\helpers\Html;
echo Html::style("
#yii-debug-toolbar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
background-color: #eee;
border-top: 1px solid #ccc;
margin: 0;
padding: 5px 10px;
z-index: 1000000;
font: 11px Verdana, Arial, sans-serif;
text-align: left;
}
.yii-debug-toolbar-block {
float: left;
margin: 0 10px;
");
?>
<div class="yii-debug-toolbar-block">
</div>
<div class="yii-debug-toolbar-block">
Peak memory: <?php echo sprintf('%.2fMB', $memory / 1048576); ?>
</div>
<div class="yii-debug-toolbar-block">
Time spent: <?php echo sprintf('%.3fs', $time); ?>
</div>
<div class="yii-debug-toolbar-block">
</div>
<div class="yii-debug-toolbar-block">
</div>

40
yii/logging/DebugTarget.php

@ -15,6 +15,8 @@ use Yii;
*/ */
class DebugTarget extends Target class DebugTarget extends Target
{ {
public $maxLogFiles = 20;
/** /**
* Exports log messages to a specific destination. * Exports log messages to a specific destination.
* Child classes must implement this method. * Child classes must implement this method.
@ -30,7 +32,14 @@ class DebugTarget extends Target
$file = $path . '/' . Yii::getLogger()->getTag() . '.log'; $file = $path . '/' . Yii::getLogger()->getTag() . '.log';
$data = array( $data = array(
'messages' => $messages, 'messages' => $messages,
'globals' => $GLOBALS, '_SERVER' => $_SERVER,
'_GET' => $_GET,
'_POST' => $_POST,
'_COOKIE' => $_COOKIE,
'_FILES' => empty($_FILES) ? array() : $_FILES,
'_SESSION' => empty($_SESSION) ? array() : $_SESSION,
'memory' => memory_get_peak_usage(),
'time' => microtime(true) - YII_BEGIN_TIME,
); );
file_put_contents($file, json_encode($data)); file_put_contents($file, json_encode($data));
} }
@ -45,9 +54,38 @@ class DebugTarget extends Target
*/ */
public function collect($messages, $final) public function collect($messages, $final)
{ {
if (Yii::$app->getModule('debug', false) !== null) {
return;
}
$this->messages = array_merge($this->messages, $this->filterMessages($messages)); $this->messages = array_merge($this->messages, $this->filterMessages($messages));
if ($final) { if ($final) {
$this->export($this->messages); $this->export($this->messages);
$this->gc();
}
}
protected function gc()
{
if (mt_rand(0, 10000) > 100) {
return;
}
$iterator = new \DirectoryIterator(Yii::$app->getRuntimePath() . '/debug');
$files = array();
foreach ($iterator as $file) {
if (preg_match('/^[\d\-]+\.log$/', $file->getFileName()) && $file->isFile()) {
$files[] = $file->getPathname();
}
}
sort($files);
if (count($files) > $this->maxLogFiles) {
$n = count($files) - $this->maxLogFiles;
foreach ($files as $i => $file) {
if ($i < $n) {
unlink($file);
} else {
break;
}
}
} }
} }
} }

Loading…
Cancel
Save