Refactor to use RuntimeOptions in Console

This commit is contained in:
netkas 2024-10-26 00:29:15 -04:00
parent 8f413a9aae
commit bd9bf431e7
4 changed files with 63 additions and 43 deletions

View file

@ -10,6 +10,7 @@
use LogLib\Log; use LogLib\Log;
use LogLib\Objects\Event; use LogLib\Objects\Event;
use LogLib\Objects\Options; use LogLib\Objects\Options;
use LogLib\Objects\RuntimeOptions;
use RuntimeException; use RuntimeException;
use Throwable; use Throwable;
@ -22,18 +23,27 @@
/** /**
* Formats the application name with a color for the console * Formats the application name with a color for the console
* *
* @param string $application The application name * @param string $application_name The application name
* @return string The formatted application name * @return string The formatted application name
* @throws RuntimeException If unable to generate a random color for the application * @throws RuntimeException If unable to generate a random color for the application
*/ */
private static function formatAppColor(string $application): string private static function formatAppColor(string $application_name): string
{ {
if(!Log::getRuntimeOptions()->displayAnsi()) if($application_name == null)
{ {
return $application; $options = Log::getRuntimeOptions();
}
else
{
$options = Log::getOptions($application_name);
} }
if(!isset(self::$application_colors[$application])) if(!$options->displayAnsi())
{
return $application_name;
}
if(!isset(self::$application_colors[$application_name]))
{ {
$colors = ConsoleColors::BRIGHT_COLORS; $colors = ConsoleColors::BRIGHT_COLORS;
@ -43,25 +53,26 @@
} }
catch (Exception $e) catch (Exception $e)
{ {
throw new RuntimeException(sprintf('Unable to generate random color for application "%s"', $application), $e->getCode(), $e); throw new RuntimeException(sprintf('Unable to generate random color for application "%s"', $application_name), $e->getCode(), $e);
} }
self::$application_colors[$application] = $color; self::$application_colors[$application_name] = $color;
} }
return self::color($application, self::$application_colors[$application]); return self::color($options, $application_name, self::$application_colors[$application_name]);
} }
/** /**
* Applies a specified color to the given text, using ANSI escape sequences. * Applies a specified color to the given text, using ANSI escape sequences.
* *
* @param RuntimeOptions $options The runtime options object.
* @param string $text The text to apply the color to. * @param string $text The text to apply the color to.
* @param ConsoleColors $color The ANSI color code to apply to the text. * @param ConsoleColors $color The ANSI color code to apply to the text.
* @return string The text with the specified color applied. * @return string The text with the specified color applied.
*/ */
private static function color(string $text, ConsoleColors $color): string private static function color(RuntimeOptions $options, string $text, ConsoleColors $color): string
{ {
if(!Log::getRuntimeOptions()->displayAnsi()) if(!$options->displayAnsi())
{ {
return $text; return $text;
} }
@ -72,12 +83,13 @@
/** /**
* Colorizes the log message based on the event level using ANSI escape sequences. * Colorizes the log message based on the event level using ANSI escape sequences.
* *
* @param RuntimeOptions $options The runtime options object.
* @param Event $event The log event to colorize. * @param Event $event The log event to colorize.
* @return string The colorized log message. * @return string The colorized log message.
*/ */
private static function colorize(Event $event): string private static function colorize(RuntimeOptions $options, Event $event): string
{ {
if(!Log::getRuntimeOptions()->displayAnsi()) if(!$options->displayAnsi())
{ {
return Utilities::levelToString($event->getLevel()); return Utilities::levelToString($event->getLevel());
} }
@ -98,15 +110,16 @@
return Utilities::levelToString($event->getLevel()); return Utilities::levelToString($event->getLevel());
} }
return self::color(Utilities::levelToString($event->getLevel()), $color); return self::color($options, Utilities::levelToString($event->getLevel()), $color);
} }
/** /**
* Retrieves the current timestamp as a formatted string. * Retrieves the current timestamp as a formatted string.
* *
* @param RuntimeOptions $options The runtime options object.
* @return string The current timestamp. * @return string The current timestamp.
*/ */
private static function getTimestamp(): string private static function getTimestamp(RuntimeOptions $options): string
{ {
$tick_time = (string)microtime(true); $tick_time = (string)microtime(true);
@ -127,11 +140,11 @@
if ($timeDiff > 1.0) if ($timeDiff > 1.0)
{ {
$fmt_tick = self::color($tick_time, ConsoleColors::LIGHT_RED); $fmt_tick = self::color($options, $tick_time, ConsoleColors::LIGHT_RED);
} }
elseif ($timeDiff > 0.5) elseif ($timeDiff > 0.5)
{ {
$fmt_tick = self::color($tick_time, ConsoleColors::YELLOW); $fmt_tick = self::color($options, $tick_time, ConsoleColors::YELLOW);
} }
} }
@ -153,38 +166,38 @@
return; return;
} }
if(Validate::checkLevelType(LogLevel::DEBUG, Log::getRuntimeOptions()->getLoglevel())) if(Validate::checkLevelType(LogLevel::DEBUG, $options->getLoglevel()))
{ {
$backtrace_output = Utilities::getTraceString($event, Log::getRuntimeOptions()->displayAnsi()); $backtrace_output = Utilities::getTraceString($event, $options->displayAnsi());
print(sprintf("[%s] [%s] [%s] %s %s" . PHP_EOL, print(sprintf("[%s] [%s] [%s] %s %s" . PHP_EOL,
self::getTimestamp(), self::getTimestamp($options),
self::formatAppColor($options->getApplicationName()), self::formatAppColor($options->getApplicationName()),
self::colorize($event), self::colorize($options, $event),
$backtrace_output, $event->getMessage() $backtrace_output, $event->getMessage()
)); ));
if($event->getException() !== null) if($event->getException() !== null)
{ {
self::outException($event->getException()); self::outException($options, $event->getException());
} }
return; return;
} }
if(Validate::checkLevelType(LogLevel::VERBOSE, Log::getRuntimeOptions()->getLoglevel())) if(Validate::checkLevelType(LogLevel::VERBOSE, $options->getLoglevel()))
{ {
$backtrace_output = Utilities::getTraceString($event, Log::getRuntimeOptions()->displayAnsi()); $backtrace_output = Utilities::getTraceString($event, $options->displayAnsi());
print(sprintf("[%s] [%s] %s %s" . PHP_EOL, print(sprintf("[%s] [%s] %s %s" . PHP_EOL,
self::formatAppColor($options->getApplicationName()), self::formatAppColor($options->getApplicationName()),
self::colorize($event), self::colorize($options, $event),
$backtrace_output, $event->getMessage() $backtrace_output, $event->getMessage()
)); ));
if($event->getException() !== null) if($event->getException() !== null)
{ {
self::outException($event->getException()); self::outException($options, $event->getException());
} }
return; return;
@ -192,7 +205,7 @@
print(sprintf("[%s] [%s] %s" . PHP_EOL, print(sprintf("[%s] [%s] %s" . PHP_EOL,
self::formatAppColor($options->getApplicationName()), self::formatAppColor($options->getApplicationName()),
self::colorize($event), self::colorize($options, $event),
$event->getMessage() $event->getMessage()
)); ));
} }
@ -201,18 +214,19 @@
* Prints information about the given exception, including the error message, error code, * Prints information about the given exception, including the error message, error code,
* and stack trace. * and stack trace.
* *
* @param RuntimeOptions $options The runtime options object.
* @param Throwable|null $exception The exception to print information about. * @param Throwable|null $exception The exception to print information about.
* @return void * @return void
*/ */
private static function outException(?Throwable $exception=null): void private static function outException(RuntimeOptions $options, ?Throwable $exception=null): void
{ {
if($exception === null) if($exception === null)
{ {
return; return;
} }
$trace_header = self::color($exception->getFile() . ':' . $exception->getLine(), ConsoleColors::PURPLE); $trace_header = self::color($options, $exception->getFile() . ':' . $exception->getLine(), ConsoleColors::PURPLE);
$trace_error = self::color('error: ', ConsoleColors::RED); $trace_error = self::color($options, 'error: ', ConsoleColors::RED);
print($trace_header . ' ' . $trace_error . $exception->getMessage() . PHP_EOL); print($trace_header . ' ' . $trace_error . $exception->getMessage() . PHP_EOL);
print(sprintf('Error code: %s', $exception->getCode()) . PHP_EOL); print(sprintf('Error code: %s', $exception->getCode()) . PHP_EOL);
@ -223,7 +237,7 @@
print('Stack Trace:' . PHP_EOL); print('Stack Trace:' . PHP_EOL);
foreach($trace as $item) foreach($trace as $item)
{ {
print( ' - ' . self::color($item['file'], ConsoleColors::RED) . ':' . $item['line'] . PHP_EOL); print( ' - ' . self::color($options, $item['file'], ConsoleColors::RED) . ':' . $item['line'] . PHP_EOL);
} }
} }
@ -231,7 +245,7 @@
{ {
print('Previous Exception:' . PHP_EOL); print('Previous Exception:' . PHP_EOL);
self::outException($exception->getPrevious()); self::outException($options, $exception->getPrevious());
} }
} }
} }

