3 * @copyright Copyright (C) 2021, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
21 namespace Friendica\Model\Log;
23 use \Friendica\Util\ReversedFileReader;
24 use \Friendica\Object\Log\ParsedLog;
28 * An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances
30 * Uses `\Friendica\Util\ReversedFileReader` to fetch log lines
31 * from newest to oldest.
33 class ParsedLogIterator implements \Iterator
38 /** @var ParsedLog current iterator value*/
41 /** @var int max number of lines to read */
44 /** @var array filters per column */
47 /** @var string search term */
52 * @param string $filename File to open
53 * @param int $limit Max num of lines to read
54 * @param array $filter filters per column
55 * @param string $search string to search to filter lines
57 public function __construct(string $filename, int $limit=0, array $filters=[], string $search="")
59 $this->reader = new ReversedFileReader($filename);
61 $this->limit = $limit;
62 $this->filters = $filters;
63 $this->search = $search;
67 * Check if parsed log line match filters.
68 * Always match if no filters are set.
70 * @param ParsedLog $parsedlog
73 private function filter($parsedlog)
76 foreach ($this->filters as $filter => $filtervalue) {
79 $match = $match && ($parsedlog->level == strtoupper($filtervalue));
82 $match = $match && ($parsedlog->context == $filtervalue);
90 * Check if parsed log line match search.
91 * Always match if no search query is set.
93 * @param ParsedLog $parsedlog
96 private function search($parsedlog)
98 if ($this->search != "") {
99 return strstr($parsedlog->logline, $this->search) !== false;
105 * Read a line from reader and parse.
106 * Returns null if limit is reached or the reader is invalid.
108 * @param ParsedLog $parsedlog
111 private function read()
113 $this->reader->next();
114 if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
118 $line = $this->reader->current();
119 return new ParsedLog($this->reader->key(), $line);
122 public function next()
124 $parsed = $this->read();
126 // if read() has not retuned none and
127 // the line don't match filters or search
128 // read the next line
129 while(is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) {
130 $parsed = $this->read();
132 $this->value = $parsed;
135 public function rewind()
138 $this->reader->rewind();
142 public function key()
144 return $this->reader->key();
147 public function current()
152 public function valid()
154 return ! is_null($this->value);