modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-27 01:53:46 +04:00
parent 64fd232f71
commit e62e4ebfa4
3 changed files with 20 additions and 9 deletions

View File

@ -5,7 +5,7 @@ namespace nur\sery\output;
* Interface IMessenger: un objet pouvant afficher des messages de l'application
*/
interface IMessenger {
const LEVEL_DEBUG = -1, LEVEL_NORMAL = 0, LEVEL_MAJOR = 1;
const LEVEL_DEBUG = -1, LEVEL_NORMAL = 0, LEVEL_MAJOR = 1, LEVEL_NONE = 2;
/**
* commencer une section.

View File

@ -11,6 +11,7 @@ class StdMessenger implements IMessenger {
self::LEVEL_DEBUG,
self::LEVEL_NORMAL,
self::LEVEL_MAJOR,
self::LEVEL_NONE,
];
const LEVEL_MAP = [
"debug" => self::LEVEL_DEBUG,
@ -19,16 +20,19 @@ class StdMessenger implements IMessenger {
"n" => self::LEVEL_NORMAL,
"major" => self::LEVEL_MAJOR,
"m" => self::LEVEL_MAJOR,
"none" => self::LEVEL_NONE,
];
protected static function verifix_level($level, bool $debug): int {
if ($level === null) $level = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL;
protected static function verifix_level($level, int $max_level=self::LEVEL_MAJOR): int {
if (!in_array($level, self::VALID_LEVELS, true)) {
$level = cl::get(self::LEVEL_MAP, $level, $level);
}
if (!in_array($level, self::VALID_LEVELS, true)) {
throw new Exception("$level: invalid level");
}
if ($level > $max_level) {
throw new Exception("$level: level not allowed here");
}
return $level;
}
@ -78,8 +82,12 @@ class StdMessenger implements IMessenger {
$output = cl::get($params, "output");
$color = cl::get($params, "color");
$debug = boolval(cl::get($params, "debug"));
$minLevel = self::verifix_level(cl::get($params, "min_level"), $debug);
$defaultLevel = self::verifix_level(cl::get($params, "default_level"), false);
$defaultLevel = cl::get($params, "default_level");
if ($defaultLevel === null) $defaultLevel = self::LEVEL_NORMAL;
$defaultLevel = self::verifix_level($defaultLevel);
$minLevel = cl::get($params, "min_level");
if ($minLevel === null) $minLevel = $debug? self::LEVEL_DEBUG: self::LEVEL_NORMAL;
$minLevel = self::verifix_level($minLevel, self::LEVEL_NONE);
$params = [
"color" => $color,
@ -91,8 +99,8 @@ class StdMessenger implements IMessenger {
$this->out = new StdOutput(STDOUT, $params);
$this->err = new StdOutput(STDERR, $params);
}
$this->minLevel = $minLevel;
$this->defaultLevel = $defaultLevel;
$this->minLevel = $minLevel;
$this->inSection = false;
$this->titles = [];
$this->title = null;
@ -106,12 +114,12 @@ class StdMessenger implements IMessenger {
/** @var StdOutput la sortie d'erreur */
protected $err;
/** @var int level minimum que doivent avoir les messages pour être affichés */
protected $minLevel;
/** @var int level par défaut dans lequel les messages sont affichés */
protected $defaultLevel;
/** @var int level minimum que doivent avoir les messages pour être affichés */
protected $minLevel;
protected function checkLevel(?int &$level): bool {
if ($level === null) $level = $this->defaultLevel;
return $level >= $this->minLevel;

View File

@ -7,6 +7,9 @@
si pas de message, prendre le message de l'exception par défaut
* [ ] possibilité de paramétrer le nom du fichier destination pour faire une
rotation des logs
* [ ] support quiet (uniquement major), very-quiet (aucun message n'est affiché)
* [ ] support verbose? cela suppose rajouter un niveau supplémentaire `LEVEL_MINOR`
entre `LEVEL_DEBUG` et `LEVEL_NORMAL`
## Principe