From 1c5674558b74b0fa3f5b06ca233c4349ca9910f0 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 28 Dec 2023 15:56:24 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/output/TODO.md | 2 ++ src/output/std/StdOutput.php | 30 +++++++++++++++++++++++++----- tbin/.gitignore | 1 + tbin/test-output-forever.php | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tbin/.gitignore create mode 100755 tbin/test-output-forever.php diff --git a/src/output/TODO.md b/src/output/TODO.md index e4da2ea..87c6b0e 100644 --- a/src/output/TODO.md +++ b/src/output/TODO.md @@ -2,5 +2,7 @@ * [ ] possibilité de paramétrer le nom du fichier destination pour faire une rotation des logs + * [ ] lors de la rotation, si l'ouverture du nouveau fichier échoue, continuer + à écrire dans l'ancien fichier -*- coding: utf-8 mode: markdown -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8:noeol:binary \ No newline at end of file diff --git a/src/output/std/StdOutput.php b/src/output/std/StdOutput.php index 1945dc1..2667764 100644 --- a/src/output/std/StdOutput.php +++ b/src/output/std/StdOutput.php @@ -80,6 +80,7 @@ class StdOutput { function resetParams(?array $params=null): void { $output = cl::get($params, "output"); + $maskErrors = null; $color = cl::get($params, "color"); $filterTags = cl::get($params, "filter_tags"); $indent = cl::get($params, "indent"); @@ -92,7 +93,10 @@ class StdOutput { $outf = STDERR; } elseif (!is_resource($output)) { # si $outf est un nom de fichier, vérifier que l'ouverture se fait sans - # erreur. à partir de là, plus aucune gestion d'erreur n'est faite + # erreur. à partir de là, plus aucune gestion d'erreur n'est faite, à + # part afficher les erreurs d'écriture la première fois qu'elles se + # produisent + $maskErrors = false; $outf = @fopen($output, "ab"); if ($outf === false) { $error = error_get_last(); @@ -105,6 +109,7 @@ class StdOutput { $outf = $output; } $this->outf = $outf; + $this->maskErrors = $maskErrors; if ($color === null) $color = stream_isatty($outf); if ($flush === null) $flush = false; } @@ -117,6 +122,9 @@ class StdOutput { /** @var resource */ protected $outf; + /** @var bool faut-il masquer les erreurs d'écriture? */ + protected $maskErrors; + /** @var bool faut-il autoriser la sortie en couleur? */ protected $color; @@ -220,14 +228,26 @@ class StdOutput { return $lines; } + private function _fwrite($outf, string $data): void { + if ($this->maskErrors === null) { + # masquer les erreurs d'écriture en permanence + @fwrite($outf, $data); + return; + } + # masquer uniquement la première erreur, jusqu'à ce que l'erreur disparaisse + if ($this->maskErrors) $r = @fwrite($outf, $data); + else $r = fwrite($outf, $data); + $this->maskErrors = $r === false; + } + function writeLines($indent, array $lines, bool $addNl=false): void { $outf = $this->outf; foreach ($lines as $line) { - if ($indent !== null) fwrite($outf, $indent); - fwrite($outf, $line); - if ($addNl) fwrite($outf, "\n"); + if ($indent !== null) $this->_fwrite($outf, $indent); + $this->_fwrite($outf, $line); + if ($addNl) $this->_fwrite($outf, "\n"); } - if ($this->flush) fflush($outf); + if ($this->flush) @fflush($outf); } function write(...$values): void { diff --git a/tbin/.gitignore b/tbin/.gitignore new file mode 100644 index 0000000..cf227b5 --- /dev/null +++ b/tbin/.gitignore @@ -0,0 +1 @@ +/output-forever.log diff --git a/tbin/test-output-forever.php b/tbin/test-output-forever.php new file mode 100755 index 0000000..6e4f5d2 --- /dev/null +++ b/tbin/test-output-forever.php @@ -0,0 +1,18 @@ +#!/usr/bin/php + "output-forever.log", +])); + +$index = 1; +while (true) { + msg::info("info $index"); + $index++; + sleep(1); +}