error and exception handler class
This commit is contained in:
		
				
					committed by
					
						 Steffen Schröder
						Steffen Schröder
					
				
			
			
				
	
			
			
			
						parent
						
							96a90b0b0c
						
					
				
				
					commit
					053649600d
				
			| @@ -53,72 +53,6 @@ function logMessage($message) { | |||||||
| 	echo $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 | // Autoload Function that loads ManiaControl Class Files on Demand | ||||||
| spl_autoload_register(function ($className) { | spl_autoload_register(function ($className) { | ||||||
|   | |||||||
							
								
								
									
										79
									
								
								application/core/ErrorHandler.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								application/core/ErrorHandler.php
									
									
									
									
									
										Normal 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}]"; | ||||||
|  | 	} | ||||||
|  | }  | ||||||
| @@ -2,6 +2,7 @@ | |||||||
|  |  | ||||||
| namespace ManiaControl; | namespace ManiaControl; | ||||||
|  |  | ||||||
|  | use ErrorHandler; | ||||||
| use ManiaControl\Admin\ActionsMenu; | use ManiaControl\Admin\ActionsMenu; | ||||||
| use ManiaControl\Admin\AuthenticationManager; | use ManiaControl\Admin\AuthenticationManager; | ||||||
| use ManiaControl\Callbacks\CallbackManager; | use ManiaControl\Callbacks\CallbackManager; | ||||||
| @@ -62,6 +63,7 @@ class ManiaControl implements CommandListener { | |||||||
| 	public $settingManager = null; | 	public $settingManager = null; | ||||||
| 	public $statisticManager = null; | 	public $statisticManager = null; | ||||||
| 	public $updateManager = null; | 	public $updateManager = null; | ||||||
|  | 	public $errorHandler = null; | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Private properties | 	 * Private properties | ||||||
| @@ -72,6 +74,9 @@ class ManiaControl implements CommandListener { | |||||||
| 	 * Construct ManiaControl | 	 * Construct ManiaControl | ||||||
| 	 */ | 	 */ | ||||||
| 	public function __construct() { | 	public function __construct() { | ||||||
|  | 		//Construct Error Handler | ||||||
|  | 		$this->errorHandler = new ErrorHandler($this); | ||||||
|  |  | ||||||
| 		$this->log('Loading ManiaControl v' . self::VERSION . '...'); | 		$this->log('Loading ManiaControl v' . self::VERSION . '...'); | ||||||
|  |  | ||||||
| 		// Load config | 		// Load config | ||||||
| @@ -100,6 +105,7 @@ class ManiaControl implements CommandListener { | |||||||
| 		$this->commandManager->registerCommandListener('shutdown', $this, 'command_Shutdown', true); | 		$this->commandManager->registerCommandListener('shutdown', $this, 'command_Shutdown', true); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  |  | ||||||
| 	/** | 	/** | ||||||
| 	 * Print a message to console and log | 	 * Print a message to console and log | ||||||
| 	 * | 	 * | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user