-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathWatch.php
More file actions
159 lines (136 loc) Β· 4.69 KB
/
Watch.php
File metadata and controls
159 lines (136 loc) Β· 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\LogReader\Command;
use OC\Core\Command\Base;
use OC\Core\Command\InterruptedException;
use OCA\LogReader\Log\FilterQuery;
use OCA\LogReader\Log\Formatter;
use OCA\LogReader\Log\LogIteratorFactory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
class Watch extends Base {
public const LEVELS = ['Debug', 'Info', 'Warning', 'Error', 'Fatal'];
public const ALL_LEVELS = '11111';
private $formatter;
private $logIteratorFactory;
public function __construct(Formatter $formatter, LogIteratorFactory $logIteratorFactory) {
parent::__construct();
$this->formatter = $formatter;
$this->logIteratorFactory = $logIteratorFactory;
}
protected function configure() {
$this
->setName('log:watch')
->setDescription('Watch the nextcloud logfile')
->addOption('raw', 'r', InputOption::VALUE_NONE, 'Output raw log json instead of formatted log item')
->addOption('filter', null, InputOption::VALUE_REQUIRED, 'Filter log items according to the provided query');
parent::configure();
}
private function getLastLogId() {
$logIterator = $this->logIteratorFactory->getLogIterator(self::ALL_LEVELS);
$logIterator->next();
if ($logIterator->current() !== null) {
return $logIterator->current()['reqId'];
}
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$raw = $input->getOption('raw');
$filter = new FilterQuery((string)$input->getOption('filter'));
return $this->watch($raw, $filter, $output);
}
public function watch(bool $raw, FilterQuery $filter, OutputInterface $output): int {
$terminal = new Terminal();
$totalWidth = $terminal->getWidth();
// 8 level, 18 for app, 26 for time, 6 for formatting
$messageWidth = $totalWidth - 8 - 18 - 26 - 6;
$lastId = $this->getLastLogId();
while (true) {
usleep(100 * 1000);
try {
$this->abortIfInterrupted();
} catch (InterruptedException $e) {
break;
}
$id = $this->getLastLogId();
if ($id !== $lastId) {
$iterator = $this->logIteratorFactory->getLogIterator(self::ALL_LEVELS);
$iterator->next();
$lines = [];
while ($iterator->valid() && count($lines) < 10) {
$line = $iterator->current();
if ($line['reqId'] === $lastId) {
break;
}
if (!is_null($line) && $filter->matches($line)) {
$lines[] = $line;
}
$iterator->next();
}
array_reverse($lines);
foreach ($lines as $line) {
if ($raw) {
$output->writeln(json_encode($line));
} else {
$this->printItem($line, $output, $messageWidth);
$output->writeln("");
}
}
$lastId = $id;
}
}
return 0;
}
private function printItem(array $logItem, OutputInterface $output, int $messageWidth) {
$widths = [8, 18, $messageWidth, 26];
$parts = [
self::LEVELS[$logItem['level']],
wordwrap($logItem['app'], 18),
$this->formatter->formatMessage($logItem, $messageWidth),
$logItem['time'],
];
$partLines = array_map(function ($part) {
return explode("\n", $part);
}, $parts);
$lineCount = array_reduce($partLines, function (int $count, array $lines) {
return max($count, count($lines));
}, 0);
$partLines = array_map(function (array $lines) use ($lineCount) {
return array_pad($lines, $lineCount, '');
}, $partLines);
$partLines = array_map(function (array $lines, int $width) {
return array_map(function (string $line) use ($width) {
return str_pad($line, $width);
}, $lines);
}, $partLines, $widths);
$lines = array_map(function (int $lineNumber) use ($partLines) {
$partsForLine = array_map(function (array $lines) use ($lineNumber) {
return $lines[$lineNumber];
}, $partLines);
return implode(' ', $partsForLine);
}, range(0, $lineCount - 1));
foreach ($lines as $line) {
$output->writeln(' ' . $line);
}
}
}