Fix code formatting

This commit is contained in:
fabrixxm 2021-05-24 22:05:02 +02:00
parent 5b9aeeeca9
commit 84fa668845
4 changed files with 102 additions and 99 deletions

View File

@ -18,11 +18,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
* *
*/ */
namespace Friendica\Model\Log; namespace Friendica\Model\Log;
use \Friendica\Util\ReversedFileReader; use Friendica\Util\ReversedFileReader;
use \Friendica\Object\Log\ParsedLog; use Friendica\Object\Log\ParsedLog;
/** /**
* An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances * An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances
@ -36,7 +36,7 @@ class ParsedLogIterator implements \Iterator
private $reader; private $reader;
/** @var ParsedLog current iterator value*/ /** @var ParsedLog current iterator value*/
private $value; private $value;
/** @var int max number of lines to read */ /** @var int max number of lines to read */
private $limit; private $limit;
@ -54,19 +54,19 @@ class ParsedLogIterator implements \Iterator
* @param array $filter filters per column * @param array $filter filters per column
* @param string $search string to search to filter lines * @param string $search string to search to filter lines
*/ */
public function __construct(string $filename, int $limit=0, array $filters=[], string $search="") public function __construct(string $filename, int $limit = 0, array $filters = [], string $search = "")
{ {
$this->reader = new ReversedFileReader($filename); $this->reader = new ReversedFileReader($filename);
$this->value = null; $this->value = null;
$this->limit = $limit; $this->limit = $limit;
$this->filters = $filters; $this->filters = $filters;
$this->search = $search; $this->search = $search;
} }
/** /**
* Check if parsed log line match filters. * Check if parsed log line match filters.
* Always match if no filters are set. * Always match if no filters are set.
* *
* @param ParsedLog $parsedlog * @param ParsedLog $parsedlog
* @return bool * @return bool
*/ */
@ -74,7 +74,7 @@ class ParsedLogIterator implements \Iterator
{ {
$match = true; $match = true;
foreach ($this->filters as $filter => $filtervalue) { foreach ($this->filters as $filter => $filtervalue) {
switch($filter) { switch ($filter) {
case "level": case "level":
$match = $match && ($parsedlog->level == strtoupper($filtervalue)); $match = $match && ($parsedlog->level == strtoupper($filtervalue));
break; break;
@ -89,7 +89,7 @@ class ParsedLogIterator implements \Iterator
/** /**
* Check if parsed log line match search. * Check if parsed log line match search.
* Always match if no search query is set. * Always match if no search query is set.
* *
* @param ParsedLog $parsedlog * @param ParsedLog $parsedlog
* @return bool * @return bool
*/ */
@ -98,20 +98,20 @@ class ParsedLogIterator implements \Iterator
if ($this->search != "") { if ($this->search != "") {
return strstr($parsedlog->logline, $this->search) !== false; return strstr($parsedlog->logline, $this->search) !== false;
} }
return True; return true;
} }
/** /**
* Read a line from reader and parse. * Read a line from reader and parse.
* Returns null if limit is reached or the reader is invalid. * Returns null if limit is reached or the reader is invalid.
* *
* @param ParsedLog $parsedlog * @param ParsedLog $parsedlog
* @return ?ParsedLog * @return ?ParsedLog
*/ */
private function read() private function read()
{ {
$this->reader->next(); $this->reader->next();
if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) { if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
return null; return null;
} }
@ -126,7 +126,7 @@ class ParsedLogIterator implements \Iterator
// if read() has not retuned none and // if read() has not retuned none and
// the line don't match filters or search // the line don't match filters or search
// read the next line // read the next line
while(is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) { while (is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) {
$parsed = $this->read(); $parsed = $this->read();
} }
$this->value = $parsed; $this->value = $parsed;
@ -153,6 +153,4 @@ class ParsedLogIterator implements \Iterator
{ {
return ! is_null($this->value); return ! is_null($this->value);
} }
} }

View File

@ -39,12 +39,12 @@ class View extends BaseAdmin
$t = Renderer::getMarkupTemplate('admin/logs/view.tpl'); $t = Renderer::getMarkupTemplate('admin/logs/view.tpl');
DI::page()->registerFooterScript(Theme::getPathForFile('js/module/admin/logs/view.js')); DI::page()->registerFooterScript(Theme::getPathForFile('js/module/admin/logs/view.js'));
$f = DI::config()->get('system', 'logfile'); $f = DI::config()->get('system', 'logfile');
$data = null; $data = null;
$error = null; $error = null;
$search = $_GET['q'] ?? ''; $search = $_GET['q'] ?? '';
$filters_valid_values = [ $filters_valid_values = [
'level' => [ 'level' => [
'', '',
@ -58,10 +58,10 @@ class View extends BaseAdmin
'context' => ['', 'index', 'worker'], 'context' => ['', 'index', 'worker'],
]; ];
$filters = [ $filters = [
'level' => $_GET['level'] ?? '', 'level' => $_GET['level'] ?? '',
'context' => $_GET['context'] ?? '', 'context' => $_GET['context'] ?? '',
]; ];
foreach($filters as $k=>$v) { foreach ($filters as $k => $v) {
if ($v == '' || !in_array($v, $filters_valid_values[$k])) { if ($v == '' || !in_array($v, $filters_valid_values[$k])) {
unset($filters[$k]); unset($filters[$k]);
} }
@ -77,14 +77,14 @@ class View extends BaseAdmin
} }
} }
return Renderer::replaceMacros($t, [ return Renderer::replaceMacros($t, [
'$title' => DI::l10n()->t('Administration'), '$title' => DI::l10n()->t('Administration'),
'$page' => DI::l10n()->t('View Logs'), '$page' => DI::l10n()->t('View Logs'),
'$data' => $data, '$data' => $data,
'$q' => $search, '$q' => $search,
'$filters' => $filters, '$filters' => $filters,
'$filtersvalues' => $filters_valid_values, '$filtersvalues' => $filters_valid_values,
'$error' => $error, '$error' => $error,
'$logname' => DI::config()->get('system', 'logfile'), '$logname' => DI::config()->get('system', 'logfile'),
]); ]);
} }
} }

