Rotate csv annually, put it in log directory with backup text log

This commit is contained in:
mlncn 2021-05-02 16:57:06 -04:00
parent 308a284fce
commit 926af1ed48
2 changed files with 19 additions and 8 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
log/*.log
timelog.csv
log/*.csv

View file

@ -53,12 +53,7 @@ def quit_prompt():
def log_step(text, utc_start, end_section=False):
""" Write detailed log of all events
"""
eastern = timezone('US/Eastern')
start = utc_start.astimezone(eastern)
timelog_path = 'log/' + str(start.year) + '-' \
+ format(start.month, '02') + '-' \
+ format(start.day, '02') + '.log'
timelog_file = Path(timelog_path)
timelog_file = Path(timelog_path(ext='log', daily=True))
timelog_file.touch(exist_ok=True)
with timelog_file.open('a') as timelog:
timelog.write(text + '\n')
@ -71,7 +66,7 @@ def record_task(whatnext, whatdid, start, end=None):
"""
if end is None:
end = datetime.now(pytz.utc)
with open('log/timelog.csv', 'a', newline='') as csvfile:
with open(prepare_file(), 'a', newline='') as csvfile:
# TODO make first line started, recorded, description, intention
timewriter = csv.writer(csvfile)
timewriter.writerow([start, end, whatdid, whatnext])
@ -89,6 +84,22 @@ def countdown_to(until):
print('\r', str_minutes(to_go), sep='', end='')
time.sleep(1)
def prepare_file(ext='csv', daily=False, utc_start):
# Ensure log directory exists.
if not os.path.exists('log'):
os.makedirs('log')
# We consider 3am the switchover to a new day, so we skip extra math
# and use Pacific time to get three hours later than our Eastern time.
logpath_tz = timezone('US/Pacific')
logpath_date = utc_start.astimezone(logpath_tz)
timelog_path = 'log/' + str(logpath_date.year);
if (daily):
timelog_path += '-' \
+ format(logpath_date.month, '02') + '-' \
+ format(logpath_date.day, '02') + '.log'
timelog_path += '.' + ext
return timelog_path
def main():
whatdid = ''
while True: