From 8371ec9dc86657bf0cbfaa08443ad52f215cce24 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 24 Sep 2026 03:43:21 -0300 Subject: [PATCH 1/2] Fix byte offsets of lines returned by LogcatProcessor.tail For every block except the one at the start of the file, the offset of the first line did not count the newline after the partial remainder, so all lines in that block were placed one byte too early. --- .../android_device_lib/logcat_processor.py | 2 ++ .../android_device_lib/services/logcat_test.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/mobly/controllers/android_device_lib/logcat_processor.py b/mobly/controllers/android_device_lib/logcat_processor.py index 7ce29529..ee9e9d07 100644 --- a/mobly/controllers/android_device_lib/logcat_processor.py +++ b/mobly/controllers/android_device_lib/logcat_processor.py @@ -493,6 +493,8 @@ def tail( # Calculate offsets and parse lines in reverse order within this block current_offset = remaining + len(remainder) + if remaining > 0: + current_offset += 1 # count \n byte after remainder block_lines: list[tuple[int, LogLine]] = [] for line_bytes in lines_chunk: line_offset = current_offset diff --git a/tests/mobly/controllers/android_device_lib/services/logcat_test.py b/tests/mobly/controllers/android_device_lib/services/logcat_test.py index 883324c1..ae49cb1d 100755 --- a/tests/mobly/controllers/android_device_lib/services/logcat_test.py +++ b/tests/mobly/controllers/android_device_lib/services/logcat_test.py @@ -901,6 +901,21 @@ def test_tail_recent_logs(self): self.assertEqual(recent_logs[-1].tag, 'BtGatt') self.assertEqual(recent_logs[-1].level, 'F') + def test_tail_position_in_large_file(self): + # Make the file larger than the block size tail() reads at a time. + self._append_log( + '08-09 22:00:05.000 1000 1030 I Filler: padding\n' * 2000 + ) + start = self.logcat_service.now() + self._append_log( + '08-09 22:00:06.000 1000 1030 I WifiService: Disconnected from' + ' wlan0\n' + ) + + last_line = self.logcat_service.tail(num_lines=1)[0] + self.assertEqual(last_line.message, 'Disconnected from wlan0') + self.assertTrue(last_line.position > start) + def test_now_and_bounded_query(self): # Take position marker before triggering an action start = self.logcat_service.now() From 2d63b3937b4aa9dcbdc0ac8e435ce0081ced20b5 Mon Sep 17 00:00:00 2001 From: kwy404 Date: Thu, 24 Sep 2026 05:25:58 -0300 Subject: [PATCH 2/2] Compute the block offset inside the remainder branches --- mobly/controllers/android_device_lib/logcat_processor.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mobly/controllers/android_device_lib/logcat_processor.py b/mobly/controllers/android_device_lib/logcat_processor.py index ee9e9d07..7f46ad91 100644 --- a/mobly/controllers/android_device_lib/logcat_processor.py +++ b/mobly/controllers/android_device_lib/logcat_processor.py @@ -487,14 +487,13 @@ def tail( if remaining > 0: remainder = split[0] lines_chunk = split[1:] + current_offset = remaining + len(remainder) + 1 else: remainder = b'' lines_chunk = split + current_offset = 0 # Calculate offsets and parse lines in reverse order within this block - current_offset = remaining + len(remainder) - if remaining > 0: - current_offset += 1 # count \n byte after remainder block_lines: list[tuple[int, LogLine]] = [] for line_bytes in lines_chunk: line_offset = current_offset