Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.37% |
38 / 43 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
LogrotateLogsChannel | |
88.37% |
38 / 43 |
|
0.00% |
0 / 3 |
8.10 | |
0.00% |
0 / 1 |
getLogFileForIndex | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getIndexForFilePath | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getLineLocationForDateTime | |
89.66% |
26 / 29 |
|
0.00% |
0 / 1 |
4.02 | |||
getLogFileBefore | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
getLogFileAfter | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 |
1 | <?php |
2 | |
3 | namespace Olz\Apps\Logs\Utils; |
4 | |
5 | abstract class LogrotateLogsChannel extends BaseLogsChannel { |
6 | abstract protected function getLogFileForIndex(int $index): LogFileInterface; |
7 | |
8 | abstract protected function getIndexForFilePath(string $file_path): int; |
9 | |
10 | protected function getLineLocationForDateTime( |
11 | \DateTime $date_time, |
12 | ): LineLocation { |
13 | $iso_date_time = $date_time->format('Y-m-d H:i:s'); |
14 | $index = -1; |
15 | $continue = true; |
16 | while ($continue) { |
17 | try { |
18 | $log_file = $this->getLogFileForIndex($index); |
19 | $file_index = $this->getOrCreateIndex($log_file); |
20 | if ($iso_date_time >= $file_index['start_date']) { |
21 | $continue = false; |
22 | } else { |
23 | $index++; |
24 | } |
25 | } catch (\Exception $exc) { |
26 | $continue = false; |
27 | throw $exc; |
28 | } |
29 | } |
30 | $log_file = $this->getLogFileForIndex($index); |
31 | $file_index = $this->getOrCreateIndex($log_file); |
32 | $number_of_lines = count($file_index['lines']); |
33 | $fp = $log_file->open('r'); |
34 | |
35 | [$line_number, $cmp] = $this->generalUtils()->binarySearch( |
36 | function ($line_number) use ($log_file, $fp, $file_index, $date_time) { |
37 | $index = $file_index['lines'][$line_number]; |
38 | $log_file->seek($fp, $index); |
39 | $line = $log_file->gets($fp); |
40 | $date_time_at_index = $this->parseDateTimeOfLine($line ?? ''); |
41 | return $date_time <=> $date_time_at_index; |
42 | }, |
43 | 0, |
44 | $number_of_lines - 1, |
45 | ); |
46 | |
47 | $log_file->close($fp); |
48 | return new LineLocation($log_file, $line_number, $cmp); |
49 | } |
50 | |
51 | protected function getLogFileBefore(LogFileInterface $log_file): LogFileInterface { |
52 | $path = $log_file->getPath(); |
53 | $index = $this->getIndexForFilePath($path); |
54 | $index_before = $index + 1; |
55 | $new_log_file = $this->getLogFileForIndex($index_before); |
56 | if (!$new_log_file->exists()) { |
57 | throw new \Exception("No such file: {$new_log_file->getPath()}"); |
58 | } |
59 | return $new_log_file; |
60 | } |
61 | |
62 | protected function getLogFileAfter(LogFileInterface $log_file): LogFileInterface { |
63 | $path = $log_file->getPath(); |
64 | $index = $this->getIndexForFilePath($path); |
65 | $index_after = $index - 1; |
66 | $new_log_file = $this->getLogFileForIndex($index_after); |
67 | if (!$new_log_file->exists()) { |
68 | throw new \Exception("No such file: {$new_log_file->getPath()}"); |
69 | } |
70 | return $new_log_file; |
71 | } |
72 | } |