Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/utility/logger.php
Show First 20 Lines • Show All 728 Lines • ▼ Show 20 Line(s) | |||||
$this->_logRecord['LogLevel'] = $log_level; | $this->_logRecord['LogLevel'] = $log_level; | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Writes prepared log to database or disk, when database isn't available | * Writes prepared log to database or disk, when database isn't available | ||||
* | * | ||||
* @param int $storage_medium | * @param integer $storage_medium Storage medium. | ||||
* @return bool|int | * @param boolean $check_origin Check error origin. | ||||
* @access public | * | ||||
* @throws InvalidArgumentException | * @return integer|boolean | ||||
* @throws InvalidArgumentException When unknown storage medium is given. | |||||
*/ | */ | ||||
public function write($storage_medium = self::LS_AUTOMATIC) | public function write($storage_medium = self::LS_AUTOMATIC, $check_origin = false) | ||||
{ | { | ||||
if ( !$this->_logRecord || $this->_logRecord['LogLevel'] > $this->_maxLogLevel || $this->_state == self::STATE_DISABLED ) { | if ( $check_origin && isset($this->_logRecord['LogSourceFilename']) ) { | ||||
// nothing to save OR less detailed logging requested OR disabled | $origin_allowed = self::isErrorOriginAllowed($this->_logRecord['LogSourceFilename']); | ||||
} | |||||
else { | |||||
$origin_allowed = true; | |||||
} | |||||
if ( !$this->_logRecord | |||||
|| $this->_logRecord['LogLevel'] > $this->_maxLogLevel | |||||
|| !$origin_allowed | |||||
|| $this->_state == self::STATE_DISABLED | |||||
) { | |||||
// Nothing to save OR less detailed logging requested OR origin not allowed OR disabled. | |||||
$this->_logRecord = array(); | |||||
return false; | return false; | ||||
} | } | ||||
$this->_logRecord['LogMemoryUsed'] = memory_get_usage(); | $this->_logRecord['LogMemoryUsed'] = memory_get_usage(); | ||||
$this->_logRecord['LogTimestamp'] = adodb_mktime(); | $this->_logRecord['LogTimestamp'] = adodb_mktime(); | ||||
$this->_logRecord['LogDate'] = adodb_date('Y-m-d H:i:s'); | $this->_logRecord['LogDate'] = adodb_date('Y-m-d H:i:s'); | ||||
if ( $storage_medium == self::LS_AUTOMATIC ) { | if ( $storage_medium == self::LS_AUTOMATIC ) { | ||||
▲ Show 20 Lines • Show All 298 Lines • ▼ Show 20 Line(s) | |||||
} | } | ||||
} | } | ||||
} | } | ||||
return $message; | return $message; | ||||
} | } | ||||
/** | /** | ||||
* Determines if error should be logged based on it's origin. | |||||
* | |||||
* @param string $file File. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public static function isErrorOriginAllowed($file) | |||||
{ | |||||
static $error_origin_regexp; | |||||
// Lazy detect error origins, because they're not available at construction time. | |||||
if ( !$error_origin_regexp ) { | |||||
$error_origins = array(); | |||||
$application = kApplication::Instance(); | |||||
foreach ( $application->ModuleInfo as $module_info ) { | |||||
$error_origins[] = preg_quote(rtrim($module_info['Path'], '/'), '/'); | |||||
} | |||||
$error_origins = array_unique($error_origins); | |||||
$error_origin_regexp = '/^' . preg_quote(FULL_PATH, '/') . '\/(' . implode('|', $error_origins) . ')\//'; | |||||
} | |||||
// Allow dynamically generated code. | |||||
if ( strpos($file, 'eval()\'d code') !== false ) { | |||||
return true; | |||||
} | |||||
// Allow known modules. | |||||
if ( preg_match('/^' . preg_quote(MODULES_PATH, '/') . '\//', $file) ) { | |||||
return preg_match($error_origin_regexp, $file) == 1; | |||||
} | |||||
// Don't allow Vendors. | |||||
if ( preg_match('/^' . preg_quote(FULL_PATH, '/') . '\/vendor\//', $file) ) { | |||||
return false; | |||||
} | |||||
// Allow everything else within main folder. | |||||
return preg_match('/^' . preg_quote(FULL_PATH, '/') . '\//', $file) == 1; | |||||
} | |||||
/** | |||||
* Parses database error message into error number, error message and sql that caused that error | * Parses database error message into error number, error message and sql that caused that error | ||||
* | * | ||||
* @static | * @static | ||||
* @param string $message | * @param string $message | ||||
* @return Array | * @return Array | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public static function parseDatabaseError($message) | public static function parseDatabaseError($message) | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Line(s) | |||||
* @return void | * @return void | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public function add($handler) | public function add($handler) | ||||
{ | { | ||||
$this->_handlers[] = $handler; | $this->_handlers[] = $handler; | ||||
} | } | ||||
/** | |||||
* Returns `true`, when no other error handlers should process this error. | |||||
* | |||||
* @param integer $errno Error code. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
protected function _handleFatalError($errno) | protected function _handleFatalError($errno) | ||||
{ | { | ||||
$debug_mode = defined('DEBUG_MODE') && DEBUG_MODE; | $debug_mode = defined('DEBUG_MODE') && DEBUG_MODE; | ||||
$skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING; | $skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING; | ||||
if ( !$this->_handlers || ($debug_mode && $skip_reporting) ) { | if ( !$this->_handlers || ($debug_mode && $skip_reporting) ) { | ||||
// when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request) | // when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request) | ||||
if ( $this->_isFatalError($errno) ) { | if ( $this->_isFatalError($errno) ) { | ||||
$this->_displayFatalError($errno); | $this->_displayFatalError($errno); | ||||
} | } | ||||
if ( !$this->_handlers ) { | if ( !$this->_handlers ) { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return null; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Determines if given error is a fatal | * Determines if given error is a fatal | ||||
* | * | ||||
* @abstract | * @abstract | ||||
* @param Exception|int $errno | * @param Exception|int $errno | ||||
* @return bool | * @return bool | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Line(s) | |||||
* @return bool | * @return bool | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public function handle($errno, $errstr, $errfile = null, $errline = null, $errcontext = Array ()) | public function handle($errno, $errstr, $errfile = null, $errline = null, $errcontext = Array ()) | ||||
{ | { | ||||
$log = $this->_logger->prepare()->addError($errno, $errstr, $errfile, $errline); | $log = $this->_logger->prepare()->addError($errno, $errstr, $errfile, $errline); | ||||
if ( $this->_handleFatalError($errno) ) { | if ( $this->_handleFatalError($errno) ) { | ||||
$log->write(); | $log->write(kLogger::LS_AUTOMATIC, !$this->_isFatalError($errno)); | ||||
return true; | return true; | ||||
} | } | ||||
$log->write(); | $log->write(kLogger::LS_AUTOMATIC, !$this->_isFatalError($errno)); | ||||
$res = false; | $res = false; | ||||
foreach ($this->_handlers as $handler) { | foreach ($this->_handlers as $handler) { | ||||
$res = call_user_func($handler, $errno, $errstr, $errfile, $errline, $errcontext); | $res = call_user_func($handler, $errno, $errstr, $errfile, $errline, $errcontext); | ||||
} | } | ||||
return $res; | return $res; | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |