class Qse_XDebug { private $met_out = array( 'log' => 'log' , 'debug' => 'debug' , 'info' => 'info' , 'warn' => 'warn' , 'error' => 'error' , 'time' => 'time' , 'timeEnd' => 'timeEnd' , 'profile' => 'profile' , 'profileEnd' => 'profileEnd' , 'trace' => 'trace' , 'group' => 'group' , 'groupEnd' => 'groupEnd' , 'dir' => 'dir' , 'dirxml' => 'dirxml' , ); protected $info_store = array(); public function __construct(){ $this->group(get_class($this) . " 调试控制台"); } private function js_text($js_code=null){ return "{$js_code}\n" ; } function __call($funcname,$args){ if (!empty($funcname) && isset($this->met_out[$funcname])) { $object = array( $funcname => $args ); $this->insert_data($object); } } private function insert_data($object=null){ array_push($this->info_store,$object); } function execute($returnvalue=false){ $output = "<script>%s</script>" ; $content = '' ; foreach ($this->info_store as $info){ if (!is_array($info)) continue ; foreach ($info as $met=>$args){ $str_args = $this->parse_values($args) ; $content .= $this->js_text("console.{$met}({$str_args});"); } } if ($return_value) return sprintf($output,$content) ; echo sprintf($output,$content) ; } private function parse_values($args){ if (!is_array($args)) return '' ; $values = $val = '' ; foreach ($args as $arg){ if (is_string($arg)) $val = "'{$arg}'"; else if(is_numeric($arg)) $val = $arg ; else if(is_array($arg) || is_object($arg)){ if (function_exists('json_encode')) $val = json_encode($arg); } $values .= $val . ',' ; } return preg_replace('/,$/','',$values) ; } }
demo:
<?php $xdebug->log("hellow world!"); $xdebug->group("组测试"); $xdebug->error("测试 %s ; 时间: %d","旅游景点",time()); $xdebug->warn("hellow world!",'chouzhutou',array(1,2,4,QContext::instance()));
?>