forked from bcit-ci/CodeIgniter
-
Notifications
You must be signed in to change notification settings - Fork 0
FirePHP class
Derek Jones edited this page Jul 5, 2012
·
6 revisions
Here is a new FirePHP library for printing variables to the firebug console. Its much simpler than the original Firephp core and easier to use(I think so anyway)
It supports most of the FirePHP protocol except for types = table and group. It is php5 only.
There are some known bugs around logging some custom objects. Also I've noticed that large arrays do not seem to print to the console. This may be due to a bug in Firephp or a limitation to the allowed size of http headers.?
Usage:
$this->load->library('console');
$this->console->log('woot'); // prints 'woot' to the firebug console
$this->console->log(array('foo'=>'bar'), 'warn'); // prints array contents as a warning
$this->console->log('woot', 'error', true); // prints 'woot' as an error and also writes it to the CI log file
Anyway, here is the code:
<?php
class Console {
public $enabled = true;
private $index = 1;
private $CI;
function Console($enable=true) {
$this->CI =& get_instance();
$this->enabled = $enable;
}
/**
* Log data to the fireBug Console (via firePHP)
* @param Mixed $type
* @param Mixed $message
* @param Bool $write_to_file [optional]
*/
function log($message, $type='log', $write_to_file=false) {
$header_name = 'X-Wf-1-1-1-'.$this->index;
if (!is_array($type) && !is_object($type)) {
if (in_array(strtolower($type), array('log','info','warn','error'))) {
// create header value
$header_value = '[{"Type":"'.strtoupper($type).'"},'.json_encode($message).']';
if ($write_to_file==true) {
log_message($type, print_r($message, true));
}
}
}
else {
$meta;
// create meta Object
foreach ($type as $key=>$value) {
$key = ucfirst($key);
$meta->$key = $value;
}
$body;
// create body object
foreach ($message as $key=>$value) {
$key = ucfirst($key);
$body->$key = $value;
}
// create header value
$header_value = '['.json_encode($meta).','.json_encode($body).']';
if ($write_to_file==true) {
log_message($meta->Type, $body->Trace.': '.print_r(json_decode($body->Trace), true));
}
}
if ($this->enabled) {
if ($this->index==1) {
// set base firePHP headers
$this->CI->output->set_header('X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
$this->CI->output->set_header('X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
$this->CI->output->set_header('X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
}
// set output header
$this->CI->output->set_header($header_name.': '.strlen($header_value).'|'.$header_value.'|');
// increase log index
$this->index++;
}
}
}
/* End of file console.php */
/* Location: application/libraries/console.php */
UPDATE: just fixed the print out of arrays/objects to the log file (now uses print_r).