error and exception handler class

This commit is contained in:
kremsy 2014-01-27 20:28:37 +01:00 committed by Steffen Schröder
parent 96a90b0b0c
commit 053649600d
3 changed files with 102 additions and 83 deletions

View File

@ -53,72 +53,6 @@ function logMessage($message) {
echo $message;
}
/**
* Get the prefix for the given error level
*
* @param int $errorLevel
* @return string
*/
function getErrorTag($errorLevel) {
if ($errorLevel == E_NOTICE) {
return '[PHP NOTICE]';
}
if ($errorLevel == E_WARNING) {
return '[PHP WARNING]';
}
if ($errorLevel == E_ERROR) {
return '[PHP ERROR]';
}
if ($errorLevel == E_USER_NOTICE) {
return '[ManiaControl NOTICE]';
}
if ($errorLevel == E_USER_WARNING) {
return '[ManiaControl WARNING]';
}
if ($errorLevel == E_USER_ERROR) {
return '[ManiaControl ERROR]';
}
return "[PHP {$errorLevel}]";
}
// Register error handler
set_error_handler('errorHandler', -1);
set_exception_handler('exceptionHandler');
/**
* Error Handler
*
* @param $errorNumber
* @param $errorString
* @param $errorFile
* @param $errorLine
* @return bool
*/
function errorHandler($errorNumber, $errorString, $errorFile, $errorLine) {
if (error_reporting() == 0) {
// Error suppressed
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
spl_autoload_register(function ($className) {

View File

@ -0,0 +1,79 @@
<?php
/**
* Error and Exception Manager Class
*
* @author steeffeen & kremsy
*/
class ErrorHandler {
/**
* Construct Error Handler
*/
public function __construct() {
set_error_handler(array(&$this, 'errorHandler'), -1);
set_exception_handler(array(&$this, 'exceptionHandler'));
}
/**
* ManiaControl ExceptionHandler
*
* @param Exception $ex
*/
public function exceptionHandler(Exception $ex) {
$message = "[ManiaControl EXCEPTION]: {$ex->getMessage()} Trace: {$ex->getTraceAsString()}!";
logMessage($message);
}
/**
* Error Handler
*
* @param $errorNumber
* @param $errorString
* @param $errorFile
* @param $errorLine
* @return bool
*/
public function errorHandler($errorNumber, $errorString, $errorFile, $errorLine) {
if (error_reporting() == 0) {
// Error suppressed
return false;
}
// Log error
$errorTag = $this->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;
}
/**
* Get the prefix for the given error level
*
* @param int $errorLevel
* @return string
*/
public function getErrorTag($errorLevel) {
if ($errorLevel == E_NOTICE) {
return '[PHP NOTICE]';
}
if ($errorLevel == E_WARNING) {
return '[PHP WARNING]';
}
if ($errorLevel == E_ERROR) {
return '[PHP ERROR]';
}
if ($errorLevel == E_USER_NOTICE) {
return '[ManiaControl NOTICE]';
}
if ($errorLevel == E_USER_WARNING) {
return '[ManiaControl WARNING]';
}
if ($errorLevel == E_USER_ERROR) {
return '[ManiaControl ERROR]';
}
return "[PHP {$errorLevel}]";
}
}

View File

@ -2,6 +2,7 @@
namespace ManiaControl;
use ErrorHandler;
use ManiaControl\Admin\ActionsMenu;
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackManager;
@ -62,6 +63,7 @@ class ManiaControl implements CommandListener {
public $settingManager = null;
public $statisticManager = null;
public $updateManager = null;
public $errorHandler = null;
/**
* Private properties
@ -72,6 +74,9 @@ class ManiaControl implements CommandListener {
* Construct ManiaControl
*/
public function __construct() {
//Construct Error Handler
$this->errorHandler = new ErrorHandler($this);
$this->log('Loading ManiaControl v' . self::VERSION . '...');
// Load config
@ -100,6 +105,7 @@ class ManiaControl implements CommandListener {
$this->commandManager->registerCommandListener('shutdown', $this, 'command_Shutdown', true);
}
/**
* Print a message to console and log
*