123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace XMPPHP;
- class Log
- {
- const LEVEL_ERROR = 0;
- const LEVEL_WARNING = 1;
- const LEVEL_INFO = 2;
- const LEVEL_DEBUG = 3;
- const LEVEL_VERBOSE = 4;
-
- protected $data = [];
-
- protected $names = ['ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE'];
-
- protected $runlevel;
-
- protected $printout;
-
- public function __construct($printout = false, ?int $runlevel = self::LEVEL_INFO)
- {
- $this->printout = (bool) $printout;
- $this->runlevel = (int) ($runlevel ?? 0);
- }
-
- public function log($msg, $runlevel = self::LEVEL_INFO): void
- {
- $time = time();
-
- if ($this->printout and $runlevel <= $this->runlevel) {
- $this->writeLine($msg, $runlevel, $time);
- }
- }
-
- protected function writeLine(string $msg, int $runlevel, int $time): void
- {
-
- echo $time . " [" . $this->names[$runlevel] . "]: " . $msg . "\n";
- flush();
- }
-
- public function printout(bool $clear = true, int $runlevel = null): void
- {
- if ($runlevel === null) {
- $runlevel = $this->runlevel;
- }
- foreach ($this->data as $data) {
- if ($runlevel <= $data[0]) {
- $this->writeLine($data[1], $runlevel, $data[2]);
- }
- }
- if ($clear) {
- $this->data = [];
- }
- }
- }
|