]> git.mxchange.org Git - friendica.git/blob - src/Model/Log/ParsedLogIterator.php
Merge remote-tracking branch 'upstream/develop' into inbox-gsid
[friendica.git] / src / Model / Log / ParsedLogIterator.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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
22 namespace Friendica\Model\Log;
23
24 use Friendica\Util\ReversedFileReader;
25 use Friendica\Object\Log\ParsedLogLine;
26
27 /**
28  * An iterator which returns `\Friendica\Objec\Log\ParsedLogLine` instances
29  *
30  * Uses `\Friendica\Util\ReversedFileReader` to fetch log lines
31  * from newest to oldest.
32  */
33 class ParsedLogIterator implements \Iterator
34 {
35         /** @var \Iterator */
36         private $reader;
37
38         /** @var ParsedLogLine current iterator value*/
39         private $value = null;
40
41         /** @var int max number of lines to read */
42         private $limit = 0;
43
44         /** @var array filters per column */
45         private $filters = [];
46
47         /** @var string search term */
48         private $search = '';
49
50
51         /**
52          * @param ReversedFileReader $reader
53          */
54         public function __construct(ReversedFileReader $reader)
55         {
56                 $this->reader = $reader;
57         }
58
59         /**
60          * @param string $filename      File to open
61          * @return $this
62          */
63         public function open(string $filename): ParsedLogIterator
64         {
65                 $this->reader->open($filename);
66                 return $this;
67         }
68
69         /**
70          * @param int $limit            Max num of lines to read
71          * @return $this
72          */
73         public function withLimit(int $limit): ParsedLogIterator
74         {
75                 $this->limit = $limit;
76                 return $this;
77         }
78
79         /**
80          * @param array $filters                filters per column
81          * @return $this
82          */
83         public function withFilters(array $filters): ParsedLogIterator
84         {
85                 $this->filters = $filters;
86                 return $this;
87         }
88
89         /**
90          * @param string $search        string to search to filter lines
91          * @return $this
92          */
93         public function withSearch(string $search): ParsedLogIterator
94         {
95                 $this->search = $search;
96                 return $this;
97         }
98
99         /**
100          * Check if parsed log line match filters.
101          * Always match if no filters are set.
102          *
103          * @param ParsedLogLine $parsedlogline ParsedLogLine instance
104          * @return bool Wether the parse log line matches
105          */
106         private function filter(ParsedLogLine $parsedlogline): bool
107         {
108                 $match = true;
109                 foreach ($this->filters as $filter => $filtervalue) {
110                         switch ($filter) {
111                                 case 'level':
112                                         $match = $match && ($parsedlogline->level == strtoupper($filtervalue));
113                                         break;
114
115                                 case 'context':
116                                         $match = $match && ($parsedlogline->context == $filtervalue);
117                                         break;
118                         }
119                 }
120                 return $match;
121         }
122
123         /**
124          * Check if parsed log line match search.
125          * Always match if no search query is set.
126          *
127          * @param ParsedLogLine $parsedlogline
128          * @return bool
129          */
130         private function search(ParsedLogLine $parsedlogline): bool
131         {
132                 if ($this->search != '') {
133                         return strstr($parsedlogline->logline, $this->search) !== false;
134                 }
135                 return true;
136         }
137
138         /**
139          * Read a line from reader and parse.
140          * Returns null if limit is reached or the reader is invalid.
141          *
142          * @return ?ParsedLogLine
143          */
144         private function read()
145         {
146                 $this->reader->next();
147                 if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
148                         return null;
149                 }
150
151                 $line = $this->reader->current();
152                 return new ParsedLogLine($this->reader->key(), $line);
153         }
154
155
156         /**
157          * Fetch next parsed log line which match with filters or search and
158          * set it as current iterator value.
159          *
160          * @see Iterator::next()
161          * @return void
162          */
163         public function next(): void
164         {
165                 $parsed = $this->read();
166
167                 while (is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) {
168                         $parsed = $this->read();
169                 }
170                 $this->value = $parsed;
171         }
172
173
174         /**
175          * Rewind the iterator to the first matching log line
176          *
177          * @see Iterator::rewind()
178          * @return void
179          */
180         public function rewind(): void
181         {
182                 $this->value = null;
183                 $this->reader->rewind();
184                 $this->next();
185         }
186
187         /**
188          * Return current parsed log line number
189          *
190          * @see Iterator::key()
191          * @see ReversedFileReader::key()
192          * @return int
193          */
194         public function key(): int
195         {
196                 return $this->reader->key();
197         }
198
199         /**
200          * Return current iterator value
201          *
202          * @see Iterator::current()
203          * @return ?ParsedLogLine
204          */
205         public function current(): ?ParsedLogLine
206         {
207                 return $this->value;
208         }
209
210         /**
211          * Checks if current iterator value is valid, that is, not null
212          *
213          * @see Iterator::valid()
214          * @return bool
215          */
216         public function valid(): bool
217         {
218                 return !is_null($this->value);
219         }
220 }