View File

@ -18,6 +18,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
* *
*/ */
namespace Friendica\Object\Log; namespace Friendica\Object\Log;
/** /**
@ -64,18 +65,22 @@ class ParsedLog
private function parse($logline) private function parse($logline)
{ {
list($logline, $jsonsource) = explode(' - ', $logline); list($logline, $jsonsource) = explode(' - ', $logline);
$jsondata = null; $jsondata = null;
if (strpos($logline, '{"') > 0) { if (strpos($logline, '{"') > 0) {
list($logline, $jsondata) = explode('{"', $logline, 2); list($logline, $jsondata) = explode('{"', $logline, 2);
$jsondata = '{"' . $jsondata; $jsondata = '{"' . $jsondata;
} }
preg_match(self::REGEXP, $logline, $matches); preg_match(self::REGEXP, $logline, $matches);
$this->date = $matches[1];
$this->date = $matches[1];
$this->context = $matches[2]; $this->context = $matches[2];
$this->level = $matches[3]; $this->level = $matches[3];
$this->message = $matches[4]; $this->message = $matches[4];
$this->data = $jsondata; $this->data = $jsondata;
$this->source = $jsonsource; $this->source = $jsonsource;
$this->try_fix_json(); $this->try_fix_json();
$this->logline = $logline; $this->logline = $logline;
@ -83,7 +88,7 @@ class ParsedLog
/** /**
* Fix message / data split * Fix message / data split
* *
* In log boundary between message and json data is not specified. * In log boundary between message and json data is not specified.
* If message contains '{' the parser thinks there starts the json data. * If message contains '{' the parser thinks there starts the json data.
* This method try to parse the found json and if it fails, search for next '{' * This method try to parse the found json and if it fails, search for next '{'
@ -112,12 +117,12 @@ class ParsedLog
* *
* @return array * @return array
*/ */
public function get_data() { public function get_data()
{
$data = json_decode($this->data, true); $data = json_decode($this->data, true);
if ($data) { if ($data) {
foreach($data as $k => $v) { foreach ($data as $k => $v) {
$v = print_r($v, true); $data[$k] = print_r($v, true);
$data[$k] = $v;
} }
} }
return $data; return $data;
@ -128,7 +133,8 @@ class ParsedLog
* *
* @return array * @return array
*/ */
public function get_source() { public function get_source()
{
return json_decode($this->source, true); return json_decode($this->source, true);
} }
} }

