Create a gist now

Instantly share code, notes, and snippets.

Embed
This is small utility to log output to the console of the browser from the PHP.
<?php
register_shutdown_function('cconsole::flush');
class cconsole
{
private static $logs = array();
private static $prefix = '[php] ';
private static $timers = array();
public static function setprefix($prefix) {
self::$prefix = $prefix;
}
public static function getcallerlocation($offset=0) {
$trace = debug_backtrace();
$trace = $trace[$offset];
return array($trace['file'], $trace['line']);
}
public static function log($value) {
self::_log('log', func_get_args());
}
public static function error($value) {
self::_log('error', func_get_args());
}
public static function warn($value) {
self::_log('warn', func_get_args());
}
public static function info($value) {
self::_log('info', func_get_args());
}
public static function debug($value) {
self::_log('debug', func_get_args());
}
private static function _log($type, $values) {
list($file, $line) = self::getcallerlocation(2);
$values = array_map('json_encode', $values);
$values = implode(',', $values);
self::$logs[] = sprintf('console.%s("%s%s", %d, %s);',
$type, self::$prefix, $file, $line, $values);
}
public static function time($label) {
self::$timers[$label] = ceil(microtime(true) * 1000);
}
public static function timeEnd($label) {
if (empty(self::$timers[$label])) {
throw new ErrorException("Timer \"{$label}\" has not started");
}
$time = ceil(microtime(true) * 1000) - self::$timers[$label];
self::$logs[] = sprintf('console.log("%s%s", "%sms");', self::$prefix, $label, $time);
}
public static function clear() {
self::$logs = array();
}
public static function display() {
echo '<script type="text/javascript">if(window.console){';
echo implode("\n", self::$logs);
echo '}</script>';
}
public static function flush() {
self::display();
self::clear();
}
}
<?php
require_once 'cconsole.php';
cconsole::log(111, 222, 333);
cconsole::info("佐野元春");
cconsole::warn("たけのこ太郎");
cconsole::error("ジョジョ");
cconsole::debug($_POST);
cconsole::time('MyTimer1');
$array = array();
for ($i = 0; $i < 10000; $i++) {
$array[$i] = (object) array();
}
cconsole::timeEnd('MyTimer1');
@jh3qil

This comment has been minimized.

Show comment
Hide comment
@jh3qil

jh3qil Dec 16, 2013

'faq', 'posts_per_page' => 5 ) ); ?> have_posts() ) : $loop->the_post(); ?>
>

jh3qil commented Dec 16, 2013

'faq', 'posts_per_page' => 5 ) ); ?> have_posts() ) : $loop->the_post(); ?>
>

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