プロファイラーにデバッグログを書き出す

http://d.hatena.ne.jp/KuniTsuji/20080904/1220536127
昨日プロファイラーをポップアップにしましたが、今日はそこにデバッグLOGを書き出してみました。
まずはMY_Profiler.php
に次のメソッドを追加

    function _getLog()
    {
        $log = '';
        $LOG =& load_class('Log');
        $log = $LOG->getLog();
        if (! $log)
        {
            return '';
        }
        $CI =& get_instance();
        $logstr = '';
        foreach($log as $val)
        {
            $logstr .= "" . $val . "<br />";
        }
        return $CI->db->escape($logstr);
    }

run()メソッドを一部修正

    function run()
    {
        $win = <<< EOT
<script language="JavaScript">
var win = window.open('', 'profiler', 'toolbar=no,scrollbars,width=750,height=500');
win.document.writeln('<html>');
win.document.writeln('<head>');
win.document.writeln('<title>Profiler Window</title>');
win.document.writeln('<style type="text/css">');
win.document.writeln('body { font-family: monospace; font-size: 8pt; width:90%}');
win.document.writeln('td,th { font-size: 10pt; }');
win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
win.document.writeln('td,th { border-right: #999999 solid 1px; }');
win.document.writeln('tr { text-align: left; vertical-align: top; }');
win.document.writeln('</style>');
win.document.writeln('</head>');
win.document.writeln('<body>');
win.document.writeln({$this->_runStr()});
win.document.writeln('<div>');
win.document.writeln({$this->_getLog()});
win.document.writeln('</div>');
win.document.writeln('<a href="javascript:window.close();">close</a>');
win.document.writeln('</body></html>');
</script>
EOT;

        return $win;
    }

Logクラスを修正
MY_Log.phpを用意します。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category
 * @package    MYNETS_Validation Class
 * @author     KUNIHARU Tsujioka <kunitsuji@gmail.com>
 * @copyright  Copyright (c) 2008 KUNIHARU Tsujioka <kunitsuji@gmail.com>
 * @copyright  Copyright (c) 2006-2008 Usagi Project (URL:http://usagi.mynets.jp)
 * @license    New BSD License
 */
class MYNETS_Log extends CI_Log
{
    
    private $_logmessage = array();
    
    function __construct()
    {
        parent::__construct();
    }


	/**
	 * Write Log File
	 *
	 * Generally this function will be called using the global log_message() function
	 *
	 * @access	public
	 * @param	string	the error level
	 * @param	string	the error message
	 * @param	bool	whether the error is a native PHP error
	 * @return	bool
	 */
	
	//2008-09-05 KUNIHARU Tsujioka UPDATE
	
	function write_log($level = 'error', $msg, $php_error = FALSE)
	{		
		if ($this->_enabled === FALSE)
		{
			return FALSE;
		}
	
		$level = strtoupper($level);
		
		if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
		{
			return FALSE;
		}
	
		$filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
		$message  = '';
		
		if ( ! file_exists($filepath))
		{
			$message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
		}
			
		if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
		{
			return FALSE;
		}

		$message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
		
		flock($fp, LOCK_EX);	
		fwrite($fp, $message);
		flock($fp, LOCK_UN);
		fclose($fp);
	    
	    $this->_logmessage[] = $message;    //add logmessage array
	    
		@chmod($filepath, FILE_WRITE_MODE); 
		return TRUE;
	}
	
	public function getLog()
	{
	    return $this->_logmessage;
	}

}

これでデバッグするときにプロファイラーをポップアップにして、その下にログを出力できます。
system/logs/にもログがかきだされています。

これ、どんどんPOPUPのほうに追記されていきますが、画面ごとにクリアできないかな。