]> git.mxchange.org Git - friendica.git/blob - src/Model/Log/ParsedLogIterator.php
3e1832d969998cc46e10c200fd0e6cbd7119da55
[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
22 namespace Friendica\Model\Log;
23
24 use Friendica\Util\ReversedFileReader;
25 use Friendica\Object\Log\ParsedLog;
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         /** @var \Iterator */
36         private $reader;
37
38         /** @var ParsedLog current iterator value*/
39         private $value;
40
41         /** @var int max number of lines to read */
42         private $limit;
43
44         /** @var array filters per column */
45         private $filters;
46
47         /** @var string search term */
48         private $search;
49
50
51         /**
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
56          */
57         public function __construct(string $filename, int $limit = 0, array $filters = [], string $search = "")
58         {
59                 $this->reader  = new ReversedFileReader($filename);
60                 $this->value   = null;
61                 $this->limit   = $limit;
62                 $this->filters = $filters;
63                 $this->search  = $search;
64         }
65
66         /**
67          * Check if parsed log line match filters.
68          * Always match if no filters are set.
69          *
70          * @param ParsedLog $parsedlog
71          * @return bool
72          */
73         private function filter($parsedlog)
74         {
75                 $match = true;
76                 foreach ($this->filters as $filter => $filtervalue) {
77                         switch ($filter) {
78                                 case "level":
79                                         $match = $match && ($parsedlog->level == strtoupper($filtervalue));
80                                         break;
81                                 case "context":
82                                         $match = $match && ($parsedlog->context == $filtervalue);
83                                         break;
84                         }
85                 }
86                 return $match;
87         }
88
89         /**
90          * Check if parsed log line match search.
91          * Always match if no search query is set.
92          *
93          * @param ParsedLog $parsedlog
94          * @return bool
95          */
96         private function search($parsedlog)
97         {
98                 if ($this->search != "") {
99                         return strstr($parsedlog->logline, $this->search) !== false;
100                 }
101                 return true;
102         }
103
104         /**
105          * Read a line from reader and parse.
106          * Returns null if limit is reached or the reader is invalid.
107          *
108          * @param ParsedLog $parsedlog
109          * @return ?ParsedLog
110          */
111         private function read()
112         {
113                 $this->reader->next();
114                 if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
115                         return null;
116                 }
117
118                 $line = $this->reader->current();
119                 return new ParsedLog($this->reader->key(), $line);
120         }
121
122         public function next()
123         {
124                 $parsed = $this->read();
125
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();
131                 }
132                 $this->value = $parsed;
133         }
134
135         public function rewind()
136         {
137                 $this->value = null;
138                 $this->reader->rewind();
139                 $this->next();
140         }
141
142         public function key()
143         {
144                 return $this->reader->key();
145         }
146
147         public function current()
148         {
149                 return $this->value;
150         }
151
152         public function valid()
153         {
154                 return ! is_null($this->value);
155         }
156 }