]> git.mxchange.org Git - friendica.git/blob - src/Model/Log/ParsedLogIterator.php
Display structured logs in admin
[friendica.git] / src / Model / Log / ParsedLogIterator.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2021, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21 namespace Friendica\Model\Log;
22
23 use \Friendica\Util\ReversedFileReader;
24 use \Friendica\Object\Log\ParsedLog;
25
26
27 /**
28  * An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances
29  *
30  * Uses `\Friendica\Util\ReversedFileReader` to fetch log lines
31  * from newest to oldest
32  */
33 class ParsedLogIterator implements \Iterator
34 {
35         public function __construct(string $filename, int $limit=0)
36         {
37                 $this->reader = new ReversedFileReader($filename);
38                 $this->_value = null;
39                 $this->_limit = $limit;
40         }
41
42         public function next()
43         {
44                 $this->reader->next();
45                 if ($this->_limit > 0 && $this->reader->key() > $this->_limit) {
46                         $this->_value = null;
47                         return;
48                 }
49                 if ($this->reader->valid()) {
50                         $line = $this->reader->current();
51                         $this->_value = new ParsedLog($this->reader->key(), $line);
52                 } else {
53                         $this->_value = null;
54                 }
55         }
56
57
58         public function rewind()
59         {
60                 $this->_value = null;
61                 $this->reader->rewind();
62                 $this->next();
63         }
64
65         public function key()
66         {
67                 return $this->reader->key();
68         }
69
70         public function current()
71         {
72                 return $this->_value;
73         }
74
75         public function valid()
76         {
77                 return ! is_null($this->_value);
78         }
79
80 }
81