Last active
December 14, 2015 04:39
-
-
Save ithinkihaveacat/5029429 to your computer and use it in GitHub Desktop.
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
| <?php | |
| /** | |
| * Similar to print_r(), except that it writes to /tmp/rawlog.log. This function is | |
| * intended for debugging, where you want to be absolutely sure that your output | |
| * is not being captured or buffered or otherwise interfered with. | |
| * | |
| * One good way to ensure this is always available is to use the auto_prepend_file | |
| * php.ini setting. | |
| */ | |
| function rawlog() { | |
| $s = ""; | |
| for ($i = 0; $i < func_num_args(); $i++) { | |
| $arg = func_get_arg($i); | |
| if (is_numeric($arg) || is_string($arg)) { | |
| $s .= $arg; | |
| } | |
| else { | |
| $s .= var_export($arg, true); | |
| } | |
| } | |
| if (empty($s)) $s = "[EMPTY]"; | |
| $s = trim($s) . "\n"; | |
| // TODO Investigate logging to a FIFO (mkfifo /tmp/foo)-- | |
| // file needs to be opened in non-blocking mode for this to | |
| // work. | |
| file_put_contents(dirname(__FILE__) . "/rawlog.log", $s, FILE_APPEND | LOCK_EX); | |
| } | |
| /** | |
| * Writes a stack trace to /tmp/rawlog.log. | |
| */ | |
| function rawstack() { | |
| $a = array(); | |
| foreach (debug_backtrace() as $s) { | |
| foreach (array("file", "line", "function") as $k) { | |
| if (!array_key_exists($k, $s)) { | |
| $s[$k] = "<UNKNOWN>"; | |
| } | |
| } | |
| $a[] = array( | |
| "file" => $s["file"], | |
| "line" => $s["line"], | |
| "function" => $s["function"] | |
| ); | |
| } | |
| rawlog($a); | |
| } | |
| function rawtimer($s) { | |
| $t0 = microtime(true); | |
| return function() use($s, $t0) { | |
| $t1 = microtime(true); | |
| rawlog(sprintf("%s (%.3fms)", $s, 1000 * ($t1 - $t0))); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Michael.
Have you considered this? http://craig.is/writing/chrome-logger
Bogdan.