View file

@ -109,17 +109,24 @@
/** /**
* Logs a message with a specified application name, level, optional message, and optional throwable. * Logs a message with a specified application name, level, optional message, and optional throwable.
* *
* @param string $application_name The name of the application * @param string|null $application_name The name of the application
* @param LogLevel $level The level type of the log (default is LevelType::INFO) * @param LogLevel $level The level type of the log (default is LevelType::INFO)
* @param string|null $message The message of the log event * @param string|null $message The message of the log event
* @param Throwable|null $throwable The exception that was thrown, if any * @param Throwable|null $throwable The exception that was thrown, if any
* @return void * @return void
*/ */
private static function log(string $application_name, LogLevel $level=LogLevel::INFO, ?string $message=null, ?Throwable $throwable=null): void private static function log(?string $application_name, LogLevel $level=LogLevel::INFO, ?string $message=null, ?Throwable $throwable=null): void
{ {
$application = self::getOptions($application_name); if($application_name === null)
{
$options = self::getRuntimeOptions();
}
else
{
$options = self::getOptions($application_name);
}
if(!Validate::checkLevelType($level, self::getRuntimeOptions()->getLoglevel())) if(!Validate::checkLevelType($level, $options->getLoglevel()))
{ {
return; return;
} }
@ -136,9 +143,9 @@
$event->setBacktrace(Utilities::getBacktrace()); $event->setBacktrace(Utilities::getBacktrace());
} }
if(self::getRuntimeOptions()->isConsoleOutput()) if($options->isConsoleOutput())
{ {
Console::out($application, $event); Console::out($options, $event);
} }
} }
@ -218,7 +225,8 @@
*/ */
public static function registerExceptionHandler(): void public static function registerExceptionHandler(): void
{ {
set_exception_handler(static function(Throwable $throwable) { set_exception_handler(static function(Throwable $throwable)
{
self::error('Runtime', $throwable->getMessage(), $throwable); self::error('Runtime', $throwable->getMessage(), $throwable);
}); });
} }

View file

@ -5,7 +5,7 @@
namespace LogLib\Objects; namespace LogLib\Objects;
class Options class Options extends RuntimeOptions
{ {
/** /**
* @var string * @var string
@ -17,8 +17,8 @@
*/ */
public function __construct(string $application_name) public function __construct(string $application_name)
{ {
$this->application_name = $application_name; parent::__construct();
} $this->application_name = $application_name;}
/** /**
* Returns the name of the Application * Returns the name of the Application
@ -29,5 +29,4 @@
{ {
return $this->application_name; return $this->application_name;
} }
} }

View file

@ -17,7 +17,6 @@
private LogLevel $fileLoggingLevel; private LogLevel $fileLoggingLevel;
/** /**
*
* @return void * @return void
*/ */
public function __construct() public function __construct()