improved error handler, added exception handler

This commit is contained in:
kremsy 2014-01-27 20:16:20 +01:00 committed by Steffen Schröder
parent a18fe5c96f
commit 96a90b0b0c

View File

@ -1,6 +1,7 @@
<?php <?php
// Run configuration // Run configuration
define('LOG_WRITE_CURRENT_FILE', 'ManiaControl.log'); // Write current log to extra file in base dir define('LOG_WRITE_CURRENT_FILE', 'ManiaControl.log'); // Write current log to extra file in base dir
define('LOG_NAME_USE_DATE', true); // Use current date as suffix for log file name in logs folder define('LOG_NAME_USE_DATE', true); // Use current date as suffix for log file name in logs folder
define('LOG_NAME_USE_PID', true); // Use current process id as suffix for log file name in logs folder define('LOG_NAME_USE_PID', true); // Use current process id as suffix for log file name in logs folder
@ -20,8 +21,12 @@ if (!is_dir($logFileName)) {
mkdir($logFileName); mkdir($logFileName);
} }
$logFileName .= '/ManiaControl'; $logFileName .= '/ManiaControl';
if (LOG_NAME_USE_DATE) $logFileName .= '_' . date('Y-m-d'); if (LOG_NAME_USE_DATE) {
if (LOG_NAME_USE_PID) $logFileName .= '_' . getmypid(); $logFileName .= '_' . date('Y-m-d');
}
if (LOG_NAME_USE_PID) {
$logFileName .= '_' . getmypid();
}
$logFileName .= '.log'; $logFileName .= '.log';
define('LOG_FILE', $logFileName); define('LOG_FILE', $logFileName);
@ -41,7 +46,9 @@ if (LOG_WRITE_CURRENT_FILE) {
*/ */
function logMessage($message) { function logMessage($message) {
$message .= PHP_EOL; $message .= PHP_EOL;
if (defined('LOG_CURRENT_FILE')) file_put_contents(LOG_CURRENT_FILE, $message, FILE_APPEND); if (defined('LOG_CURRENT_FILE')) {
file_put_contents(LOG_CURRENT_FILE, $message, FILE_APPEND);
}
file_put_contents(LOG_FILE, $message, FILE_APPEND); file_put_contents(LOG_FILE, $message, FILE_APPEND);
echo $message; echo $message;
} }
@ -75,33 +82,53 @@ function getErrorTag($errorLevel) {
} }
// Register error handler // Register error handler
set_error_handler( set_error_handler('errorHandler', -1);
function ($errorNumber, $errorString, $errorFile, $errorLine) { set_exception_handler('exceptionHandler');
if (error_reporting() == 0) {
// Error suppressed /**
return false; * Error Handler
} *
// Log error * @param $errorNumber
$errorTag = getErrorTag($errorNumber); * @param $errorString
$message = "{$errorTag}: {$errorString} in File '{$errorFile}' on Line {$errorLine}!"; * @param $errorFile
logMessage($message); * @param $errorLine
if ($errorNumber == E_ERROR || $errorNumber == E_USER_ERROR) { * @return bool
logMessage('Stopping execution...'); */
exit(); function errorHandler($errorNumber, $errorString, $errorFile, $errorLine) {
} if (error_reporting() == 0) {
return false; // Error suppressed
}, -1); return false;
}
// Log error
$errorTag = getErrorTag($errorNumber);
$message = "{$errorTag}: {$errorString} in File '{$errorFile}' on Line {$errorLine}!";
logMessage($message);
if ($errorNumber == E_ERROR || $errorNumber == E_USER_ERROR) {
logMessage('Stopping execution...');
exit();
}
return false;
}
/**
* Exception Handler
*
* @param Exception $ex
*/
function exceptionHandler(Exception $ex) {
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()} Trace: {$ex->getTraceAsString()}!";
logMessage($message);
}
// Autoload Function that loads ManiaControl Class Files on Demand // Autoload Function that loads ManiaControl Class Files on Demand
spl_autoload_register( spl_autoload_register(function ($className) {
function ($className) { $classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className);
$classPath = str_replace('\\', DIRECTORY_SEPARATOR, $className); $classPath = preg_replace('/ManiaControl/', 'core', $classPath, 1);
$classPath = preg_replace('/ManiaControl/', 'core', $classPath, 1); $filePath = ManiaControlDir . DIRECTORY_SEPARATOR . $classPath . '.php';
$filePath = ManiaControlDir . DIRECTORY_SEPARATOR . $classPath . '.php'; if (file_exists($filePath)) {
if (file_exists($filePath)) { require_once $filePath;
require_once $filePath; }
} });
});
// Start ManiaControl // Start ManiaControl
$maniaControl = new \ManiaControl\ManiaControl(); $maniaControl = new \ManiaControl\ManiaControl();