DUPLICATOR_MAX_LOG_SIZE) { $backup_log_filepath = self::GetBackupTraceFilepath(); if (file_exists($backup_log_filepath)) { if (@unlink($backup_log_filepath) === false) { self::errLog("Couldn't delete backup log $backup_log_filepath"); } } if (@rename($log_filepath, $backup_log_filepath) === false) { self::errLog("Couldn't rename log $log_filepath to $backup_log_filepath"); } } if (@file_put_contents($log_filepath, $formatted_logging_message, FILE_APPEND) === false) { // Not en error worth reporting } } } class DUP_Handler { const MODE_OFF = 0; // don't write in log const MODE_LOG = 1; // write errors in log file const MODE_VAR = 2; // put php errors in $varModeLog static var const SHUTDOWN_TIMEOUT = 'tm'; /** * * @var bool */ private static $initialized = false; /** * * @var array */ private static $shutdownReturns = array( 'tm' => 'timeout' ); /** * * @var int */ private static $handlerMode = self::MODE_LOG; /** * * @var bool // print code reference and errno at end of php error line [CODE:10|FILE:test.php|LINE:100] */ private static $codeReference = true; /** * * @var bool // print prefix in php error line [PHP ERR][WARN] MSG: ..... */ private static $errPrefix = true; /** * * @var string // php errors in MODE_VAR */ private static $varModeLog = ''; /** * This function only initializes the error handler the first time it is called */ public static function init_error_handler() { if (!self::$initialized) { @set_error_handler(array(__CLASS__, 'error')); @register_shutdown_function(array(__CLASS__, 'shutdown')); self::$initialized = true; } } /** * Error handler * * @param integer $errno Error level * @param string $errstr Error message * @param string $errfile Error file * @param integer $errline Error line * @return void */ public static function error($errno, $errstr, $errfile, $errline) { switch (self::$handlerMode) { case self::MODE_OFF: if ($errno == E_ERROR) { $log_message = self::getMessage($errno, $errstr, $errfile, $errline); DUP_Log::error($log_message); } break; case self::MODE_VAR: self::$varModeLog .= self::getMessage($errno, $errstr, $errfile, $errline)."\n"; break; case self::MODE_LOG: default: switch ($errno) { case E_ERROR : $log_message = self::getMessage($errno, $errstr, $errfile, $errline); DUP_Log::error($log_message); break; case E_NOTICE : case E_WARNING : default : $log_message = self::getMessage($errno, $errstr, $errfile, $errline); DUP_Log::Info($log_message); break; } } } private static function getMessage($errno, $errstr, $errfile, $errline) { $result = ''; if (self::$errPrefix) { $result = '[PHP ERR]'; switch ($errno) { case E_ERROR : $result .= '[FATAL]'; break; case E_WARNING : $result .= '[WARN]'; break; case E_NOTICE : $result .= '[NOTICE]'; break; default : $result .= '[ISSUE]'; break; } $result .= ' MSG:'; } $result .= $errstr; if (self::$codeReference) { $result .= ' [CODE:'.$errno.'|FILE:'.$errfile.'|LINE:'.$errline.']'; $result .= "\n".wp_debug_backtrace_summary(); } return $result; } /** * if setMode is called without params set as default * * @param int $mode * @param bool $errPrefix // print prefix in php error line [PHP ERR][WARN] MSG: ..... * @param bool $codeReference // print code reference and errno at end of php error line [CODE:10|FILE:test.php|LINE:100] */ public static function setMode($mode = self::MODE_LOG, $errPrefix = true, $codeReference = true) { switch ($mode) { case self::MODE_OFF: case self::MODE_VAR: self::$handlerMode = $mode; break; case self::MODE_LOG: default: self::$handlerMode = self::MODE_LOG; } self::$varModeLog = ''; self::$errPrefix = $errPrefix; self::$codeReference = $codeReference; } /** * * @return string // return var log string in MODE_VAR */ public static function getVarLog() { return self::$varModeLog; } /** * * @return string // return var log string in MODE_VAR and clean var */ public static function getVarLogClean() { $result = self::$varModeLog; self::$varModeLog = ''; return $result; } /** * * @param string $status // timeout * @param string */ public static function setShutdownReturn($status, $str) { self::$shutdownReturns[$status] = $str; } /** * Shutdown handler * * @return void */ public static function shutdown() { if (($error = error_get_last())) { if (preg_match('/^Maximum execution time (?:.+) exceeded$/i', $error['message'])) { echo self::$shutdownReturns[self::SHUTDOWN_TIMEOUT]; } self::error($error['type'], $error['message'], $error['file'], $error['line']); } } }