<?php
function shutdown(){ //捕获错误
	$aError = error_get_last();
	if ((!empty( $aError)) && ($aError['type'] !== E_NOTICE)) {
		$date = date( 'Ymd',time());
        $dir = PATH_DAT . '/phperror';
        $file = $dir.'/' . $date . '.log';
		$error = '';
		if ( ! is_dir( $dir)) {
			mkdir( $dir,  0777);
            chgrp_chown($dir);
		}
		if ( ! file_exists( $file)) {
			touch($file) && chmod($file, PRODUCTION_SERVER ? 0664 : 0666);
            chgrp_chown($file);
            if(IS_DEBUF){
                $error = "<?php\nexit();\n";
            }
		}
		$error .= date( 'Y-m-d H:m:s',time()) . '---';
		$error .= 'Error:' . $aError['message'] . '--';
		$error .= 'File:' . $aError['file'] . '--';
		$error .= 'Line:' . $aError['line'];
        $error .= $error . "--shutdown";
        if(!IS_DEBUF){
            $data = ['log_level' => 'error', 'log_module' => 'shutdown_error', 'time' => date('Y-m-d H:i:s'), 'host_ip' => SERVER_INT_IP, 'content' => $error];
            @file_put_contents($file, json_encode($data)."\n", FILE_APPEND | LOCK_EX);
        }else{
            @file_put_contents( $file, $error . "--shutdown \n ", FILE_APPEND | LOCK_EX);
        }
		exit();
	}
}
function myErrorHandler($errno, $errstr, $errfile, $errline){
	if ($errno != E_NOTICE) {
		$date = date( 'Ymd');
		$dir = PATH_DAT . '/phperror';
		$file = $dir.'/' . $date . '.log';
		if ( ! is_dir( $dir)) {
			mkdir( $dir,  0775);
            chgrp_chown($dir);
		}
		$error = '';
		if ( ! file_exists( $file)) {
			touch($file) && chmod($file, PRODUCTION_SERVER ? 0664 : 0666);
            chgrp_chown($file);
            if(IS_DEBUF){
                $error = "<?php\nexit();\n";
            }
		}
		$error .= date( 'Y-m-d H:m:s',time()) . '---';
		$error .= 'Error:' . $errstr . '--';
		$error .= 'File:' . $errfile . '--';
		$error .= 'Line:' . $errline;
		$error .= "\n";
		
 		$backtrace = array();
 		$straces = debug_backtrace();
 		foreach ((array) $straces as $k => $v)
 		{
 			if ($k == 0) continue;
 			if(isset($v['file'])){
                $error .= sprintf("#%s %s:%s@%s Args:%s\n", $k, $v['file'], $v['line'], $v['function'], json_encode($v));
            }else{
                $file = PATH_DAT . 'phperror/' . $date . '.log';
                @file_put_contents( $file, json_encode($error) . "--myErrorHandler \n ", FILE_APPEND | LOCK_EX);
              	return true;
            }
 		}

		$file = PATH_DAT . 'phperror/' . $date . '.log';
        $error .= "--myErrorHandlerEnd";
        if(!IS_DEBUF) {
            $data = ['log_level' => 'error', 'log_module' => 'custom_error', 'time' => date('Y-m-d H:i:s'), 'host_ip' => SERVER_INT_IP, 'content' => $error];
            @file_put_contents($file, json_encode($data) . "\n", FILE_APPEND | LOCK_EX);
        }else{
            @file_put_contents( $file, $error . "--myErrorHandlerEnd \n ", FILE_APPEND | LOCK_EX);
        }
	}
}

/**
 * Notes:设置文件所属
 * User: wsc
 * Time: 2020/6/3 11:59
 * @param $f
 */
function chgrp_chown($f){
    if(function_exists('chgrp')&&php_sapi_name()=='cli'){//防止root创建www无权限
        @chgrp($f,'www');
        @chown($f,'www');
    }
}
//注册错误函数
set_error_handler( 'myErrorHandler');
register_shutdown_function( 'shutdown');
