diff --git a/mobly/controllers/android_device_lib/logcat_processor.py b/mobly/controllers/android_device_lib/logcat_processor.py index 7ce29529..7f46ad91 100644 --- a/mobly/controllers/android_device_lib/logcat_processor.py +++ b/mobly/controllers/android_device_lib/logcat_processor.py @@ -487,12 +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) 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()