Add error and shutdown handlers

This commit is contained in:
netkas 2024-10-28 19:56:13 -04:00
parent 735dc7b33e
commit 6f61db996c
2 changed files with 27 additions and 1 deletions

View file

@ -345,7 +345,7 @@
$args = Parse::getArguments(); $args = Parse::getArguments();
$LOGGING_DIRECTORY = ($args['logging-directory'] ?? getenv('LOGGING_DIRECTORY') ?? null); $LOGGING_DIRECTORY = ($args['logging-directory'] ?? getenv('LOGGING_DIRECTORY') ?? null);
if($LOGGING_DIRECTORY === null) if($LOGGING_DIRECTORY === null || $LOGGING_DIRECTORY === false || strlen($LOGGING_DIRECTORY) === 0)
{ {
return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'logs'; return sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'logs';
} }

View file

@ -4,6 +4,7 @@
namespace LogLib; namespace LogLib;
use ErrorException;
use Exception; use Exception;
use InvalidArgumentException; use InvalidArgumentException;
use LogLib\Classes\Utilities; use LogLib\Classes\Utilities;
@ -223,6 +224,31 @@
return; return;
} }
}); });
// Register error handler
set_error_handler(static function($errno, $errstr, $errfile, $errline)
{
// Convert error to exception and throw it
try
{
self::warning('Runtime', sprintf("%s:%s (%s) %s", $errfile, $errline, $errno, $errstr));
}
catch(Exception)
{
return;
}
});
register_shutdown_function(static function()
{
$error = error_get_last();
if ($error !== null && ($error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR)))
{
// Convert fatal error to exception and handle it
$exception = new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']);
self::error('Fatal', $exception->getMessage(), $exception);
}
});
} }
/** /**