Skip to content

Instantly share code, notes, and snippets.

@Terrance
Created April 1, 2013 11:36
Show Gist options
  • Save Terrance/5284467 to your computer and use it in GitHub Desktop.
Save Terrance/5284467 to your computer and use it in GitHub Desktop.
PHP debugging class. Include and call post() to log an entry. View the page with ?d to display all entries.
<?
class Debug {
const log = 0;
const success = 1;
const warning = 2;
const error = 3;
private static $init = false;
public static function start() {
session_start();
if (!$_SESSION["debug"]) {
$_SESSION["debug"] = array();
}
Debug::$init = true;
}
public static function post($msg, $lvl=0) {
if (!Debug::$init) {
Debug::start();
}
$_SESSION["debug"][] = array(date("H:i:s"), $msg, $lvl);
}
}
if (isset($_REQUEST["d"])) {
Debug::start();
?><html>
<head>
<title>Debug</title>
<script>setTimeout("location.reload();", 2000);</script>
</head>
<body>
<table style="font-family: Consolas, &quot;Ubuntu Mono&quot;">
<?
foreach ($_SESSION["debug"] as $item) {
$colour = array(
0 => "dimgrey",
1 => "darkgreen",
2 => "darkorange",
3 => "red"
);
?>
<tr style="color: <? print($colour[$item[2]]); ?>">
<td valign="top" style="padding-right: 10px; border-right: 2px solid <? print($colour[$item[2]]); ?>;"><? print($item[0]); ?></td>
<td style="padding-left: 10px;"><? print(preg_replace("/\n/m", "<br/>", $item[1])); ?></td>
</tr><?
}
?>
</table>
</body>
</html><?
}
elseif (isset($_REQUEST["c"])) {
Debug::start();
$_SESSION["debug"] = array();
header("Location: ?d");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment