Refactor logging level checks to use LogLevel enum directly
This commit is contained in:
parent
5ea0235515
commit
6398b2958b
7 changed files with 43 additions and 33 deletions
|
@ -114,7 +114,7 @@
|
||||||
self::$log_level = LogLevel::INFO->value;
|
self::$log_level = LogLevel::INFO->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(self::$log_level, LogLevel::DEBUG->value))
|
if(Resolver::checkLogLevel(self::$log_level, LogLevel::DEBUG))
|
||||||
{
|
{
|
||||||
Console::outDebug('Debug logging enabled');
|
Console::outDebug('Debug logging enabled');
|
||||||
|
|
||||||
|
@ -230,15 +230,21 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return LogLevel
|
||||||
*/
|
*/
|
||||||
public static function getLogLevel(): string
|
public static function getLogLevel(): LogLevel
|
||||||
{
|
{
|
||||||
if(self::$log_level === null)
|
if(self::$log_level === null)
|
||||||
{
|
{
|
||||||
self::$log_level = LogLevel::INFO->value;
|
self::$log_level = LogLevel::INFO;
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$log_level;
|
$level = LogLevel::tryFrom(self::$log_level);
|
||||||
|
if($level === null)
|
||||||
|
{
|
||||||
|
return self::$log_level = LogLevel::INFO;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $level;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -123,7 +123,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugging information
|
// Debugging information
|
||||||
if(Resolver::checkLogLevel(LogLevel::DEBUG->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::DEBUG, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
foreach($this->project_manager->getProjectConfiguration()->getAssembly()->toArray() as $prop => $value)
|
foreach($this->project_manager->getProjectConfiguration()->getAssembly()->toArray() as $prop => $value)
|
||||||
{
|
{
|
||||||
|
|
|
@ -112,11 +112,11 @@
|
||||||
|
|
||||||
switch(Main::getLogLevel())
|
switch(Main::getLogLevel())
|
||||||
{
|
{
|
||||||
case LogLevel::VERBOSE->value:
|
case LogLevel::VERBOSE:
|
||||||
$gcc_options[] = '-v';
|
$gcc_options[] = '-v';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LogLevel::DEBUG->value:
|
case LogLevel::DEBUG:
|
||||||
$gcc_options[] = '-v';
|
$gcc_options[] = '-v';
|
||||||
$gcc_options[] = '-v';
|
$gcc_options[] = '-v';
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -665,7 +665,7 @@
|
||||||
{
|
{
|
||||||
$progress_bar->setMiscText($component_name);
|
$progress_bar->setMiscText($component_name);
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
Console::outVerbose(sprintf('Extracting component %s to %s', $component_name, $bin_path . DIRECTORY_SEPARATOR . $component_name));
|
Console::outVerbose(sprintf('Extracting component %s to %s', $component_name, $bin_path . DIRECTORY_SEPARATOR . $component_name));
|
||||||
}
|
}
|
||||||
|
@ -683,7 +683,7 @@
|
||||||
{
|
{
|
||||||
$progress_bar->setMiscText($resource_name);
|
$progress_bar->setMiscText($resource_name);
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
Console::outVerbose(sprintf('Extracting resource %s to %s', $resource_name, $bin_path . DIRECTORY_SEPARATOR . $resource_name));
|
Console::outVerbose(sprintf('Extracting resource %s to %s', $resource_name, $bin_path . DIRECTORY_SEPARATOR . $resource_name));
|
||||||
}
|
}
|
||||||
|
@ -701,7 +701,7 @@
|
||||||
{
|
{
|
||||||
$progress_bar->setMiscText($unit);
|
$progress_bar->setMiscText($unit);
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
Console::outVerbose(sprintf('Extracting execution unit %s to %s', $unit, $package_path . DIRECTORY_SEPARATOR . 'units' . DIRECTORY_SEPARATOR . $package_reader->getExecutionUnit($unit)->getExecutionPolicy()->getName() . '.unit'));
|
Console::outVerbose(sprintf('Extracting execution unit %s to %s', $unit, $package_path . DIRECTORY_SEPARATOR . 'units' . DIRECTORY_SEPARATOR . $package_reader->getExecutionUnit($unit)->getExecutionPolicy()->getName() . '.unit'));
|
||||||
}
|
}
|
||||||
|
@ -907,7 +907,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
$percentage = round(($downloaded / $download_size) * 100, 2);
|
$percentage = round(($downloaded / $download_size) * 100, 2);
|
||||||
Console::out(sprintf('Download progress %s (%s/%s) for %s', $percentage, $downloaded, $download_size, $url));
|
Console::out(sprintf('Download progress %s (%s/%s) for %s', $percentage, $downloaded, $download_size, $url));
|
||||||
|
|
|
@ -109,12 +109,12 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::INFO->value, Main::getLogLevel()))
|
if(!Resolver::checkLogLevel(LogLevel::INFO, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$no_prefix && Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(!$no_prefix && Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
$message = self::setPrefix(LogLevel::INFO->value, $message);
|
$message = self::setPrefix(LogLevel::INFO->value, $message);
|
||||||
}
|
}
|
||||||
|
@ -142,7 +142,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::DEBUG->value, Main::getLogLevel()))
|
if(!Resolver::checkLogLevel(LogLevel::DEBUG, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(!Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -225,12 +225,12 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::WARNING->value, Main::getLogLevel()))
|
if(!Resolver::checkLogLevel(LogLevel::WARNING, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
self::out(self::setPrefix(LogLevel::WARNING->value, $message), $newline, true);
|
self::out(self::setPrefix(LogLevel::WARNING->value, $message), $newline, true);
|
||||||
return;
|
return;
|
||||||
|
@ -254,12 +254,12 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::ERROR->value, Main::getLogLevel()))
|
if(!Resolver::checkLogLevel(LogLevel::ERROR, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE->value, Main::getLogLevel()))
|
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
self::out(self::setPrefix(LogLevel::ERROR->value, $message), $newline, true);
|
self::out(self::setPrefix(LogLevel::ERROR->value, $message), $newline, true);
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if($message !== '' && Resolver::checkLogLevel(LogLevel::ERROR->value, Main::getLogLevel()))
|
if($message !== '' && Resolver::checkLogLevel(LogLevel::ERROR, Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
self::out(PHP_EOL . self::formatColor('Error: ', ConsoleColors::RED->value) . $message);
|
self::out(PHP_EOL . self::formatColor('Error: ', ConsoleColors::RED->value) . $message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -163,24 +163,23 @@
|
||||||
/**
|
/**
|
||||||
* Checks if the input level matches the current level
|
* Checks if the input level matches the current level
|
||||||
*
|
*
|
||||||
* @param string|null $input
|
* @param LogLevel|null $input
|
||||||
* @param string|null $current_level
|
* @param LogLevel|null $current_level
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function checkLogLevel(?string $input, ?string $current_level): bool
|
public static function checkLogLevel(?LogLevel $input, ?LogLevel $current_level): bool
|
||||||
{
|
{
|
||||||
|
// TODO: This method can be merged into the enum class instead
|
||||||
if ($input === null || $current_level === null)
|
if ($input === null || $current_level === null)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$input = strtolower($input);
|
|
||||||
if (!Validate::checkLogLevel($input))
|
if (!Validate::checkLogLevel($input))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$current_level = strtolower($current_level);
|
|
||||||
if (!Validate::checkLogLevel($current_level))
|
if (!Validate::checkLogLevel($current_level))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -188,12 +187,12 @@
|
||||||
|
|
||||||
return match ($current_level)
|
return match ($current_level)
|
||||||
{
|
{
|
||||||
LogLevel::DEBUG->value => in_array($input, [LogLevel::DEBUG->value, LogLevel::VERBOSE->value, LogLevel::INFO->value, LogLevel::WARNING->value, LogLevel::FATAL->value, LogLevel::ERROR->value], true),
|
LogLevel::DEBUG => in_array($input, [LogLevel::DEBUG, LogLevel::VERBOSE, LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
LogLevel::VERBOSE->value => in_array($input, [LogLevel::VERBOSE->value, LogLevel::INFO->value, LogLevel::WARNING->value, LogLevel::FATAL->value, LogLevel::ERROR->value], true),
|
LogLevel::VERBOSE => in_array($input, [LogLevel::VERBOSE, LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
LogLevel::INFO->value => in_array($input, [LogLevel::INFO->value, LogLevel::WARNING->value, LogLevel::FATAL->value, LogLevel::ERROR->value], true),
|
LogLevel::INFO => in_array($input, [LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
LogLevel::WARNING->value => in_array($input, [LogLevel::WARNING->value, LogLevel::FATAL->value, LogLevel::ERROR->value], true),
|
LogLevel::WARNING => in_array($input, [LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
LogLevel::ERROR->value => in_array($input, [LogLevel::FATAL->value, LogLevel::ERROR->value], true),
|
LogLevel::ERROR => in_array($input, [LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
LogLevel::FATAL->value => $input === LogLevel::FATAL->value,
|
LogLevel::FATAL => $input === LogLevel::FATAL,
|
||||||
default => false,
|
default => false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -226,8 +226,13 @@ namespace ncc\Utilities;
|
||||||
* @param string $input
|
* @param string $input
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function checkLogLevel(string $input): bool
|
public static function checkLogLevel(string|LogLevel $input): bool
|
||||||
{
|
{
|
||||||
|
if($input instanceof LogLevel)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return in_array(strtolower($input), array_map(
|
return in_array(strtolower($input), array_map(
|
||||||
fn($case) => $case->value, LogLevel::cases()), true
|
fn($case) => $case->value, LogLevel::cases()), true
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Reference in a new issue