ryoakg
5/10/2013 - 10:17 AM

This is small utility to log output to the console of the browser from the PHP.

This is small utility to log output to the console of the browser from the PHP.

<?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');

<?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();
	}
}