Refactor log level checking to enum method
This commit is contained in:
parent
8b64a48a5f
commit
e624663f62
6 changed files with 42 additions and 52 deletions
|
@ -98,7 +98,7 @@
|
||||||
self::$log_level = LogLevel::INFO;
|
self::$log_level = LogLevel::INFO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(self::$log_level, LogLevel::DEBUG))
|
if(self::$log_level->checkLogLevel(LogLevel::DEBUG))
|
||||||
{
|
{
|
||||||
Console::outDebug('Debug logging enabled');
|
Console::outDebug('Debug logging enabled');
|
||||||
|
|
||||||
|
|
|
@ -123,7 +123,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugging information
|
// Debugging information
|
||||||
if(Resolver::checkLogLevel(LogLevel::DEBUG, Main::getLogLevel()))
|
if(LogLevel::DEBUG->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
foreach($this->project_manager->getProjectConfiguration()->getAssembly()->toArray() as $prop => $value)
|
foreach($this->project_manager->getProjectConfiguration()->getAssembly()->toArray() as $prop => $value)
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,6 +22,8 @@
|
||||||
|
|
||||||
namespace ncc\Enums;
|
namespace ncc\Enums;
|
||||||
|
|
||||||
|
use ncc\Utilities\Validate;
|
||||||
|
|
||||||
enum LogLevel : string
|
enum LogLevel : string
|
||||||
{
|
{
|
||||||
case SILENT = 'silent';
|
case SILENT = 'silent';
|
||||||
|
@ -38,6 +40,31 @@
|
||||||
|
|
||||||
case FATAL = 'fatal';
|
case FATAL = 'fatal';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the current log level permits logging at the specified level.
|
||||||
|
*
|
||||||
|
* @param LogLevel|null $current_level The log level to be checked. If null, the method returns false.
|
||||||
|
* @return bool Returns true if logging is permitted at the specified level, otherwise false.
|
||||||
|
*/
|
||||||
|
public function checkLogLevel(?LogLevel $current_level): bool
|
||||||
|
{
|
||||||
|
if ($current_level === null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return match ($current_level)
|
||||||
|
{
|
||||||
|
LogLevel::DEBUG => in_array($this, [LogLevel::DEBUG, LogLevel::VERBOSE, LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
|
LogLevel::VERBOSE => in_array($this, [LogLevel::VERBOSE, LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
|
LogLevel::INFO => in_array($this, [LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
|
LogLevel::WARNING => in_array($this, [LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
|
LogLevel::ERROR => in_array($this, [LogLevel::FATAL, LogLevel::ERROR], true),
|
||||||
|
LogLevel::FATAL => $this === LogLevel::FATAL,
|
||||||
|
default => false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the given string input to a LogLevel.
|
* Converts the given string input to a LogLevel.
|
||||||
* If the input is invalid or not found, it defaults to LogLevel::INFO.
|
* If the input is invalid or not found, it defaults to LogLevel::INFO.
|
||||||
|
|
|
@ -665,7 +665,7 @@
|
||||||
{
|
{
|
||||||
$progress_bar->setMiscText($component_name);
|
$progress_bar->setMiscText($component_name);
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
if(LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if(LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if(LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if(LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if(!LogLevel::INFO->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$no_prefix && Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
if(!$no_prefix && LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if(!LogLevel::DEBUG->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -179,7 +179,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
if(!LogLevel::VERBOSE->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -225,12 +225,12 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!Resolver::checkLogLevel(LogLevel::WARNING, Main::getLogLevel()))
|
if(!LogLevel::WARNING->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
if(LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if(!LogLevel::ERROR->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(Resolver::checkLogLevel(LogLevel::VERBOSE, Main::getLogLevel()))
|
if(LogLevel::VERBOSE->checkLogLevel(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, Main::getLogLevel()))
|
if($message !== '' && LogLevel::ERROR->checkLogLevel(Main::getLogLevel()))
|
||||||
{
|
{
|
||||||
self::out(PHP_EOL . self::formatColor('Error: ', ConsoleColors::RED->value) . $message);
|
self::out(PHP_EOL . self::formatColor('Error: ', ConsoleColors::RED->value) . $message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -160,43 +160,6 @@
|
||||||
return hash('haval128,3', self::resolveFullConstantName($scope, $name));
|
return hash('haval128,3', self::resolveFullConstantName($scope, $name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the input level matches the current level
|
|
||||||
*
|
|
||||||
* @param LogLevel|null $input
|
|
||||||
* @param LogLevel|null $current_level
|
|
||||||
* @return 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)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Validate::checkLogLevel($input))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Validate::checkLogLevel($current_level))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return match ($current_level)
|
|
||||||
{
|
|
||||||
LogLevel::DEBUG => in_array($input, [LogLevel::DEBUG, LogLevel::VERBOSE, LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
|
||||||
LogLevel::VERBOSE => in_array($input, [LogLevel::VERBOSE, LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
|
||||||
LogLevel::INFO => in_array($input, [LogLevel::INFO, LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
|
||||||
LogLevel::WARNING => in_array($input, [LogLevel::WARNING, LogLevel::FATAL, LogLevel::ERROR], true),
|
|
||||||
LogLevel::ERROR => in_array($input, [LogLevel::FATAL, LogLevel::ERROR], true),
|
|
||||||
LogLevel::FATAL => $input === LogLevel::FATAL,
|
|
||||||
default => false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the ProjectDetectionResults of the project in the specified directory
|
* Returns the ProjectDetectionResults of the project in the specified directory
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue