From 7446f8cbbffc1958abe11331d9aa8ffdff9c1489 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Thu, 10 Apr 2025 09:08:23 +0200 Subject: [PATCH] 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. --- src/opnsense/site-python/log_helper.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/opnsense/site-python/log_helper.py b/src/opnsense/site-python/log_helper.py index 7c6cff76d..a5e676baa 100644 --- a/src/opnsense/site-python/log_helper.py +++ b/src/opnsense/site-python/log_helper.py @@ -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 ''