modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-28 15:10:14 +04:00
parent 95db501ccf
commit a8777c1af8
7 changed files with 164 additions and 79 deletions

View File

@ -5,7 +5,7 @@ namespace nur\sery\output;
* Interface IMessenger: un objet pouvant afficher des messages de l'application * Interface IMessenger: un objet pouvant afficher des messages de l'application
*/ */
interface IMessenger { interface IMessenger {
const DEBUG = -1, NORMAL = 0, MAJOR = 1, NONE = 2; const DEBUG = -1, MINOR = 0, NORMAL = 1, MAJOR = 2, NONE = 3;
const MIN_LEVEL = self::DEBUG, MAX_LEVEL = self::MAJOR; const MIN_LEVEL = self::DEBUG, MAX_LEVEL = self::MAJOR;
/** réinitialiser les paramètres de l'objet */ /** réinitialiser les paramètres de l'objet */

View File

@ -2,7 +2,5 @@
* [ ] possibilité de paramétrer le nom du fichier destination pour faire une * [ ] possibilité de paramétrer le nom du fichier destination pour faire une
rotation des logs rotation des logs
* [ ] support verbose? cela suppose rajouter un niveau supplémentaire `LEVEL_MINOR`
entre `LEVEL_DEBUG` et `LEVEL_NORMAL`
-*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary

View File

@ -66,6 +66,7 @@ abstract class _messenger {
############################################################################# #############################################################################
const DEBUG = IMessenger::DEBUG; const DEBUG = IMessenger::DEBUG;
const MINOR = IMessenger::MINOR;
const NORMAL = IMessenger::NORMAL; const NORMAL = IMessenger::NORMAL;
const MAJOR = IMessenger::MAJOR; const MAJOR = IMessenger::MAJOR;
const NONE = IMessenger::NONE; const NONE = IMessenger::NONE;
@ -84,10 +85,13 @@ abstract class _messenger {
static function note($content, ?int $level=null): void { static::get()->note($content, $level); } static function note($content, ?int $level=null): void { static::get()->note($content, $level); }
static function warn($content, ?int $level=null): void { static::get()->warn($content, $level); } static function warn($content, ?int $level=null): void { static::get()->warn($content, $level); }
static function error($content, ?int $level=null): void { static::get()->error($content, $level); } static function error($content, ?int $level=null): void { static::get()->error($content, $level); }
static function end(bool $all=false): void { static::get()->end($all); }
static function debug($content): void { self::info($content, self::DEBUG);} static function debug($content): void { self::info($content, self::DEBUG);}
static function normal($content): void { self::info($content, self::NORMAL);}
static function minor($content): void { self::info($content, self::MINOR);}
static function important($content): void { self::info($content, self::MAJOR);} static function important($content): void { self::info($content, self::MAJOR);}
static function attention($content): void { self::note($content, self::MAJOR);} static function attention($content): void { self::note($content, self::MAJOR);}
static function critwarn($content): void { self::warn($content, self::MAJOR);} static function critwarn($content): void { self::warn($content, self::MAJOR);}
static function criterror($content): void { self::error($content, self::MAJOR);} static function criterror($content): void { self::error($content, self::MAJOR);}
static function end(bool $all=false): void { static::get()->end($all); }
} }

View File

@ -25,10 +25,68 @@ class ProxyMessenger implements IMessenger {
}; unset($msg); }; unset($msg);
return $clone; return $clone;
} }
function section($content, ?callable $func=null, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->section($content, $func, $level); } } function section($content, ?callable $func=null, ?int $level=null): void {
function title($content, ?callable $func=null, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->title($content, $func, $level); } } $useFunc = false;
foreach ($this->msgs as $msg) {
$msg->section($content, null, $level);
if ($msg instanceof _IMessenger) $useFunc = true;
}
if ($useFunc && $func !== null) {
try {
$func($this);
} finally {
/** @var _IMessenger $msg */
foreach ($this->msgs as $msg) {
$msg->_endSection();
}
}
}
}
function title($content, ?callable $func=null, ?int $level=null): void {
$useFunc = false;
$untils = [];
foreach ($this->msgs as $msg) {
$msg->title($content, null, $level);
if ($msg instanceof _IMessenger) {
$useFunc = true;
$untils[] = $msg->_getTitleMark();
}
}
if ($useFunc && $func !== null) {
try {
$func($this);
} finally {
/** @var _IMessenger $msg */
$index = 0;
foreach ($this->msgs as $msg) {
$msg->_endTitle($untils[$index++]);
}
}
}
}
function desc($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->desc($content, $level); } } function desc($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->desc($content, $level); } }
function action($content, ?callable $func=null, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->action($content, $func, $level); } } function action($content, ?callable $func=null, ?int $level=null): void {
$useFunc = false;
$untils = [];
foreach ($this->msgs as $msg) {
$msg->action($content, null, $level);
if ($msg instanceof _IMessenger) {
$useFunc = true;
$untils[] = $msg->_getTitleMark();
}
}
if ($useFunc && $func !== null) {
try {
$func($this);
} finally {
/** @var _IMessenger $msg */
$index = 0;
foreach ($this->msgs as $msg) {
$msg->_endAction($untils[$index++]);
}
}
}
}
function step($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->step($content, $level); } } function step($content, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->step($content, $level); } }
function asuccess($content=null): void { foreach ($this->msgs as $msg) { $msg->asuccess($content); } } function asuccess($content=null): void { foreach ($this->msgs as $msg) { $msg->asuccess($content); } }
function afailure($content=null): void { foreach ($this->msgs as $msg) { $msg->afailure($content); } } function afailure($content=null): void { foreach ($this->msgs as $msg) { $msg->afailure($content); } }

