summaryrefslogtreecommitdiffstats
path: root/classes/class_debug.php
diff options
context:
space:
mode:
authorGit <git@what.cd>2011-10-29 08:00:15 +0000
committerGit <git@what.cd>2011-10-29 08:00:15 +0000
commitd052d1df505d8a7389001a890cd4b54825c55b50 (patch)
tree7324863ef87ed3d5cd7d6e5950152d3f1866dfdd /classes/class_debug.php
parent38f0e6765e9a0e5ed627d8e602d93210022e349a (diff)
downloadGazelle-d052d1df505d8a7389001a890cd4b54825c55b50.zip
Gazelle-d052d1df505d8a7389001a890cd4b54825c55b50.tar.gz
Gazelle-d052d1df505d8a7389001a890cd4b54825c55b50.tar.bz2
Empty commit
Diffstat (limited to 'classes/class_debug.php')
-rw-r--r--classes/class_debug.php160
1 files changed, 106 insertions, 54 deletions
diff --git a/classes/class_debug.php b/classes/class_debug.php
index 466f0b7..98d8a51 100644
--- a/classes/class_debug.php
+++ b/classes/class_debug.php
@@ -9,27 +9,28 @@ define('MAX_QUERIES', 30); //Maxmimum queries
class DEBUG {
public $Errors = array();
public $Flags = array();
-
+ private $LoggedVars = array();
+
public function profile($Automatic='') {
global $ScriptStartTime;
$Reason = array();
-
+
if (!empty($Automatic)) {
$Reason[] = $Automatic;
}
-
+
$Micro = (microtime(true)-$ScriptStartTime)*1000;
- if ($Micro > MAX_TIME && !defined('TIME_EXCEPTION')) {
+ if ($Micro > MAX_TIME && !defined('TIME_EXCEPTION')) {
$Reason[] = number_format($Micro, 3).' ms';
}
-
+
$Errors = count($this->get_errors());
- if ($Errors > MAX_ERRORS && !defined('ERROR_EXCEPTION')) {
+ if ($Errors > MAX_ERRORS && !defined('ERROR_EXCEPTION')) {
$Reason[] = $Errors.' PHP Errors';
}
/*
$Queries = count($this->get_queries());
- if ($Queries > MAX_QUERIES && !defined('QUERY_EXCEPTION')) {
+ if ($Queries > MAX_QUERIES && !defined('QUERY_EXCEPTION')) {
$Reason[] = $Queries.' Queries';
}
*/
@@ -37,20 +38,20 @@ class DEBUG {
if ($Ram > MAX_MEMORY && !defined('MEMORY_EXCEPTION')) {
$Reason[] = get_size($Ram).' Ram Used';
}
-
+
if (isset($_REQUEST['profile'])) {
global $LoggedUser;
$Reason[] = 'Requested by '.$LoggedUser['Username'];
}
-
+
if (isset($Reason[0])) {
$this->analysis(implode(', ', $Reason));
return true;
}
-
+
return false;
}
-
+
public function analysis($Message, $Report='', $Time=43200) {
global $Cache, $Document;
if (empty($Report)) {
@@ -58,7 +59,7 @@ class DEBUG {
}
$Identifier = make_secret(5);
$Cache->cache_value(
- 'analysis_'.$Identifier,
+ 'analysis_'.$Identifier,
array(
'url' => $_SERVER['REQUEST_URI'],
'message' => $Report,
@@ -66,25 +67,36 @@ class DEBUG {
'queries' => $this->get_queries(),
'flags' => $this->get_flags(),
'includes' => $this->get_includes(),
- 'cache' => $this->get_cache_keys()
+ 'cache' => $this->get_cache_keys(),
+ 'vars' => $this->get_logged_vars()
),
$Time
);
send_irc('PRIVMSG '.LAB_CHAN.' :'.$Message.' '.$Document.' '.' http://'.NONSSL_SITE_URL.'/tools.php?action=analysis&case='.$Identifier.' http://'.NONSSL_SITE_URL.$_SERVER['REQUEST_URI']);
}
-
+
+ public function log_var($Var, $VarName = FALSE) {
+ $BackTrace = debug_backtrace();
+ $ID = uniqid();
+ if(!$VarName) {
+ $VarName = $ID;
+ }
+ $File = array('path' => substr($BackTrace[0]['file'], strlen(SERVER_ROOT)), 'line' => $BackTrace[0]['line']);
+ $this->LoggedVars[$ID] = array($VarName => array('bt' => $File, 'data' => $Var));
+ }
+
public function set_flag($Event) {
global $ScriptStartTime;
$this->Flags[] = array($Event,(microtime(true)-$ScriptStartTime)*1000,memory_get_usage(true));
}
-
+
//This isn't in the constructor because $this is not available, and the function cannot be made static
public function handle_errors() {
//error_reporting(E_ALL ^ E_STRICT | E_WARNING | E_DEPRECATED | E_ERROR | E_PARSE); //E_STRICT disabled
error_reporting(E_WARNING | E_ERROR | E_PARSE);
set_error_handler(array($this, 'php_error_handler'));
}
-
+
protected function format_args($Array) {
$LastKey = -1;
$Return = array();
@@ -110,30 +122,30 @@ class DEBUG {
}
return implode(', ', $Return);
}
-
+
public function php_error_handler($Level, $Error, $File, $Line) {
//Who added this, it's still something to pay attention to...
if (stripos('Undefined index', $Error) !== false) {
//return true;
}
-
+
$Steps = 1; //Steps to go up in backtrace, default one
$Call = '';
$Args = '';
$Tracer = debug_backtrace();
-
+
//This is in case something in this function goes wrong and we get stuck with an infinite loop
if (isset($Tracer[$Steps]['function'], $Tracer[$Steps]['class']) && $Tracer[$Steps]['function'] == 'php_error_handler' && $Tracer[$Steps]['class'] == 'DEBUG') {
return true;
}
-
+
//If this error was thrown, we return the function which threw it
if (isset($Tracer[$Steps]['function']) && $Tracer[$Steps]['function'] == 'trigger_error') {
$Steps++;
$File = $Tracer[$Steps]['file'];
$Line = $Tracer[$Steps]['line'];
}
-
+
//At this time ONLY Array strict typing is fully supported.
//Allow us to abuse strict typing (IE: function test(Array))
if (preg_match('/^Argument (\d+) passed to \S+ must be an (array), (array|string|integer|double|object) given, called in (\S+) on line (\d+) and defined$/', $Error, $Matches)) {
@@ -141,17 +153,17 @@ class DEBUG {
$File = $Matches[4];
$Line = $Matches[5];
}
-
+
//Lets not be repetative
if (($Tracer[$Steps]['function'] == 'include' || $Tracer[$Steps]['function'] == 'require' ) && isset($Tracer[$Steps]['args'][0]) && $Tracer[$Steps]['args'][0] == $File) {
unset($Tracer[$Steps]['args']);
}
-
+
//Class
if (isset($Tracer[$Steps]['class'])) {
$Call .= $Tracer[$Steps]['class'].'::';
}
-
+
//Function & args
if (isset($Tracer[$Steps]['function'])) {
$Call .= $Tracer[$Steps]['function'];
@@ -159,7 +171,7 @@ class DEBUG {
$Args = $this->format_args($Tracer[$Steps]['args']);
}
}
-
+
//Shorten the path & we're done
$File = str_replace(SERVER_ROOT, '', $File);
$Error = str_replace(SERVER_ROOT, '', $Error);
@@ -172,13 +184,13 @@ class DEBUG {
*/
return true;
}
-
+
/* Data wrappers */
-
+
public function get_flags() {
return $this->Flags;
}
-
+
public function get_errors($Light=false) {
//Because the cache can't take some of these variables
if ($Light) {
@@ -192,7 +204,7 @@ class DEBUG {
public function get_constants() {
return get_defined_constants(true);
}
-
+
public function get_classes() {
foreach (get_declared_classes() as $Class) {
$Classes[$Class]['Vars'] = get_class_vars($Class);
@@ -200,18 +212,18 @@ class DEBUG {
}
return $Classes;
}
-
+
public function get_extensions() {
foreach (get_loaded_extensions() as $Extension) {
$Extensions[$Extension]['Functions'] = get_extension_funcs($Extension);
}
return $Extensions;
}
-
+
public function get_includes() {
return get_included_files();
}
-
+
public function get_cache_time() {
global $Cache;
return $Cache->Time;
@@ -221,27 +233,31 @@ class DEBUG {
global $Cache;
return array_keys($Cache->CacheHits);
}
-
+
public function get_sphinx_queries() {
global $SS;
return $SS->Queries;
}
-
+
public function get_sphinx_time() {
global $SS;
return $SS->Time;
}
-
+
public function get_queries() {
global $DB;
return $DB->Queries;
}
-
+
public function get_query_time() {
global $DB;
return $DB->Time;
}
+ public function get_logged_vars() {
+ return $this->LoggedVars;
+ }
+
/* Output Formatting */
public function include_table($Includes=false) {
@@ -254,7 +270,7 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_include').toggle();return false;">(View)</a> <?=number_format(count($Includes))?> Includes:</strong></td>
</tr>
</table>
- <table id="debug_include" class="hidden" width="100%">
+ <table id="debug_include" class="debug_table hidden" width="100%">
<?
foreach ($Includes as $File) {
?>
@@ -265,7 +281,7 @@ class DEBUG {
}
?>
</table>
-<?
+<?
}
public function class_table($Classes=false) {
@@ -278,7 +294,7 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_classes').toggle();return false;">(View)</a> Classes:</strong></td>
</tr>
</table>
- <table id="debug_classes" class="hidden" width="100%">
+ <table id="debug_classes" class="debug_table hidden" width="100%">
<tr>
<td align="left">
<pre><? print_r($Classes) ?></pre>
@@ -295,7 +311,7 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_extensions').toggle();return false;">(View)</a> Extensions:</strong></td>
</tr>
</table>
- <table id="debug_extensions" class="hidden" width="100%">
+ <table id="debug_extensions" class="debug_table hidden" width="100%">
<tr>
<td align="left">
<pre><? print_r($this->get_extensions()) ?></pre>
@@ -318,7 +334,7 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_flags').toggle();return false;">(View)</a> Flags:</strong></td>
</tr>
</table>
- <table id="debug_flags" class="hidden" width="100%">
+ <table id="debug_flags" class="debug_table hidden" width="100%">
<?
foreach ($Flags as $Flag) {
list($Event,$MicroTime,$Memory) = $Flag;
@@ -345,9 +361,9 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_constants').toggle();return false;">(View)</a> Constants:</strong></td>
</tr>
</table>
- <table id="debug_constants" class="hidden" width="100%">
+ <table id="debug_constants" class="debug_table hidden" width="100%">
<tr>
- <td align="left">
+ <td align="left" class="debug_data debug_constants_data">
<pre><?=display_str(print_r($Constants, true))?></pre>
</td>
</tr>
@@ -373,13 +389,13 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_cache').toggle();return false;">(View)</a><?=$Header?></strong></td>
</tr>
</table>
- <table id="debug_cache" class="hidden" width="100%">
+ <table id="debug_cache" class="debug_table hidden" width="100%">
<? foreach($CacheKeys as $Key) { ?>
<tr>
<td align="left">
<a href="#" onclick="$('#debug_cache_<?=$Key?>').toggle(); return false;"><?=display_str($Key)?></a>
</td>
- <td align="left">
+ <td align="left" class="debug_data debug_cache_data">
<pre id="debug_cache_<?=$Key?>" class="hidden"><?=display_str(print_r($Cache->get_value($Key, true), true))?></pre>
</td>
</tr>
@@ -401,21 +417,21 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_error').toggle();return false;">(View)</a> <?=number_format(count($Errors))?> Errors:</strong></td>
</tr>
</table>
- <table id="debug_error" class="hidden" width="100%">
+ <table id="debug_error" class="debug_table hidden" width="100%">
<?
foreach ($Errors as $Error) {
list($Error,$Location,$Call,$Args) = $Error;
?>
<tr valign="top">
<td align="left"><?=display_str($Call)?>(<?=display_str($Args)?>)</td>
- <td align="left"><?=display_str($Error)?></td>
+ <td class="debug_data debug_error_data" align="left"><?=display_str($Error)?></td>
<td align="left"><?=display_str($Location)?></td>
</tr>
<?
}
?>
</table>
-<?
+<?
}
public function query_table($Queries=false) {
@@ -434,20 +450,20 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_database').toggle();return false;">(View)</a><?=$Header?></strong></td>
</tr>
</table>
- <table id="debug_database" class="hidden" width="100%">
+ <table id="debug_database" class="debug_table hidden" width="100%">
<?
foreach ($Queries as $Query) {
list($SQL,$Time) = $Query;
?>
<tr valign="top">
- <td class="query"><div><?=str_replace("\t", '&nbsp;&nbsp;', nl2br(display_str($SQL)))?></div></td>
+ <td class="debug_data debug_query_data"><div><?=str_replace("\t", '&nbsp;&nbsp;', nl2br(display_str($SQL)))?></div></td>
<td class="rowa" style="width:130px;" align="left"><?=number_format($Time, 5)?> ms</td>
</tr>
<?
}
?>
</table>
-<?
+<?
}
public function sphinx_table($Queries=false) {
@@ -466,19 +482,55 @@ class DEBUG {
<td align="left"><strong><a href="#" onclick="$('#debug_sphinx').toggle();return false;">(View)</a><?=$Header?></strong></td>
</tr>
</table>
- <table id="debug_sphinx" class="hidden" width="100%">
+ <table id="debug_sphinx" class="debug_table hidden" width="100%">
<?
foreach ($Queries as $Query) {
list($Params,$Time) = $Query;
?>
<tr valign="top">
- <td><pre><?=str_replace("\t", ' ', display_str($Params))?></pre></td>
+ <td class="debug_data debug_sphinx_data"><pre><?=str_replace("\t", ' ', display_str($Params))?></pre></td>
<td class="rowa" style="width:130px;" align="left"><?=number_format($Time, 5)?> ms</td>
</tr>
<?
}
?>
</table>
-<?
+<?
+ }
+
+ public function vars_table($Vars=false) {
+ $Header = 'Logged Variables';
+ if (empty($Vars)) {
+ if(empty($this->LoggedVars)) {
+ return;
+ }
+ $Vars = $this->LoggedVars;
+ }
+ $Header = ' '.number_format(count($Vars)).' '.$Header.':';
+
+?>
+ <table width="100%">
+ <tr>
+ <td align="left"><strong><a href="#" onclick="$('#debug_loggedvars').toggle();return false;">(View)</a><?=$Header?></strong></td>
+ </tr>
+ </table>
+ <table id="debug_loggedvars" class="debug_table hidden" width="100%">
+<?
+ foreach($Vars as $ID => $Var) {
+ list($Key, $Data) = each($Var);
+ $Size = count($Data['data']);
+?>
+ <tr>
+ <td align="left">
+ <a href="#" onclick="$('#debug_loggedvars_<?=$ID?>').toggle(); return false;"><?=display_str($Key)?></a> (<?=$Size . ($Size == 1 ? ' element' : ' elements')?>)
+ <div><?=$Data['bt']['path'].':'.$Data['bt']['line'];?></div>
+ </td>
+ <td class="debug_data debug_loggedvars_data" align="left">
+ <pre id="debug_loggedvars_<?=$ID?>" class="hidden"><?=display_str(print_r($Data['data'], true));?></pre>
+ </td>
+ </tr>
+<? } ?>
+ </table>
+<?
}
}