if ( !defined('ML_ERROR')) define('ML_ERROR' ,E_USER_ERROR ,true);
if ( !defined('ML_WARNING'))define('ML_WARNING' ,E_USER_WARNING ,true);
if ( !defined('ML_NOTICE')) define('ML_NOTICE' ,E_USER_NOTICE ,true);
if ( !defined('ML_ALL')) define('ML_ALL',ML_ERROR|ML_WARNING|ML_NOTICE,true);
if ( !class_exists('MLog') ){
if ( !defined('DEBUG_Mlog') ) define('DEBUG_MLog',false,true);
if ( !defined('DEBUG_MLog_ls') ) define('DEBUG_MLog_ls',"<br>\n",true);
class MLog {
private static $mlog_init;
private static $mlog_log_mask;
private static $mlog_debug;
public function setDebug($flag){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return $this->_setDebug($flag);
}
public function getDebug(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return $this->_getDebug();
}
public function isDebug(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return $this->_getDebug();
}
public function setLogMask($mask){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return $this->_setLogMask($mask);
}
public function getLogMask(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return $this->_getLogMask();
}
public function setLogFile($filename=null){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
ini_set('error_log',$filename);
return true;
}
public function mlog(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
$argc = func_num_args();
if ( $argc == 0 ){
return false;
}
$argv = func_get_args();
$type = ML_NOTICE;
if ( $argc > 1 ){
if ( $argv[1] && ($argv[1] == ML_ERROR || $argv[1] == ML_WARNING
|| $argv[1] == ML_NOTICE) )
{
$type = $argv[1];
}
array_splice($argv,1,1);
}
if ( $type & self::$mlog_log_mask ){
$msg = call_user_func_array('sprintf',$argv);
error_log($this->_formatMessage($msg,$type));
if ( $this->isDebug() ){
echo $this->_formatMessage($msg,$type,true);
}
}
return true;
}
// private or hidden methods
protected function _formatMessage($msg,$level,$for_echo=false){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
$p = 'UNKNOWN';
switch ($level){
case ML_ERROR:
$p = 'ERROR';
break;
case ML_WARNING:
$p = 'WARNING';
break;
case ML_NOTICE:
$p = 'NOTICE';
break;
default:
}
if ($for_echo){
return "<b>$p</b>: $msg<br>\n";
}
/*
$stack = debug_backtrace();
$id = '';
return "$p $id $msg";
*/
return "$p $msg";
}
private function _setDebug($flag){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
ini_set('display_errors',($flag ? true : false));
return self::$mlog_debug = $flag ? true : false;
}
private function _getDebug(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return self::$mlog_debug ? true : false;
}
private function _setLogMask($mask){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return self::$mlog_log_mask = $mask;
}
private function _getLogMask(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return self::$mlog_log_mask;
}
protected function _isInitialized(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
return self::$mlog_init;
}
public function __construct(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
if ( !self::$mlog_init ){
ini_set('track_errors',1);
ini_set('log_errors',true);
ini_set('html_errors',false);
ini_set('display_errors',($flag ? true : false));
self::$mlog_debug = false;
self::$mlog_log_mask = ML_ALL;
self::$mlog_init = true;
}
return;
}
// virtual
public function __destruct(){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
}
public function dumpHash($hash){
//DEBUG-BEG
if ( DEBUG_MLog ) echo __CLASS__,"::",__FUNCTION__,DEBUG_MLog_ls;
//DEBUG-END
$text = '';
while (list($k,$v) = each($hash)){
$text .= "$k => $v\n";
}
return $this->mlog("\n$text");
}
} // eof class MLog
function mlog_error_handle_function($errno,$errmsg,$file,$line,$vars){
$error_codes = array (
0x0001 => ML_ERROR,
0x0002 => ML_WARNING,
0x0004 => ML_ERROR,
0x0008 => ML_NOTICE,
0x0010 => ML_ERROR,
0x0020 => ML_WARNING,
0x0040 => ML_ERROR,
0x0080 => ML_WARNING,
0x0100 => ML_ERROR,
0x0200 => ML_WARNING,
0x0400 => ML_NOTICE
);
//if ( $error_codes[ $errno ] == ML_NOTICE ){
// echo "<pre>";
// print_r(debug_backtrace());
// echo "</pre>";
//}
// format message
$msg = "PHP in $file at $line say: $errmsg";
$mlog_instance = new MLog;
$mlog_instance->mlog($msg,$error_codes[$errno]);
return true;
}
error_reporting(0);
set_error_handler('mlog_error_handle_function');
}