-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclover-tail
More file actions
executable file
·276 lines (237 loc) · 6.55 KB
/
Copy pathclover-tail
File metadata and controls
executable file
·276 lines (237 loc) · 6.55 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env php
<?php
// duration for usleep
define('ONE_TENTH_SECOND', 100000);
// number of lines to output on tail
define('TAIL_LINE_COUNT', 10);
/**
* Get file and separator from config
*/
$config = parseConfig();
$file = $config['file'] ?? null;
$separator = $config['separator'] ?? null;
/**
* Get optionsl methods to filter by
*/
$methods = greppableMethods($argv);
/**
* Display help if -h or --help is a parameter
*/
handleHelp($methods);
/**
* Entry point
*/
tailf($file, $separator, $methods);
/**
* Parse the cloverlogger config file and return array keyed with
* 'file' and 'separator'.
*
* @return array<string,string>
*/
function parseConfig(): array {
$configPath = __DIR__.'/../../../../cloverlogger.conf';
if(!file_exists($configPath)) {
die("File cloverlogger.conf does not exist. did you run vendor/bin/cloverlogger-publish-config?");
}
if(!is_readable($configPath)) {
die("File cloverlogger.conf is not readable");
}
$config = @parse_ini_file($configPath);
$file = $config['FILE'] ?? null;
$separator = $config['SEPARATOR'] ?? null;
if(!$file) {
die("FILE field is not set in cloverlogger.conf");
}
if(!$separator) {
die("SEPARATOR field is not set in cloverlogger.conf");
}
return [
'file' => $file,
'separator' => $separator,
];
}
/**
* Returns all command line args as list of methods to grep
*
* @param array<string> $argv
* @return array<string>
*/
function greppableMethods($argv)
{
array_shift($argv);
return array_map(fn($a) => trim($a), $argv);
}
/**
* Outputs the help document if any of the arguments parsed into $methods
* is -h or --help. Dies on help output.
*
* @param array<mixed> $methods
* @return void
*/
function handleHelp($methods) {
if(count(array_intersect(['-h', '--help'], $methods))) {
print <<<TEXT
Usage: vendor/bin/clover-tail [METHODS]
Outputs the tail of the configured clover-logger log file, optionally filtering by
the list of methods provided as arguments. Outputs appended data as the file
grows.
Arguments:
-h, --help Print this message
Examples:
vendor/bin/clover-tail myMethod
vendor/bin/clover-tail myMethod otherMethod
vendor/bin/clover-tail
TEXT;
die();
}
}
/**
* Opens a file, returns the pointer resource
*
* @param string $file Path to file
* @return mixed The file pointer resource
*/
function open(string $file): mixed {
if(!file_exists($file)) {
die("File $file does not exist");
}
if(!is_readable($file)) {
die("File $file is not readable");
}
$fp = fopen($file, "r");
return $fp;
}
/**
* Updates a file pointer $fp to be n bytes from the end of the file
* so that outputting to eof prints the last $lineCount lines.
*
* @param mixed $fp The file pointer resource
* @param int $lineCount How many lines back from the end of file
* @param string $separator
* @param array<string> $methods
* @return mixed The file pointer resource
*/
function windToTailStart(mixed $fp, int $lineCount, $separator = null, $methods = []): mixed {
fseek($fp, 0, SEEK_END);
$position = 0;
$lineCounter = 0;
do {
// got to start of file before getting $lineCount lines
if(ftell($fp) == 0) {
return $fp;
}
fseek($fp, $position--, SEEK_END);
if(fgetc($fp) == PHP_EOL) {
if(feof($fp)) {
return $fp;
}
$bookmark = ftell($fp);
if($bookmark === false) {
return $fp;
}
$line = fgets($fp);
fseek($fp, $bookmark, SEEK_SET);
if($line !== false) {
if(canPrint($line, $separator, $methods)) {
$lineCounter++;
}
}
}
}
while($lineCounter < $lineCount);
return $fp;
}
/**
* Returns if a line can be counted for output or output, ie if it
* contains one of the grepabble methods or if there are no grepabble
* methods.
*
* @param string $line
* @param string $separator
* @param array<mixed> $methods
* @return bool
*/
function canPrint(string $line, string $separator, array $methods): bool {
/**
* Extract the 'method' field from the log line
*
* @param string $line
* @param string $separator
* @return ?string
*/
$getMethod =fn(string $line, string $separator) => $separator ? explode($separator, $line)[1] ?? null : null;
if(count($methods) == 0) {
return true;
}
else {
if(in_array($getMethod($line, $separator), $methods)) {
return true;
}
}
return false;
}
/**
* Outputs all optionally-filtered lines of file from the position of the
* file pointer $fp to the end of file.
*
* @param mixed $fp The file pointer resource
* @param string $separator
* @param array<mixed> $methods
* @return void
*/
function output(mixed $fp, $separator = null, $methods = []) {
while(!feof($fp)) {
$line = fgets($fp);
if($line !== false) {
if(canPrint($line, $separator, $methods)) {
print $line;
}
}
}
}
/**
* Outputs the last $lineCount lines from file $file.
*
* @param string $file Path to file
* @param string $separator
* @param array<string> $methods
* @param int $lineCount
* @return void
*/
function tail(string $file, string $separator, array $methods, int $lineCount) {
$fp = open($file);
$fp = windToTailStart($fp, $lineCount, $separator, $methods);
output($fp, $separator, $methods);
fclose($fp);
}
/**
* Outputs the last $lineCount lines from file $file
* then waits for the file to update and outputs the
* new lines. ie. `tail -f`
*
* @param string $file Path to file
* @param string $separator
* @param array<string> $methods
* @param int $lineCount
* @return void
*/
function tailf(string $file, string $separator, array $methods, int $lineCount = TAIL_LINE_COUNT) {
// output tail content
tail($file, $separator, $methods, $lineCount);
// open file at the end
$fp = open($file);
fseek($fp, 0, SEEK_END);
// wait for new file content and output
while(true) { /** @phpstan-ignore while.alwaysTrue */
$tell = ftell($fp);
if($tell === false) {
break;
}
usleep(ONE_TENTH_SECOND);
fseek($fp, 0, SEEK_END);
if($tell != ftell($fp)) {
fseek($fp, $tell, SEEK_SET);
output($fp, $separator, $methods);
}
}
}