Skip to content

Commit

Permalink
add truncate() method that discards excess log entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jon committed Aug 4, 2022
1 parent 1a0fe28 commit 29909a5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions phew/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@ def log_file(file):
global _log_file
_log_file = file

# truncates the log file down to a target size while maintaining
# clean line breaks
def truncate(target):
# get the current size of the log file
size = 0
try:
size = os.stat(_log_file)[6]
except OSError:
return
# calculate how many bytes we're aiming to discard
discard = size - target

if discard < 0:
return

with open(_log_file, "rb") as infile:
# skip a bunch of the input file until we've discarded
# at least enough
while discard > 0:
chunk = infile.read(1024)
discard -= len(chunk)

with open(_log_file + ".tmp", "wb") as outfile:
# we have a partial chunk to write, try to find a line
# break nearby to split it on
break_position = chunk.find(b"\n", -discard)
if break_position == -1:
break_position = chunk.rfind(b"\n", -discard)
if break_position == -1:
break_position = 0
outfile.write(chunk[break_position + 1:])

# now copy the rest of the file
while True:
chunk = infile.read(1024)
if not chunk:
break
outfile.write(chunk)

# delete the old file and replace with the new
os.remove(_log_file)
os.rename(_log_file + ".tmp", _log_file)


def log(level, text):
datetime = datetime_string()
log_entry = "{0} [{1:8} /{2:>8}] {3}".format(datetime, level, gc.mem_free(), text)
Expand Down

0 comments on commit 29909a5

Please sign in to comment.