Skip to content

Commit

Permalink
debug tool bar WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed May 15, 2013
1 parent 490c57f commit e96fd0e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 9 deletions.
11 changes: 7 additions & 4 deletions apps/bootstrap/protected/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
'basePath' => dirname(__DIR__),
'preload' => array('log'),
'modules' => array(
'debug' => array(
'class' => 'yii\debug\Module',
)
// 'debug' => array(
// 'class' => 'yii\debug\Module',
// )
),
'components' => array(
'cache' => array(
Expand All @@ -23,10 +23,13 @@
'log' => array(
'class' => 'yii\logging\Router',
'targets' => array(
'file' => array(
array(
'class' => 'yii\logging\FileTarget',
'levels' => array('error', 'warning'),
),
// array(
// 'class' => 'yii\logging\DebugTarget',
// )
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion yii/assets/yii.debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ yii.debug = (function ($) {
//dataType: 'json',
success: function(data) {
var $e = $('#' + id);
$e.html(data);
$e.html(data).show();
}
});
}
Expand Down
5 changes: 2 additions & 3 deletions yii/debug/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
*/
class Toolbar extends Widget
{
public $debugAction = 'debug';
public $enabled = YII_DEBUG;
public $debugAction = 'debug/default/toolbar';

public function run()
{
if ($this->enabled) {
if (Yii::$app->hasModule('debug')) {
$id = 'yii-debug-toolbar';
$url = Yii::$app->getUrlManager()->createUrl($this->debugAction, array(
'tag' => Yii::getLogger()->tag,
Expand Down
12 changes: 12 additions & 0 deletions yii/debug/controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace yii\debug\controllers;

use Yii;
use yii\web\Controller;

/**
Expand All @@ -19,4 +20,15 @@ public function actionIndex($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 changes: 39 additions & 0 deletions yii/debug/views/default/toolbar.php
Original file line number Diff line number Diff line change
@@ -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 changes: 39 additions & 1 deletion yii/logging/DebugTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
class DebugTarget extends Target
{
public $maxLogFiles = 20;

/**
* Exports log messages to a specific destination.
* Child classes must implement this method.
Expand All @@ -30,7 +32,14 @@ public function export($messages)
$file = $path . '/' . Yii::getLogger()->getTag() . '.log';
$data = array(
'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));
}
Expand All @@ -45,9 +54,38 @@ public function export($messages)
*/
public function collect($messages, $final)
{
if (Yii::$app->getModule('debug', false) !== null) {
return;
}
$this->messages = array_merge($this->messages, $this->filterMessages($messages));
if ($final) {
$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;
}
}
}
}
}

0 comments on commit e96fd0e

Please sign in to comment.