Created
November 7, 2016 12:09
-
-
Save stgleb/099e048cc8052e90fac439203bd5ffe6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SEEK_BEG = 0 | |
SEEK_SET = 1 | |
SEEK_END = 2 | |
BLOCK_SIZE = 10 | |
FILE_NAME = "console.log" | |
def get_console_output(length, offset, origin=SEEK_END): | |
if origin == SEEK_END: | |
offset *= -1 | |
with open(FILE_NAME, "rb") as f: | |
offset -= length | |
f.seek(offset, origin) | |
bytes_read = f.read(length) | |
pos = f.tell() | |
# Return bytes and position of last unread byte from the begin. | |
# Subtract length to pos since we will read in opposite direction | |
return bytes_read, pos - length | |
if __name__ == "__main__": | |
data = [] | |
bytes_array, pos = get_console_output(BLOCK_SIZE, 20, SEEK_END) | |
data.append(bytes_array) | |
bytes_array, pos = get_console_output(BLOCK_SIZE, pos, origin=SEEK_BEG) | |
data.append(bytes_array) | |
bytes_array, pos = get_console_output(BLOCK_SIZE, pos, origin=SEEK_BEG) | |
data.append(bytes_array) | |
print("".join(data[::-1])) |
The issue is the content of the file which will be under constant change when log rotation is enabled:
1st 2nd 3rd
read read read
------------------------
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 10
9 10 11
10 11 12
------------------------
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above returns:
Which is not the expected data.