- Improved Logging
- Error handler - Shutdown function - Added OnShutdown callback
This commit is contained in:
parent
c97b4166f2
commit
e9e1fb94d9
@ -1,29 +1,89 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// Run configuration
|
||||||
|
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_PID', true); // Use current process id as suffix for log file name in logs folder
|
||||||
|
|
||||||
// Define base dir
|
// Define base dir
|
||||||
define('ManiaControlDir', __DIR__);
|
define('ManiaControlDir', __DIR__);
|
||||||
|
|
||||||
// Set process settings
|
// Set process settings
|
||||||
ini_set('memory_limit', '128M');
|
ini_set('memory_limit', '64M');
|
||||||
if (function_exists('date_default_timezone_get') && function_exists('date_default_timezone_set')) {
|
if (function_exists('date_default_timezone_get') && function_exists('date_default_timezone_set')) {
|
||||||
date_default_timezone_set(@date_default_timezone_get());
|
date_default_timezone_set(@date_default_timezone_get());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error handling
|
// Build log file name
|
||||||
ini_set('log_errors', true);
|
$logFileName = ManiaControlDir . '/logs/';
|
||||||
ini_set('display_errors', '1');
|
if (!is_dir($logFileName)) {
|
||||||
ini_set('error_reporting', -1);
|
mkdir($logFileName);
|
||||||
ini_set('display_startup_errors', true);
|
|
||||||
if (!is_dir('logs')) {
|
|
||||||
mkdir('logs');
|
|
||||||
}
|
}
|
||||||
ini_set('error_log', 'logs/ManiaControl_' . getmypid() . '.log');
|
$logFileName .= '/ManiaControl';
|
||||||
|
if (LOG_NAME_USE_DATE) $logFileName .= '_' . date('Y-m-d');
|
||||||
|
if (LOG_NAME_USE_PID) $logFileName .= '_' . getmypid();
|
||||||
|
$logFileName .= '.log';
|
||||||
|
define('LOG_FILE', $logFileName);
|
||||||
|
|
||||||
// Load ManiaControl class
|
// Delete old current log file
|
||||||
require_once __DIR__ . '/core/ManiaControl.php';
|
if (LOG_WRITE_CURRENT_FILE) {
|
||||||
|
$currentLogFileName = ManiaControlDir . '/' . LOG_WRITE_CURRENT_FILE;
|
||||||
|
if (file_exists($currentLogFileName) && is_writable($currentLogFileName)) {
|
||||||
|
unlink($currentLogFileName);
|
||||||
|
}
|
||||||
|
define('LOG_CURRENT_FILE', $currentLogFileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log function
|
||||||
|
function logMessage($message) {
|
||||||
|
$message .= PHP_EOL;
|
||||||
|
file_put_contents(LOG_CURRENT_FILE, $message, FILE_APPEND);
|
||||||
|
file_put_contents(LOG_FILE, $message, FILE_APPEND);
|
||||||
|
echo $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error level parse function
|
||||||
|
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(
|
||||||
|
function ($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;
|
||||||
|
}, -1);
|
||||||
|
|
||||||
// Start ManiaControl
|
// Start ManiaControl
|
||||||
error_log('Loading ManiaControl v' . ManiaControl\ManiaControl::VERSION . '...');
|
require_once __DIR__ . '/core/ManiaControl.php';
|
||||||
|
|
||||||
$maniaControl = new ManiaControl\ManiaControl();
|
$maniaControl = new ManiaControl\ManiaControl();
|
||||||
$maniaControl->run();
|
$maniaControl->run();
|
||||||
|
@ -18,6 +18,7 @@ class CallbackManager {
|
|||||||
const CB_MC_5_SECOND = 'ManiaControl.5Second';
|
const CB_MC_5_SECOND = 'ManiaControl.5Second';
|
||||||
const CB_MC_1_MINUTE = 'ManiaControl.1Minute';
|
const CB_MC_1_MINUTE = 'ManiaControl.1Minute';
|
||||||
const CB_MC_ONINIT = 'ManiaControl.OnInit';
|
const CB_MC_ONINIT = 'ManiaControl.OnInit';
|
||||||
|
const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown';
|
||||||
const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated';
|
const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated';
|
||||||
const CB_MC_BEGINMAP = 'ManiaControl.BeginMap';
|
const CB_MC_BEGINMAP = 'ManiaControl.BeginMap';
|
||||||
const CB_MC_ENDMAP = 'ManiaControl.EndMap';
|
const CB_MC_ENDMAP = 'ManiaControl.EndMap';
|
||||||
|
@ -86,6 +86,8 @@ class ManiaControl implements CommandListener {
|
|||||||
* Construct ManiaControl
|
* Construct ManiaControl
|
||||||
*/
|
*/
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
logMessage('Loading ManiaControl v' . self::VERSION . '...');
|
||||||
|
|
||||||
$this->database = new Database($this);
|
$this->database = new Database($this);
|
||||||
$this->callbackManager = new CallbackManager($this);
|
$this->callbackManager = new CallbackManager($this);
|
||||||
$this->settingManager = new SettingManager($this);
|
$this->settingManager = new SettingManager($this);
|
||||||
@ -119,8 +121,8 @@ class ManiaControl implements CommandListener {
|
|||||||
/**
|
/**
|
||||||
* Send ManiaControl version
|
* Send ManiaControl version
|
||||||
*
|
*
|
||||||
* @param array $chat
|
* @param array $chat
|
||||||
* @param Player $player
|
* @param Player $player
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function command_Version(array $chat, Player $player) {
|
public function command_Version(array $chat, Player $player) {
|
||||||
@ -134,6 +136,9 @@ class ManiaControl implements CommandListener {
|
|||||||
* @param string $message
|
* @param string $message
|
||||||
*/
|
*/
|
||||||
public function quit($message = '') {
|
public function quit($message = '') {
|
||||||
|
// OnShutdown callback
|
||||||
|
$this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN));
|
||||||
|
|
||||||
if ($this->client) {
|
if ($this->client) {
|
||||||
// Announce quit
|
// Announce quit
|
||||||
$this->chat->sendInformation('ManiaControl shutting down.');
|
$this->chat->sendInformation('ManiaControl shutting down.');
|
||||||
@ -144,7 +149,7 @@ class ManiaControl implements CommandListener {
|
|||||||
|
|
||||||
// Log quit reason
|
// Log quit reason
|
||||||
if ($message) {
|
if ($message) {
|
||||||
error_log($message);
|
logMessage($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown
|
// Shutdown
|
||||||
@ -152,7 +157,7 @@ class ManiaControl implements CommandListener {
|
|||||||
$this->client->Terminate();
|
$this->client->Terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
error_log("Quitting ManiaControl!");
|
logMessage('Quitting ManiaControl!');
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +165,7 @@ class ManiaControl implements CommandListener {
|
|||||||
* Run ManiaControl
|
* Run ManiaControl
|
||||||
*/
|
*/
|
||||||
public function run() {
|
public function run() {
|
||||||
error_log('Starting ManiaControl v' . self::VERSION . '!');
|
logMessage('Starting ManiaControl v' . self::VERSION . '!');
|
||||||
|
|
||||||
// Load plugins
|
// Load plugins
|
||||||
$this->pluginManager->loadPlugins();
|
$this->pluginManager->loadPlugins();
|
||||||
@ -168,13 +173,16 @@ class ManiaControl implements CommandListener {
|
|||||||
// Connect to server
|
// Connect to server
|
||||||
$this->connect();
|
$this->connect();
|
||||||
|
|
||||||
|
// Register shutdown handler
|
||||||
|
register_shutdown_function(array($this, 'quit'));
|
||||||
|
|
||||||
// Loading finished
|
// Loading finished
|
||||||
error_log("Loading completed!");
|
logMessage('Loading completed!');
|
||||||
|
|
||||||
// Announce ManiaControl
|
// Announce ManiaControl
|
||||||
$this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');
|
$this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!');
|
||||||
|
|
||||||
// OnInit
|
// OnInit callback
|
||||||
$this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONINIT, array(CallbackManager::CB_MC_ONINIT));
|
$this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONINIT, array(CallbackManager::CB_MC_ONINIT));
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
@ -196,7 +204,7 @@ class ManiaControl implements CommandListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Shutdown
|
// Shutdown
|
||||||
$this->client->Terminate();
|
$this->quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -213,7 +221,7 @@ class ManiaControl implements CommandListener {
|
|||||||
if (!$host) trigger_error("Invalid server configuration (port).", E_USER_ERROR);
|
if (!$host) trigger_error("Invalid server configuration (port).", E_USER_ERROR);
|
||||||
$port = (string) $port[0];
|
$port = (string) $port[0];
|
||||||
|
|
||||||
error_log("Connecting to server at {$host}:{$port}...");
|
logMessage("Connecting to server at {$host}:{$port}...");
|
||||||
|
|
||||||
// Connect
|
// Connect
|
||||||
if (!$this->client->InitWithIp($host, $port, 20)) {
|
if (!$this->client->InitWithIp($host, $port, 20)) {
|
||||||
@ -249,26 +257,22 @@ class ManiaControl implements CommandListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Connect finished
|
// Connect finished
|
||||||
error_log("Server connection successfully established!");
|
logMessage("Server connection successfully established!");
|
||||||
|
|
||||||
// Enable script callbacks if needed
|
// Enable script callbacks if needed
|
||||||
if ($this->server->getGameMode() === 0) {
|
if ($this->server->getGameMode() != 0) return;
|
||||||
if (!$this->client->query('GetModeScriptSettings')) {
|
if (!$this->client->query('GetModeScriptSettings')) {
|
||||||
trigger_error("Couldn't get mode script settings. " . $this->getClientErrorText());
|
trigger_error("Couldn't get mode script settings. " . $this->getClientErrorText());
|
||||||
}
|
return;
|
||||||
else {
|
|
||||||
$scriptSettings = $this->client->getResponse();
|
|
||||||
if (array_key_exists('S_UseScriptCallbacks', $scriptSettings)) {
|
|
||||||
$scriptSettings['S_UseScriptCallbacks'] = true;
|
|
||||||
if (!$this->client->query('SetModeScriptSettings', $scriptSettings)) {
|
|
||||||
trigger_error("Couldn't set mode script settings to enable script callbacks. " . $this->getClientErrorText());
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
error_log("Script callbacks successfully enabled.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
$scriptSettings = $this->client->getResponse();
|
||||||
|
if (!array_key_exists('S_UseScriptCallbacks', $scriptSettings)) return;
|
||||||
|
$scriptSettings['S_UseScriptCallbacks'] = true;
|
||||||
|
if (!$this->client->query('SetModeScriptSettings', $scriptSettings)) {
|
||||||
|
trigger_error("Couldn't set mode script settings to enable script callbacks. " . $this->getClientErrorText());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
logMessage('Script callbacks successfully enabled.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,14 +287,14 @@ class Server {
|
|||||||
$waitBegin = time();
|
$waitBegin = time();
|
||||||
$maxWaitTime = 20;
|
$maxWaitTime = 20;
|
||||||
$lastStatus = $response['Name'];
|
$lastStatus = $response['Name'];
|
||||||
error_log("Waiting for server to reach status {$statusCode}...");
|
logMessage("Waiting for server to reach status {$statusCode}...");
|
||||||
error_log("Current Status: {$lastStatus}");
|
logMessage("Current Status: {$lastStatus}");
|
||||||
while ($response['Code'] !== 4) {
|
while ($response['Code'] !== 4) {
|
||||||
sleep(1);
|
sleep(1);
|
||||||
$this->maniaControl->client->query('GetStatus');
|
$this->maniaControl->client->query('GetStatus');
|
||||||
$response = $this->maniaControl->client->getResponse();
|
$response = $this->maniaControl->client->getResponse();
|
||||||
if ($lastStatus !== $response['Name']) {
|
if ($lastStatus !== $response['Name']) {
|
||||||
error_log("New Status: " . $response['Name']);
|
logMessage("New Status: " . $response['Name']);
|
||||||
$lastStatus = $response['Name'];
|
$lastStatus = $response['Name'];
|
||||||
}
|
}
|
||||||
if (time() - $maxWaitTime > $waitBegin) {
|
if (time() - $maxWaitTime > $waitBegin) {
|
||||||
|
Loading…
Reference in New Issue
Block a user