View File

@ -21,7 +21,6 @@
namespace Friendica\Util; namespace Friendica\Util;
/** /**
* An iterator which returns lines from file in reversed order * An iterator which returns lines from file in reversed order
* *
@ -29,87 +28,87 @@ namespace Friendica\Util;
*/ */
class ReversedFileReader implements \Iterator class ReversedFileReader implements \Iterator
{ {
const BUFFER_SIZE = 4096; const BUFFER_SIZE = 4096;
const SEPARATOR = "\n"; const SEPARATOR = "\n";
/** @var int */ /** @var int */
private $filesize; private $filesize;
/** @var int */ /** @var int */
private $pos; private $pos;
/** @var array */ /** @var array */
private $buffer; private $buffer;
/** @var int */ /** @var int */
private $key; private $key;
/** @var string */ /** @var string */
private $value; private $value;
public function __construct($filename) public function __construct($filename)
{ {
$this->_fh = fopen($filename, 'r'); $this->_fh = fopen($filename, 'r');
if (!$this->_fh) { if (!$this->_fh) {
// this should use a custom exception. // this should use a custom exception.
throw \Exception("Unable to open $filename"); throw \Exception("Unable to open $filename");
} }
$this->filesize = filesize($filename); $this->filesize = filesize($filename);
$this->pos = -1; $this->pos = -1;
$this->buffer = null; $this->buffer = null;
$this->key = -1; $this->key = -1;
$this->value = null; $this->value = null;
} }
public function _read($size) public function _read($size)
{ {
$this->pos -= $size; $this->pos -= $size;
fseek($this->_fh, $this->pos); fseek($this->_fh, $this->pos);
return fread($this->_fh, $size); return fread($this->_fh, $size);
} }
public function _readline() public function _readline()
{ {
$buffer =& $this->buffer; $buffer = & $this->buffer;
while (true) { while (true) {
if ($this->pos == 0) { if ($this->pos == 0) {
return array_pop($buffer); return array_pop($buffer);
} }
if (count($buffer) > 1) { if (count($buffer) > 1) {
return array_pop($buffer); return array_pop($buffer);
} }
$buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]); $buffer = explode(self::SEPARATOR, $this->_read(self::BUFFER_SIZE) . $buffer[0]);
} }
} }
public function next() public function next()
{ {
++$this->key; ++$this->key;
$this->value = $this->_readline(); $this->value = $this->_readline();
} }
public function rewind() public function rewind()
{ {
if ($this->filesize > 0) { if ($this->filesize > 0) {
$this->pos = $this->filesize; $this->pos = $this->filesize;
$this->value = null; $this->value = null;
$this->key = -1; $this->key = -1;
$this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE)); $this->buffer = explode(self::SEPARATOR, $this->_read($this->filesize % self::BUFFER_SIZE ?: self::BUFFER_SIZE));
$this->next(); $this->next();
} }
} }
public function key() public function key()
{ {
return $this->key; return $this->key;
} }
public function current() public function current()
{ {
return $this->value; return $this->value;
} }
public function valid() public function valid()
{ {
return ! is_null($this->value); return ! is_null($this->value);
} }