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; 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) {

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; 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
* *
@ -107,7 +113,7 @@ class ManiaControl implements CommandListener {
*/ */
public function log($message, $stripCodes = false) { public function log($message, $stripCodes = false) {
$date = date("d.M y H:i:s"); $date = date("d.M y H:i:s");
if($stripCodes) { if ($stripCodes) {
$message = Formatter::stripCodes($message); $message = Formatter::stripCodes($message);
} }
logMessage($date . ' ' . $message); logMessage($date . ' ' . $message);
@ -121,15 +127,15 @@ class ManiaControl implements CommandListener {
*/ */
public function getOS($compareOS = null) { public function getOS($compareOS = null) {
$windows = defined('PHP_WINDOWS_VERSION_MAJOR'); $windows = defined('PHP_WINDOWS_VERSION_MAJOR');
if($compareOS) { if ($compareOS) {
// Return bool whether OS equals $compareOS // Return bool whether OS equals $compareOS
if($compareOS == self::OS_WIN) { if ($compareOS == self::OS_WIN) {
return $windows; return $windows;
} }
return !$windows; return !$windows;
} }
// Return OS // Return OS
if($windows) { if ($windows) {
return self::OS_WIN; return self::OS_WIN;
} }
return self::OS_UNIX; return self::OS_UNIX;
@ -153,7 +159,7 @@ class ManiaControl implements CommandListener {
* @param Player $player * @param Player $player
*/ */
public function command_Restart(array $chatCallback, Player $player) { public function command_Restart(array $chatCallback, Player $player) {
if(!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { if (!AuthenticationManager::checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
$this->authenticationManager->sendNotAllowed($player); $this->authenticationManager->sendNotAllowed($player);
return; return;
} }
@ -167,7 +173,7 @@ class ManiaControl implements CommandListener {
* @param Player $player * @param Player $player
*/ */
public function command_Shutdown(array $chat, Player $player) { public function command_Shutdown(array $chat, Player $player) {
if(!$this->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { if (!$this->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
$this->authenticationManager->sendNotAllowed($player); $this->authenticationManager->sendNotAllowed($player);
return; return;
} }
@ -180,7 +186,7 @@ class ManiaControl implements CommandListener {
* @param string $message * @param string $message
*/ */
public function quit($message = null) { public function quit($message = null) {
if($message) { if ($message) {
$this->log($message); $this->log($message);
} }
exit(); exit();
@ -217,7 +223,7 @@ class ManiaControl implements CommandListener {
// Announce restart // Announce restart
$this->chat->sendInformation('Restarting ManiaControl...'); $this->chat->sendInformation('Restarting ManiaControl...');
if($message) { if ($message) {
$this->log($message); $this->log($message);
} }
@ -227,7 +233,7 @@ class ManiaControl implements CommandListener {
$this->log('Restarting ManiaControl!'); $this->log('Restarting ManiaControl!');
// Execute start script in background // Execute start script in background
if($this->getOS(self::OS_UNIX)) { if ($this->getOS(self::OS_UNIX)) {
$command = 'sh ' . escapeshellarg(ManiaControlDir . '/ManiaControl.sh') . ' > /dev/null &'; $command = 'sh ' . escapeshellarg(ManiaControlDir . '/ManiaControl.sh') . ' > /dev/null &';
exec($command); exec($command);
} else { } else {
@ -274,7 +280,7 @@ class ManiaControl implements CommandListener {
// Yield for next tick // Yield for next tick
$loopEnd = microtime(true); $loopEnd = microtime(true);
$sleepTime = 300000 - $loopEnd + $loopStart; $sleepTime = 300000 - $loopEnd + $loopStart;
if($sleepTime > 0) { if ($sleepTime > 0) {
usleep($sleepTime); usleep($sleepTime);
} }
} }
@ -289,12 +295,12 @@ class ManiaControl implements CommandListener {
private function connect() { private function connect() {
// Load remote client // Load remote client
$host = $this->config->server->xpath('host'); $host = $this->config->server->xpath('host');
if(!$host) { if (!$host) {
trigger_error("Invalid server configuration (host).", E_USER_ERROR); trigger_error("Invalid server configuration (host).", E_USER_ERROR);
} }
$host = (string)$host[0]; $host = (string)$host[0];
$port = $this->config->server->xpath('port'); $port = $this->config->server->xpath('port');
if(!$host) { if (!$host) {
trigger_error("Invalid server configuration (port).", E_USER_ERROR); trigger_error("Invalid server configuration (port).", E_USER_ERROR);
} }
$port = (string)$port[0]; $port = (string)$port[0];
@ -302,12 +308,12 @@ class ManiaControl implements CommandListener {
$this->log("Connecting to server at {$host}:{$port}..."); $this->log("Connecting to server at {$host}:{$port}...");
$login = $this->config->server->xpath('login'); $login = $this->config->server->xpath('login');
if(!$login) { if (!$login) {
trigger_error("Invalid server configuration (login).", E_USER_ERROR); trigger_error("Invalid server configuration (login).", E_USER_ERROR);
} }
$login = (string)$login[0]; $login = (string)$login[0];
$pass = $this->config->server->xpath('pass'); $pass = $this->config->server->xpath('pass');
if(!$pass) { if (!$pass) {
trigger_error("Invalid server configuration (password).", E_USER_ERROR); trigger_error("Invalid server configuration (password).", E_USER_ERROR);
} }
$pass = (string)$pass[0]; $pass = (string)$pass[0];
@ -326,7 +332,7 @@ class ManiaControl implements CommandListener {
} }
// Wait for server to be ready // Wait for server to be ready
if(!$this->server->waitForStatus(4)) { if (!$this->server->waitForStatus(4)) {
trigger_error("Server couldn't get ready!", E_USER_ERROR); trigger_error("Server couldn't get ready!", E_USER_ERROR);
} }
@ -343,7 +349,7 @@ class ManiaControl implements CommandListener {
$this->client->sendHideManialinkPage(); $this->client->sendHideManialinkPage();
// Enable script callbacks if needed // Enable script callbacks if needed
if($this->server->getGameMode() != 0) { if ($this->server->getGameMode() != 0) {
return; return;
} }
@ -354,7 +360,7 @@ class ManiaControl implements CommandListener {
return; return;
} }
if(!array_key_exists('S_UseScriptCallbacks', $scriptSettings)) { if (!array_key_exists('S_UseScriptCallbacks', $scriptSettings)) {
return; return;
} }