<?php
/**
* Debug helper
*/
class Debug
{
/**
* Debug initiated?
*
* @var boolean
*/
static private $_initiated = false;
/**
* Initialize debugging
*/
static public function main()
{
/* Init once */
if (self::$_initiated)
{
return;
}
self::$_initiated = true;
/* Setup error/exception handling */
$handler = array(__CLASS__, IN_DEV ? 'devhdl' : 'prodhdl');
set_error_handler($handler);
set_exception_handler($handler);
}
/**
* Development mode handler
*/
static public function devhdl()
{
/* Init */
$msg = self::_parseError(func_get_args());
/* Build output */
$out = sprintf('<pre>%s</pre>', print_r($msg, 1));
/* Send */
die($out);
}
/**
* Production mode handler
*/
static public function prodHdl()
{
/* Init */
$msg = self::_parseError(func_get_args());
$sep = "\n" . str_repeat('-', 60) . "\n";
/* Log message */
$log = fopen(DEV_LOG, 'a+b');
flock($log, LOCK_EX);
fwrite($log, $msg . $sep);
flock($log, LOCK_UN);
fclose($log);
/* Send */
die('System error');
}
/**
* Error parser
*
* @param array $params <p>Input arguments</p>
* @return string <p>Error message</p>
*/
static private function _parseError($params)
{
/* Invalid call? */
if (!(is_array($params) && isset($params[0])))
{
die('System error');
}
/* Exception has been thrown? */
if ($params[0] instanceof Exception)
{
return $params[0]->getMessage() . "\n\n" . $params[0]->getTraceAsString();
}
/* Process error */
if (count($params) == 5)
{
if (!(error_reporting() & $params[0]))
{
return 'unhandlable_error';
}
return $params[1] . "\n\n" . $params[2] . ':' . $params[3];
}
return 'unknown_error';
}
}
?>
当IN_DEV == 1
- 工作正常时
当IN_DEV == 0
- 工作正常时,但fwrite会写两次日志消息
WinXP / SP3,PHP / 5.3
我应该如何解决这个问题?