View File

@ -8,28 +8,19 @@ use nulib\UserException;
use nur\sery\output\IMessenger; use nur\sery\output\IMessenger;
use Throwable; use Throwable;
class StdMessenger implements IMessenger { class StdMessenger implements _IMessenger {
const INDENT = " "; const INDENT = " ";
const DATE_FORMAT = 'Y-m-d\TH:i:s.u'; const DATE_FORMAT = 'Y-m-d\TH:i:s.u';
const VALID_LEVELS = [ const VALID_LEVELS = [self::DEBUG, self::MINOR, self::NORMAL, self::MAJOR, self::NONE];
self::DEBUG,
self::NORMAL,
self::MAJOR,
self::NONE,
];
const LEVEL_MAP = [ const LEVEL_MAP = [
"debug" => self::DEBUG, "verbose" => self::DEBUG, "debug" => self::DEBUG,
"minor" => self::MINOR, "verbose" => self::MINOR,
"normal" => self::NORMAL, "normal" => self::NORMAL,
"major" => self::MAJOR, "quiet" => self::MAJOR, "major" => self::MAJOR, "quiet" => self::MAJOR,
"none" => self::NONE, "silent" => self::NONE, "none" => self::NONE, "silent" => self::NONE,
]; ];
protected static function decr_level(int $level): int {
if (--$level < self::MIN_LEVEL) $level = self::MIN_LEVEL;
return $level;
}
protected static function verifix_level($level, int $max_level=self::MAX_LEVEL): int { protected static function verifix_level($level, int $max_level=self::MAX_LEVEL): int {
if (!in_array($level, self::VALID_LEVELS, true)) { if (!in_array($level, self::VALID_LEVELS, true)) {
$level = cl::get(self::LEVEL_MAP, $level, $level); $level = cl::get(self::LEVEL_MAP, $level, $level);
@ -51,7 +42,7 @@ class StdMessenger implements IMessenger {
"error" => ["CRIT.ERROR!", "<color @r>E!", "</color>"], "error" => ["CRIT.ERROR!", "<color @r>E!", "</color>"],
"warn" => ["CRIT.WARN!", "<color @y>W!", "</color>"], "warn" => ["CRIT.WARN!", "<color @y>W!", "</color>"],
"note" => ["ATTENTION!", "<color @g>N!", "</color>"], "note" => ["ATTENTION!", "<color @g>N!", "</color>"],
"info" => ["IMPORTANT!", "<color @b>I!", "</color>"], "info" => ["IMPORTANT!", "<color @b>N!", "</color>"],
"step" => ["*", "<color @w>.</color>", ""], "step" => ["*", "<color @w>.</color>", ""],
"print" => [null, null, null], "print" => [null, null, null],
], ],
@ -66,6 +57,17 @@ class StdMessenger implements IMessenger {
"step" => ["*", "<color @w>.</color>", ""], "step" => ["*", "<color @w>.</color>", ""],
"print" => [null, null, null], "print" => [null, null, null],
], ],
self::MINOR => [
"section" => [true, "section", null, "<color @w>>>", "<<</color>", null],
"title" => [false, "title", null, "<color b>t", "</color>", null],
"desc" => ["desc", "<color b>></color>", ""],
"error" => ["error", "<color r>E</color><color -r>", "</color>"],
"warn" => ["warn", "<color y>W</color><color -y>", "</color>"],
"note" => ["note", "<color g>N</color>", ""],
"info" => ["info", "<color b>I</color><color w>", "</color>"],
"step" => ["*", "<color w>.</color>", ""],
"print" => [null, null, null],
],
self::DEBUG => [ self::DEBUG => [
"section" => [true, "section", null, "<color @w>>>", "<<</color>", null], "section" => [true, "section", null, "<color @w>>>", "<<</color>", null],
"title" => [false, "title", null, "<color b>t", "</color>", null], "title" => [false, "title", null, "<color b>t", "</color>", null],
@ -74,7 +76,7 @@ class StdMessenger implements IMessenger {
"warn" => ["debugW", "<color y>w</color><color -y>", "</color>"], "warn" => ["debugW", "<color y>w</color><color -y>", "</color>"],
"note" => ["debugN", "<color b>i</color>", ""], "note" => ["debugN", "<color b>i</color>", ""],
"info" => ["debug", "<color @w>D</color><color w>", "</color>"], "info" => ["debug", "<color @w>D</color><color w>", "</color>"],
"step" => ["*", "<color @w>.</color>", ""], "step" => ["*", "<color w>.</color>", ""],
"print" => [null, null, null], "print" => [null, null, null],
], ],
]; ];
@ -204,8 +206,15 @@ class StdMessenger implements IMessenger {
return $linePrefix; return $linePrefix;
} }
protected function decrLevel(int $level, int $amount=-1): int {
$level += $amount;
if ($level < self::MIN_LEVEL) $level = self::MIN_LEVEL;
return $level;
}
protected function checkLevel(?int &$level): bool { protected function checkLevel(?int &$level): bool {
if ($level === null) $level = $this->defaultLevel; if ($level === null) $level = $this->defaultLevel;
elseif ($level < 0) $level = $this->decrLevel($this->defaultLevel, $level);
return $level >= $this->minLevel; return $level >= $this->minLevel;
} }
@ -397,7 +406,7 @@ class StdMessenger implements IMessenger {
$this->_printGeneric($linePrefix, $level, $type, $content, $indentLevel, $out); $this->_printGeneric($linePrefix, $level, $type, $content, $indentLevel, $out);
} }
if ($exceptions !== null) { if ($exceptions !== null) {
$level1 = self::decr_level($level); $level1 = $this->decrLevel($level);
$showTraceback = $this->checkLevel($level1); $showTraceback = $this->checkLevel($level1);
foreach ($exceptions as $exception) { foreach ($exceptions as $exception) {
# tout d'abord userMessage # tout d'abord userMessage
@ -425,7 +434,7 @@ class StdMessenger implements IMessenger {
protected $section; protected $section;
function section($content, ?callable $func=null, ?int $level=null): void { function section($content, ?callable $func=null, ?int $level=null): void {
$this->endSection(); $this->_endSection();
$this->inSection = true; $this->inSection = true;
if (!$this->checkLevel($level)) return; if (!$this->checkLevel($level)) return;
$this->section = [ $this->section = [
@ -438,7 +447,7 @@ class StdMessenger implements IMessenger {
try { try {
$func($this); $func($this);
} finally { } finally {
$this->endSection(); $this->_endSection();
} }
} }
} }
@ -454,7 +463,7 @@ class StdMessenger implements IMessenger {
} }
} }
protected function endSection(): void { function _endSection(): void {
$this->inSection = false; $this->inSection = false;
$this->section = null; $this->section = null;
} }
@ -465,9 +474,13 @@ class StdMessenger implements IMessenger {
/** @var array */ /** @var array */
protected $title; protected $title;
function _getTitleMark(): int {
return count($this->titles);
}
function title($content, ?callable $func=null, ?int $level=null): void { function title($content, ?callable $func=null, ?int $level=null): void {
if (!$this->checkLevel($level)) return; if (!$this->checkLevel($level)) return;
$until = count($this->titles); $until = $this->_getTitleMark();
$this->titles[] = [ $this->titles[] = [
"line_prefix" => $this->getLinePrefix(), "line_prefix" => $this->getLinePrefix(),
"level" => $level, "level" => $level,
@ -476,12 +489,12 @@ class StdMessenger implements IMessenger {
"descs" => [], "descs" => [],
"print_descs" => false, "print_descs" => false,
]; ];
$this->title =& $this->titles[count($this->titles) - 1]; $this->title =& $this->titles[$until - 1];
if ($func !== null) { if ($func !== null) {
try { try {
$func($this); $func($this);
} finally { } finally {
$this->endTitle($until); $this->_endTitle($until);
} }
} }
} }
@ -523,8 +536,8 @@ class StdMessenger implements IMessenger {
}; unset($title); }; unset($title);
} }
protected function endTitle(?int $until=null): void { function _endTitle(?int $until=null): void {
if ($until === null) $until = count($this->titles) - 1; if ($until === null) $until = $this->_getTitleMark() - 1;
while (count($this->titles) > $until) { while (count($this->titles) > $until) {
array_pop($this->titles); array_pop($this->titles);
} }
@ -542,9 +555,13 @@ class StdMessenger implements IMessenger {
/** @var array */ /** @var array */
protected $action; protected $action;
function _getActionMark(): int {
return count($this->actions);
}
function action($content, ?callable $func=null, ?int $level=null): void { function action($content, ?callable $func=null, ?int $level=null): void {
$this->checkLevel($level); $this->checkLevel($level);
$until = count($this->actions); $until = $this->_getActionMark();
$this->actions[] = [ $this->actions[] = [
"line_prefix" => $this->getLinePrefix(), "line_prefix" => $this->getLinePrefix(),
"level" => $level, "level" => $level,
@ -553,7 +570,7 @@ class StdMessenger implements IMessenger {
"result_success" => null, "result_success" => null,
"result_content" => null, "result_content" => null,
]; ];
$this->action =& $this->actions[count($this->actions) - 1]; $this->action =& $this->actions[$until - 1];
if ($func !== null) { if ($func !== null) {
try { try {
$result = $func($this); $result = $func($this);
@ -563,25 +580,26 @@ class StdMessenger implements IMessenger {
else $this->adone($result); else $this->adone($result);
} }
} finally { } finally {
$this->endAction($until); $this->_endAction($until);
} }
} }
} }
function printActions(bool $willEnd=false): void { function printActions(bool $endAction=false): void {
$this->printTitles(); $this->printTitles();
$err = $this->err; $err = $this->err;
$indentLevel = $this->getIndentLevel(false); $indentLevel = $this->getIndentLevel(false);
$lastIndex = count($this->actions) - 1; $lastIndex = count($this->actions) - 1;
$index = 0; $index = 0;
foreach ($this->actions as &$action) { foreach ($this->actions as &$action) {
$mergeResult = $index++ == $lastIndex && $willEnd; $mergeResult = $index++ == $lastIndex && $endAction;
$linePrefix = $action["line_prefix"]; $linePrefix = $action["line_prefix"];
$level = $action["level"]; $level = $action["level"];
$content = $action["content"]; $content = $action["content"];
$printContent = $action["print_content"]; $printContent = $action["print_content"];
$rsuccess = $action["result_success"]; $rsuccess = $action["result_success"];
$rcontent = $action["result_content"]; $rcontent = $action["result_content"];
if ($level < $this->minLevel) continue;
if ($mergeResult) { if ($mergeResult) {
$this->_printAction( $this->_printAction(
$linePrefix, $level, $linePrefix, $level,
@ -598,6 +616,7 @@ class StdMessenger implements IMessenger {
} }
$indentLevel++; $indentLevel++;
}; unset($action); }; unset($action);
if ($endAction) $this->_endAction();
} }
function step($content, ?int $level=null): void { function step($content, ?int $level=null): void {
@ -609,7 +628,6 @@ class StdMessenger implements IMessenger {
$this->action["result_success"] = true; $this->action["result_success"] = true;
$this->action["result_content"] = $content; $this->action["result_content"] = $content;
$this->printActions(true); $this->printActions(true);
$this->endAction();
} }
function afailure($content=null): void { function afailure($content=null): void {
@ -617,7 +635,6 @@ class StdMessenger implements IMessenger {
$this->action["result_success"] = false; $this->action["result_success"] = false;
$this->action["result_content"] = $content; $this->action["result_content"] = $content;
$this->printActions(true); $this->printActions(true);
$this->endAction();
} }
function adone($content=null): void { function adone($content=null): void {
@ -625,11 +642,10 @@ class StdMessenger implements IMessenger {
$this->action["result_success"] = null; $this->action["result_success"] = null;
$this->action["result_content"] = $content; $this->action["result_content"] = $content;
$this->printActions(true); $this->printActions(true);
$this->endAction();
} }
protected function endAction(?int $until=null): void { function _endAction(?int $until=null): void {
if ($until === null) $until = count($this->actions) - 1; if ($until === null) $until = $this->_getActionMark() - 1;
while (count($this->actions) > $until) { while (count($this->actions) > $until) {
array_pop($this->actions); array_pop($this->actions);
} }
@ -664,14 +680,14 @@ class StdMessenger implements IMessenger {
function end(bool $all=false): void { function end(bool $all=false): void {
if ($all) { if ($all) {
while ($this->actions) $this->adone(); while ($this->actions) $this->adone();
while ($this->titles) $this->endTitle(); while ($this->titles) $this->_endTitle();
$this->endSection(); $this->_endSection();
} elseif ($this->actions) { } elseif ($this->actions) {
$this->endAction(); $this->_endAction();
} elseif ($this->titles) { } elseif ($this->titles) {
$this->endTitle(); $this->_endTitle();
} else { } else {
$this->endSection(); $this->_endSection();
} }
} }
} }

View File

@ -0,0 +1,19 @@
<?php
namespace nur\sery\output\std;
use nur\sery\output\IMessenger;
/**
* Interface _IMessenger: méthodes privées de IMessenger
*/
interface _IMessenger extends IMessenger {
function _endSection(): void;
function _getTitleMark(): int;
function _endTitle(?int $until=null): void;
function _getActionMark(): int;
function _endAction(?int $until=null): void;
}

View File

@ -9,39 +9,29 @@ $params = [];
$count = count($argv) - 1; $count = count($argv) - 1;
for ($i = 1; $i <= $count; $i++) { for ($i = 1; $i <= $count; $i++) {
switch ($argv[$i]) { switch ($argv[$i]) {
case "-n": case "-L": $i++; $params["output"] = $argv[$i]; break;
$params["color"] = false; case "-i": $i++; $params["id"] = $argv[$i]; break;
break; case "-t": $params["add_date"] = true; break;
case "+n": case "-n": $params["color"] = false; break;
$params["color"] = true; case "+n": $params["color"] = true; break;
break; case "-d": $params["debug"] = true; break;
case "-d": case "+d": $params["debug"] = false; break;
$params["debug"] = true; case "-D": $params["default_level"] = "debug"; break;
break; case "-m": $params["default_level"] = "minor"; break;
case "+d": case "-M": $params["default_level"] = "major"; break;
$params["debug"] = false; case "--show-debug":
break; case "--sd": $params["min_level"] = "debug"; break;
case "-L": case "--show-minor":
$i++; case "--verbose":
$params["output"] = $argv[$i]; case "--sm": $params["min_level"] = "minor"; break;
break; case "--show-normal":
case "-D": case "--sn": $params["min_level"] = "normal"; break;
$params["default_level"] = "debug"; case "--show-major":
$params["debug"] = true; case "--quiet":
break; case "--sM": $params["min_level"] = "major"; break;
case "-N": case "--show-none":
$params["default_level"] = "normal"; case "--silent":
break; case "--sx": $params["min_level"] = "none"; break;
case "-M":
$params["default_level"] = "major";
break;
case "-i":
$i++;
$params["id"] = $argv[$i];
break;
case "-t":
$params["add_date"] = true;
break;
} }
} }
$msg = new StdMessenger($params); $msg = new StdMessenger($params);