Created
September 30, 2014 14:12
-
-
Save Terrance/b92726dcd51cde53a10d to your computer and use it in GitHub Desktop.
A lightweight PHP include to post and view debugging messages for a session. Call `Debug::start()` then `Debug::post("message", [0-3])`. View output by appending `?d` to the URL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| 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, "Ubuntu Mono""> | |
| <? | |
| 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