modifs.mineures sans commentaires
This commit is contained in:
parent
95db501ccf
commit
a8777c1af8
|
@ -5,7 +5,7 @@ namespace nur\sery\output;
|
|||
* Interface IMessenger: un objet pouvant afficher des messages de l'application
|
||||
*/
|
||||
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;
|
||||
|
||||
/** réinitialiser les paramètres de l'objet */
|
||||
|
|
|
@ -2,7 +2,5 @@
|
|||
|
||||
* [ ] possibilité de paramétrer le nom du fichier destination pour faire une
|
||||
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
|
|
@ -66,6 +66,7 @@ abstract class _messenger {
|
|||
#############################################################################
|
||||
|
||||
const DEBUG = IMessenger::DEBUG;
|
||||
const MINOR = IMessenger::MINOR;
|
||||
const NORMAL = IMessenger::NORMAL;
|
||||
const MAJOR = IMessenger::MAJOR;
|
||||
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 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 end(bool $all=false): void { static::get()->end($all); }
|
||||
|
||||
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 attention($content): void { self::note($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 end(bool $all=false): void { static::get()->end($all); }
|
||||
}
|
||||
|
|
|
@ -25,10 +25,68 @@ class ProxyMessenger implements IMessenger {
|
|||
}; unset($msg);
|
||||
return $clone;
|
||||
}
|
||||
function section($content, ?callable $func=null, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->section($content, $func, $level); } }
|
||||
function title($content, ?callable $func=null, ?int $level=null): void { foreach ($this->msgs as $msg) { $msg->title($content, $func, $level); } }
|
||||
function section($content, ?callable $func=null, ?int $level=null): void {
|
||||
$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 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 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); } }
|
||||
|
|
|
@ -8,28 +8,19 @@ use nulib\UserException;
|
|||
use nur\sery\output\IMessenger;
|
||||
use Throwable;
|
||||
|
||||
class StdMessenger implements IMessenger {
|
||||
class StdMessenger implements _IMessenger {
|
||||
const INDENT = " ";
|
||||
const DATE_FORMAT = 'Y-m-d\TH:i:s.u';
|
||||
|
||||
const VALID_LEVELS = [
|
||||
self::DEBUG,
|
||||
self::NORMAL,
|
||||
self::MAJOR,
|
||||
self::NONE,
|
||||
];
|
||||
const VALID_LEVELS = [self::DEBUG, self::MINOR, self::NORMAL, self::MAJOR, self::NONE];
|
||||
const LEVEL_MAP = [
|
||||
"debug" => self::DEBUG, "verbose" => self::DEBUG,
|
||||
"debug" => self::DEBUG,
|
||||
"minor" => self::MINOR, "verbose" => self::MINOR,
|
||||
"normal" => self::NORMAL,
|
||||
"major" => self::MAJOR, "quiet" => self::MAJOR,
|
||||
"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 {
|
||||
if (!in_array($level, self::VALID_LEVELS, true)) {
|
||||
$level = cl::get(self::LEVEL_MAP, $level, $level);
|
||||
|
@ -51,7 +42,7 @@ class StdMessenger implements IMessenger {
|
|||
"error" => ["CRIT.ERROR!", "<color @r>E!", "</color>"],
|
||||
"warn" => ["CRIT.WARN!", "<color @y>W!", "</color>"],
|
||||
"note" => ["ATTENTION!", "<color @g>N!", "</color>"],
|
||||
"info" => ["IMPORTANT!", "<color @b>I!", "</color>"],
|
||||
"info" => ["IMPORTANT!", "<color @b>N!", "</color>"],
|
||||
"step" => ["*", "<color @w>.</color>", ""],
|
||||
"print" => [null, null, null],
|
||||
],
|
||||
|
@ -66,6 +57,17 @@ class StdMessenger implements IMessenger {
|
|||
"step" => ["*", "<color @w>.</color>", ""],
|
||||
"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 => [
|
||||
"section" => [true, "section", null, "<color @w>>>", "<<</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>"],
|
||||
"note" => ["debugN", "<color b>i</color>", ""],
|
||||
"info" => ["debug", "<color @w>D</color><color w>", "</color>"],
|
||||
"step" => ["*", "<color @w>.</color>", ""],
|
||||
"step" => ["*", "<color w>.</color>", ""],
|
||||
"print" => [null, null, null],
|
||||
],
|
||||
];
|
||||
|
@ -204,8 +206,15 @@ class StdMessenger implements IMessenger {
|
|||
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 {
|
||||
if ($level === null) $level = $this->defaultLevel;
|
||||
elseif ($level < 0) $level = $this->decrLevel($this->defaultLevel, $level);
|
||||
return $level >= $this->minLevel;
|
||||
}
|
||||
|
||||
|
@ -397,7 +406,7 @@ class StdMessenger implements IMessenger {
|
|||
$this->_printGeneric($linePrefix, $level, $type, $content, $indentLevel, $out);
|
||||
}
|
||||
if ($exceptions !== null) {
|
||||
$level1 = self::decr_level($level);
|
||||
$level1 = $this->decrLevel($level);
|
||||
$showTraceback = $this->checkLevel($level1);
|
||||
foreach ($exceptions as $exception) {
|
||||
# tout d'abord userMessage
|
||||
|
@ -425,7 +434,7 @@ class StdMessenger implements IMessenger {
|
|||
protected $section;
|
||||
|
||||
function section($content, ?callable $func=null, ?int $level=null): void {
|
||||
$this->endSection();
|
||||
$this->_endSection();
|
||||
$this->inSection = true;
|
||||
if (!$this->checkLevel($level)) return;
|
||||
$this->section = [
|
||||
|
@ -438,7 +447,7 @@ class StdMessenger implements IMessenger {
|
|||
try {
|
||||
$func($this);
|
||||
} finally {
|
||||
$this->endSection();
|
||||
$this->_endSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -454,7 +463,7 @@ class StdMessenger implements IMessenger {
|
|||
}
|
||||
}
|
||||
|
||||
protected function endSection(): void {
|
||||
function _endSection(): void {
|
||||
$this->inSection = false;
|
||||
$this->section = null;
|
||||
}
|
||||
|
@ -465,9 +474,13 @@ class StdMessenger implements IMessenger {
|
|||
/** @var array */
|
||||
protected $title;
|
||||
|
||||
function _getTitleMark(): int {
|
||||
return count($this->titles);
|
||||
}
|
||||
|
||||
function title($content, ?callable $func=null, ?int $level=null): void {
|
||||
if (!$this->checkLevel($level)) return;
|
||||
$until = count($this->titles);
|
||||
$until = $this->_getTitleMark();
|
||||
$this->titles[] = [
|
||||
"line_prefix" => $this->getLinePrefix(),
|
||||
"level" => $level,
|
||||
|
@ -476,12 +489,12 @@ class StdMessenger implements IMessenger {
|
|||
"descs" => [],
|
||||
"print_descs" => false,
|
||||
];
|
||||
$this->title =& $this->titles[count($this->titles) - 1];
|
||||
$this->title =& $this->titles[$until - 1];
|
||||
if ($func !== null) {
|
||||
try {
|
||||
$func($this);
|
||||
} finally {
|
||||
$this->endTitle($until);
|
||||
$this->_endTitle($until);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -523,8 +536,8 @@ class StdMessenger implements IMessenger {
|
|||
}; unset($title);
|
||||
}
|
||||
|
||||
protected function endTitle(?int $until=null): void {
|
||||
if ($until === null) $until = count($this->titles) - 1;
|
||||
function _endTitle(?int $until=null): void {
|
||||
if ($until === null) $until = $this->_getTitleMark() - 1;
|
||||
while (count($this->titles) > $until) {
|
||||
array_pop($this->titles);
|
||||
}
|
||||
|
@ -542,9 +555,13 @@ class StdMessenger implements IMessenger {
|
|||
/** @var array */
|
||||
protected $action;
|
||||
|
||||
function _getActionMark(): int {
|
||||
return count($this->actions);
|
||||
}
|
||||
|
||||
function action($content, ?callable $func=null, ?int $level=null): void {
|
||||
$this->checkLevel($level);
|
||||
$until = count($this->actions);
|
||||
$until = $this->_getActionMark();
|
||||
$this->actions[] = [
|
||||
"line_prefix" => $this->getLinePrefix(),
|
||||
"level" => $level,
|
||||
|
@ -553,7 +570,7 @@ class StdMessenger implements IMessenger {
|
|||
"result_success" => null,
|
||||
"result_content" => null,
|
||||
];
|
||||
$this->action =& $this->actions[count($this->actions) - 1];
|
||||
$this->action =& $this->actions[$until - 1];
|
||||
if ($func !== null) {
|
||||
try {
|
||||
$result = $func($this);
|
||||
|
@ -563,25 +580,26 @@ class StdMessenger implements IMessenger {
|
|||
else $this->adone($result);
|
||||
}
|
||||
} finally {
|
||||
$this->endAction($until);
|
||||
$this->_endAction($until);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function printActions(bool $willEnd=false): void {
|
||||
function printActions(bool $endAction=false): void {
|
||||
$this->printTitles();
|
||||
$err = $this->err;
|
||||
$indentLevel = $this->getIndentLevel(false);
|
||||
$lastIndex = count($this->actions) - 1;
|
||||
$index = 0;
|
||||
foreach ($this->actions as &$action) {
|
||||
$mergeResult = $index++ == $lastIndex && $willEnd;
|
||||
$mergeResult = $index++ == $lastIndex && $endAction;
|
||||
$linePrefix = $action["line_prefix"];
|
||||
$level = $action["level"];
|
||||
$content = $action["content"];
|
||||
$printContent = $action["print_content"];
|
||||
$rsuccess = $action["result_success"];
|
||||
$rcontent = $action["result_content"];
|
||||
if ($level < $this->minLevel) continue;
|
||||
if ($mergeResult) {
|
||||
$this->_printAction(
|
||||
$linePrefix, $level,
|
||||
|
@ -598,6 +616,7 @@ class StdMessenger implements IMessenger {
|
|||
}
|
||||
$indentLevel++;
|
||||
}; unset($action);
|
||||
if ($endAction) $this->_endAction();
|
||||
}
|
||||
|
||||
function step($content, ?int $level=null): void {
|
||||
|
@ -609,7 +628,6 @@ class StdMessenger implements IMessenger {
|
|||
$this->action["result_success"] = true;
|
||||
$this->action["result_content"] = $content;
|
||||
$this->printActions(true);
|
||||
$this->endAction();
|
||||
}
|
||||
|
||||
function afailure($content=null): void {
|
||||
|
@ -617,7 +635,6 @@ class StdMessenger implements IMessenger {
|
|||
$this->action["result_success"] = false;
|
||||
$this->action["result_content"] = $content;
|
||||
$this->printActions(true);
|
||||
$this->endAction();
|
||||
}
|
||||
|
||||
function adone($content=null): void {
|
||||
|
@ -625,11 +642,10 @@ class StdMessenger implements IMessenger {
|
|||
$this->action["result_success"] = null;
|
||||
$this->action["result_content"] = $content;
|
||||
$this->printActions(true);
|
||||
$this->endAction();
|
||||
}
|
||||
|
||||
protected function endAction(?int $until=null): void {
|
||||
if ($until === null) $until = count($this->actions) - 1;
|
||||
function _endAction(?int $until=null): void {
|
||||
if ($until === null) $until = $this->_getActionMark() - 1;
|
||||
while (count($this->actions) > $until) {
|
||||
array_pop($this->actions);
|
||||
}
|
||||
|
@ -664,14 +680,14 @@ class StdMessenger implements IMessenger {
|
|||
function end(bool $all=false): void {
|
||||
if ($all) {
|
||||
while ($this->actions) $this->adone();
|
||||
while ($this->titles) $this->endTitle();
|
||||
$this->endSection();
|
||||
while ($this->titles) $this->_endTitle();
|
||||
$this->_endSection();
|
||||
} elseif ($this->actions) {
|
||||
$this->endAction();
|
||||
$this->_endAction();
|
||||
} elseif ($this->titles) {
|
||||
$this->endTitle();
|
||||
$this->_endTitle();
|
||||
} else {
|
||||
$this->endSection();
|
||||
$this->_endSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -9,39 +9,29 @@ $params = [];
|
|||
$count = count($argv) - 1;
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
switch ($argv[$i]) {
|
||||
case "-n":
|
||||
$params["color"] = false;
|
||||
break;
|
||||
case "+n":
|
||||
$params["color"] = true;
|
||||
break;
|
||||
case "-d":
|
||||
$params["debug"] = true;
|
||||
break;
|
||||
case "+d":
|
||||
$params["debug"] = false;
|
||||
break;
|
||||
case "-L":
|
||||
$i++;
|
||||
$params["output"] = $argv[$i];
|
||||
break;
|
||||
case "-D":
|
||||
$params["default_level"] = "debug";
|
||||
$params["debug"] = true;
|
||||
break;
|
||||
case "-N":
|
||||
$params["default_level"] = "normal";
|
||||
break;
|
||||
case "-M":
|
||||
$params["default_level"] = "major";
|
||||
break;
|
||||
case "-i":
|
||||
$i++;
|
||||
$params["id"] = $argv[$i];
|
||||
break;
|
||||
case "-t":
|
||||
$params["add_date"] = true;
|
||||
break;
|
||||
case "-L": $i++; $params["output"] = $argv[$i]; break;
|
||||
case "-i": $i++; $params["id"] = $argv[$i]; break;
|
||||
case "-t": $params["add_date"] = true; break;
|
||||
case "-n": $params["color"] = false; break;
|
||||
case "+n": $params["color"] = true; break;
|
||||
case "-d": $params["debug"] = true; break;
|
||||
case "+d": $params["debug"] = false; break;
|
||||
case "-D": $params["default_level"] = "debug"; break;
|
||||
case "-m": $params["default_level"] = "minor"; break;
|
||||
case "-M": $params["default_level"] = "major"; break;
|
||||
case "--show-debug":
|
||||
case "--sd": $params["min_level"] = "debug"; break;
|
||||
case "--show-minor":
|
||||
case "--verbose":
|
||||
case "--sm": $params["min_level"] = "minor"; break;
|
||||
case "--show-normal":
|
||||
case "--sn": $params["min_level"] = "normal"; break;
|
||||
case "--show-major":
|
||||
case "--quiet":
|
||||
case "--sM": $params["min_level"] = "major"; break;
|
||||
case "--show-none":
|
||||
case "--silent":
|
||||
case "--sx": $params["min_level"] = "none"; break;
|
||||
}
|
||||
}
|
||||
$msg = new StdMessenger($params);
|
||||
|
|
Loading…
Reference in New Issue