From 291b478d93be19b2aef8278e1effb22e91c6fb07 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 11 Jun 2024 15:56:54 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- nur_src/cli/Application.php | 51 ++++++++++++-------- src/app/app.php | 17 +++++-- src/output/_messenger.php | 45 ++++++++---------- src/output/console.php | 29 ++++++++++++ src/output/log.php | 41 +++++------------ src/output/msg.php | 92 +++++++++++++++++++++++-------------- src/output/say.php | 30 +++++------- src/php/func.php | 12 +++++ 8 files changed, 186 insertions(+), 131 deletions(-) create mode 100644 src/output/console.php diff --git a/nur_src/cli/Application.php b/nur_src/cli/Application.php index 2ad278d..a3d3b80 100644 --- a/nur_src/cli/Application.php +++ b/nur_src/cli/Application.php @@ -12,8 +12,8 @@ use nur\path; use nur\sery\app\app; use nur\sery\output\log as nlog; use nur\sery\output\msg as nmsg; -use nur\sery\output\say as nsay; -use nur\sery\output\std\StdMessenger as nStdMessenger; +use nur\sery\output\console as nconsole; +use nur\sery\output\std\StdMessenger; /** * Class Application: application de base @@ -43,6 +43,9 @@ abstract class Application { const VARDIR = null; const LOGDIR = null; + /** @var bool faut-il activer automatiquement l'écriture dans les logs */ + const USE_LOGFILE = null; + /** @var bool faut-il maintenir un fichier de suivi du process? */ const USE_RUNFILE = false; @@ -55,20 +58,16 @@ abstract class Application { const USE_RUNLOCK = false; protected static function _app_init(): void { - app::init(static::class); - config::set_fact(config::FACT_CLI_APP); - nmsg::set_messenger_class(nStdMessenger::class); - nmsg::reset_params(["min_level" => nmsg::DEBUG]); - + # avant que l'application soit configurée, configurer le mode debug msg::set_messenger_class(Console::class); msg::get()->setParametrableParams([ # En ligne de commande, on peut afficher les messages loggués "display_log" => true, ]); - # avant que l'application soit configurée, rester en mode debug msg::get()->setLevels(msg::DEBUG_LEVELS, msg::DEBUG_LEVELS, msg::DEBUG); + # si un fichier nommé .default-profile-devel existe dans le répertoire de # l'application ou du projet, alors le profil par défaut est devel global $argv; @@ -85,6 +84,11 @@ abstract class Application { $projdir = path::dirname($projdir); if ($projdir == "/") break; } + + app::init(static::class); + nmsg::set_messenger(new StdMessenger([ + "min_level" => nmsg::DEBUG, + ])); } protected static function _app_configure(Application $app): void { @@ -92,7 +96,18 @@ abstract class Application { # revenir à la configuration par défaut une fois que la configuration # initiale est faite msg::get()->setLevels(msg::PRINT_LEVELS, msg::LOG_LEVELS, msg::TYPE_LEVELS); - nsay::reset_params(["min_level" => nmsg::NORMAL]); + + $msgs = ["console" => new StdMessenger([ + "min_level" => nmsg::NORMAL, + ])]; + if (static::USE_LOGFILE) { + $msgs["log"] = new StdMessenger([ + "output" => app::get()->getLogfile(), + "min_level" => nmsg::MINOR, + "add_date" => true, + ]); + } + nmsg::init($msgs); $app->parseArgs(); config::configure(); @@ -213,7 +228,7 @@ abstract class Application { static function set_application_verbosity(string $verbosity): void { $msg = msg::get(); - $nsay = nsay::get(); + $nconsole = nconsole::get(); switch ($verbosity) { case "s": case "silent": @@ -222,7 +237,7 @@ abstract class Application { msg::TECH => msg::NEVER, msg::EXCEPTION => msg::NEVER, ]); - $nsay->resetParams([ + $nconsole->resetParams([ "min_level" => nmsg::NONE, ]); break; @@ -233,7 +248,7 @@ abstract class Application { msg::TECH => msg::CRITICAL, msg::EXCEPTION => msg::NEVER, ]); - $nsay->resetParams([ + $nconsole->resetParams([ "min_level" => nmsg::MAJOR, ]); break; @@ -244,7 +259,7 @@ abstract class Application { msg::TECH => msg::MAJOR, msg::EXCEPTION => msg::NEVER, ]); - $nsay->resetParams([ + $nconsole->resetParams([ "min_level" => nmsg::MAJOR, ]); break; @@ -255,7 +270,7 @@ abstract class Application { msg::TECH => msg::MINOR, msg::EXCEPTION => msg::NEVER, ]); - $nsay->resetParams([ + $nconsole->resetParams([ "min_level" => nmsg::MINOR, ]); break; @@ -267,7 +282,7 @@ abstract class Application { msg::TECH => msg::MINOR, msg::EXCEPTION => msg::NORMAL, ], null, msg::DEBUG); - $nsay->resetParams([ + $nconsole->resetParams([ "min_level" => nmsg::DEBUG, ]); break; @@ -279,7 +294,7 @@ abstract class Application { msg::TECH => msg::MINOR, msg::EXCEPTION => msg::MINOR, ], null, msg::DEBUG); - $nsay->resetParams([ + $nconsole->resetParams([ "min_level" => nmsg::DEBUG, ]); break; @@ -296,14 +311,14 @@ abstract class Application { msg::get()->setParametrableParams(["log_output" => $logfile]); nlog::create_or_reset_params([ "output" => $logfile, - ], nStdMessenger::class, [ + ], StdMessenger::class, [ "add_date" => true, "min_level" => nlog::MINOR, ]); } static function set_application_color(bool $color): void { msg::get()->setParametrableParams(["color" => $color]); - nsay::reset_params([ + nconsole::reset_params([ "color" => $color, ]); } diff --git a/src/app/app.php b/src/app/app.php index 336c439..a3eb7d4 100644 --- a/src/app/app.php +++ b/src/app/app.php @@ -2,8 +2,8 @@ namespace nur\sery\app; use nur\cli\Application; -use nur\os; use nur\sery\os\path; +use nur\sery\os\sh; use nur\sery\ValueException; class app { @@ -27,6 +27,8 @@ class app { $etcdir = constant("$app::ETCDIR"); $vardir = constant("$app::VARDIR"); $logdir = constant("$app::LOGDIR"); + } elseif (is_array($app)) { + return new static($app); } else { throw ValueException::invalid_type($app, Application::class); } @@ -202,7 +204,9 @@ class app { } function getVarfile(string $name, $profile=null): string { - return $this->withProfile($this->fencedJoin($this->vardir, $name), $profile); + $file = $this->withProfile($this->fencedJoin($this->vardir, $name), $profile); + sh::mkdirof($file); + return $file; } protected string $logdir; @@ -211,8 +215,11 @@ class app { return $this->logdir; } - function getLogfile(string $name, $profile=null): string { - return $this->withProfile($this->fencedJoin($this->logdir, $name), $profile); + function getLogfile(?string $name=null, $profile=null): string { + if ($name === null) $name = "{$this->name}.log"; + $file = $this->withProfile($this->fencedJoin($this->logdir, $name), $profile); + sh::mkdirof($file); + return $file; } /** @@ -233,7 +240,7 @@ class app { $file = path::reljoin($this->datadir, $file); } $file = $this->withProfile($file, $profile); - if ($ensureDir) os::mkdirof($file); + if ($ensureDir) sh::mkdirof($file); return $file; } diff --git a/src/output/_messenger.php b/src/output/_messenger.php index e24ed09..c5dc808 100644 --- a/src/output/_messenger.php +++ b/src/output/_messenger.php @@ -1,44 +1,39 @@ clone($params); } - /** @var IMessenger */ - protected static $msg; - - /** @var IMessenger[] */ - protected static $stack; - - /** pousser une nouvelle instance avec un nouveau paramétrage sur la pile */ - static function push(?array $params=null) { - self::$stack[] = static::get(); - self::$msg = self::new($params); - } - - /** dépiler la précédente instance */ - static function pop(): IMessenger { - if (self::$stack) $msg = self::$msg = array_pop(self::$stack); - else $msg = self::$msg; - return $msg; - } - static final function __callStatic($name, $args) { $name = str::us2camel($name); call_user_func_array([static::get(), $name], $args); diff --git a/src/output/console.php b/src/output/console.php new file mode 100644 index 0000000..bdcd612 --- /dev/null +++ b/src/output/console.php @@ -0,0 +1,29 @@ +