logging: reverse_log_reader() - fix off by one error due to line ending at the end of the file

Usually log lines start with a line ending, which means the first hit is always an empty line with reading things backwards.
This empty line has no relevance, but only indicates we're at the end of the file.

This commits stores the file starting position in all cases and ignores the output when we trying to yield the end of the file.
This commit is contained in:
Ad Schellevis 2025-04-10 09:08:23 +02:00
parent d0b1c8d369
commit 7446f8cbbf

View File

@ -40,9 +40,8 @@ def reverse_log_reader(filename, block_size=81920, start_pos=None):
if start_pos is None:
input_stream.seek(0, os.SEEK_END)
file_byte_start = input_stream.tell()
else:
file_byte_start = start_pos
start_pos = input_stream.tell()
file_byte_start = start_pos
data = ''
while file_byte_start > 0:
@ -64,7 +63,8 @@ def reverse_log_reader(filename, block_size=81920, start_pos=None):
line = data[bol:eol]
eol = bol
bol = data.rfind('\n', 0, eol)
yield {'line': line.strip().strip('\u0000'), 'pos': line_end}
if line_end != start_pos:
yield {'line': line.strip().strip('\u0000'), 'pos': line_end}
data = data[:eol] if bol == -1 else ''