mirror of
https://github.com/friendica/friendica
synced 2025-04-26 10:30:11 +00:00
Add search and filter to log view
This commit is contained in:
parent
b8fc6a8c02
commit
5b9aeeeca9
5 changed files with 248 additions and 81 deletions
|
@ -28,36 +28,113 @@ use \Friendica\Object\Log\ParsedLog;
|
|||
* An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances
|
||||
*
|
||||
* Uses `\Friendica\Util\ReversedFileReader` to fetch log lines
|
||||
* from newest to oldest
|
||||
* from newest to oldest.
|
||||
*/
|
||||
class ParsedLogIterator implements \Iterator
|
||||
{
|
||||
public function __construct(string $filename, int $limit=0)
|
||||
/** @var \Iterator */
|
||||
private $reader;
|
||||
|
||||
/** @var ParsedLog current iterator value*/
|
||||
private $value;
|
||||
|
||||
/** @var int max number of lines to read */
|
||||
private $limit;
|
||||
|
||||
/** @var array filters per column */
|
||||
private $filters;
|
||||
|
||||
/** @var string search term */
|
||||
private $search;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $filename File to open
|
||||
* @param int $limit Max num of lines to read
|
||||
* @param array $filter filters per column
|
||||
* @param string $search string to search to filter lines
|
||||
*/
|
||||
public function __construct(string $filename, int $limit=0, array $filters=[], string $search="")
|
||||
{
|
||||
$this->reader = new ReversedFileReader($filename);
|
||||
$this->_value = null;
|
||||
$this->_limit = $limit;
|
||||
$this->value = null;
|
||||
$this->limit = $limit;
|
||||
$this->filters = $filters;
|
||||
$this->search = $search;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if parsed log line match filters.
|
||||
* Always match if no filters are set.
|
||||
*
|
||||
* @param ParsedLog $parsedlog
|
||||
* @return bool
|
||||
*/
|
||||
private function filter($parsedlog)
|
||||
{
|
||||
$match = true;
|
||||
foreach ($this->filters as $filter => $filtervalue) {
|
||||
switch($filter) {
|
||||
case "level":
|
||||
$match = $match && ($parsedlog->level == strtoupper($filtervalue));
|
||||
break;
|
||||
case "context":
|
||||
$match = $match && ($parsedlog->context == $filtervalue);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $match;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if parsed log line match search.
|
||||
* Always match if no search query is set.
|
||||
*
|
||||
* @param ParsedLog $parsedlog
|
||||
* @return bool
|
||||
*/
|
||||
private function search($parsedlog)
|
||||
{
|
||||
if ($this->search != "") {
|
||||
return strstr($parsedlog->logline, $this->search) !== false;
|
||||
}
|
||||
return True;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a line from reader and parse.
|
||||
* Returns null if limit is reached or the reader is invalid.
|
||||
*
|
||||
* @param ParsedLog $parsedlog
|
||||
* @return ?ParsedLog
|
||||
*/
|
||||
private function read()
|
||||
{
|
||||
$this->reader->next();
|
||||
if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$line = $this->reader->current();
|
||||
return new ParsedLog($this->reader->key(), $line);
|
||||
}
|
||||
|
||||
public function next()
|
||||
{
|
||||
$this->reader->next();
|
||||
if ($this->_limit > 0 && $this->reader->key() > $this->_limit) {
|
||||
$this->_value = null;
|
||||
return;
|
||||
}
|
||||
if ($this->reader->valid()) {
|
||||
$line = $this->reader->current();
|
||||
$this->_value = new ParsedLog($this->reader->key(), $line);
|
||||
} else {
|
||||
$this->_value = null;
|
||||
}
|
||||
}
|
||||
$parsed = $this->read();
|
||||
|
||||
// if read() has not retuned none and
|
||||
// the line don't match filters or search
|
||||
// read the next line
|
||||
while(is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) {
|
||||
$parsed = $this->read();
|
||||
}
|
||||
$this->value = $parsed;
|
||||
}
|
||||
|
||||
public function rewind()
|
||||
{
|
||||
$this->_value = null;
|
||||
$this->value = null;
|
||||
$this->reader->rewind();
|
||||
$this->next();
|
||||
}
|
||||
|
@ -69,12 +146,12 @@ class ParsedLogIterator implements \Iterator
|
|||
|
||||
public function current()
|
||||
{
|
||||
return $this->_value;
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function valid()
|
||||
{
|
||||
return ! is_null($this->_value);
|
||||
return ! is_null($this->value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue