Skip to content

Instantly share code, notes, and snippets.

@ithinkihaveacat
Last active December 14, 2015 04:39
Show Gist options
  • Select an option

  • Save ithinkihaveacat/5029429 to your computer and use it in GitHub Desktop.

Select an option

Save ithinkihaveacat/5029429 to your computer and use it in GitHub Desktop.
<?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)));
};
}
@BogdanDogaru
Copy link

Hi Michael.

Have you considered this? http://craig.is/writing/chrome-logger

Bogdan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment