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/>.
*
*/
namespace Friendica\Model\Log;
use \Friendica\Util\ReversedFileReader;
use \Friendica\Object\Log\ParsedLog;
use Friendica\Util\ReversedFileReader;
use Friendica\Object\Log\ParsedLog;
/**
* An iterator which returns `\Friendica\Objec\Log\ParsedLog` instances
@ -36,7 +36,7 @@ class ParsedLogIterator implements \Iterator
private $reader;
/** @var ParsedLog current iterator value*/
private $value;
private $value;
/** @var int max number of lines to read */
private $limit;
@ -54,19 +54,19 @@ class ParsedLogIterator implements \Iterator
* @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="")
public function __construct(string $filename, int $limit = 0, array $filters = [], string $search = "")
{
$this->reader = new ReversedFileReader($filename);
$this->value = null;
$this->limit = $limit;
$this->reader = new ReversedFileReader($filename);
$this->value = null;
$this->limit = $limit;
$this->filters = $filters;
$this->search = $search;
$this->search = $search;
}
/**
* Check if parsed log line match filters.
* Always match if no filters are set.
*
*
* @param ParsedLog $parsedlog
* @return bool
*/
@ -74,7 +74,7 @@ class ParsedLogIterator implements \Iterator
{
$match = true;
foreach ($this->filters as $filter => $filtervalue) {
switch($filter) {
switch ($filter) {
case "level":
$match = $match && ($parsedlog->level == strtoupper($filtervalue));
break;
@ -89,7 +89,7 @@ class ParsedLogIterator implements \Iterator
/**
* Check if parsed log line match search.
* Always match if no search query is set.
*
*
* @param ParsedLog $parsedlog
* @return bool
*/
@ -98,20 +98,20 @@ class ParsedLogIterator implements \Iterator
if ($this->search != "") {
return strstr($parsedlog->logline, $this->search) !== false;
}
return True;
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()) {
if ($this->limit > 0 && $this->reader->key() > $this->limit || !$this->reader->valid()) {
return null;
}
@ -126,7 +126,7 @@ class ParsedLogIterator implements \Iterator
// 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))) {
while (is_null($parsed) == false && !($this->filter($parsed) && $this->search($parsed))) {
$parsed = $this->read();
}
$this->value = $parsed;
@ -153,6 +153,4 @@ class ParsedLogIterator implements \Iterator
{
return ! is_null($this->value);
}
}

View file

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

View file

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

View file

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