Av1an/av1an/logger.py

49 lines
1.1 KiB
Python
Raw Normal View History

2020-06-27 19:21:00 +00:00
#!/bin/env python
2020-07-06 15:53:25 +00:00
import sys
2020-06-22 16:30:52 +00:00
import time
from pathlib import Path
2020-06-22 20:11:07 +00:00
2020-08-19 18:07:31 +00:00
class Logger:
2020-06-22 16:30:52 +00:00
def __init__(self):
self.set_file = False
self.buffer = ''
2020-06-22 16:30:52 +00:00
def set_path(self, file):
self.set_file = Path(file)
def log(self, info):
"""Default logging function, write to file."""
if self.set_file and self.buffer:
with open(self.set_file, 'a') as logf:
logf.write(self.buffer)
self.buffer = None
2020-06-22 16:30:52 +00:00
if self.set_file:
2020-08-19 07:13:03 +00:00
with open(self.set_file, 'a') as logf:
logf.write(time.strftime('%X') + ' ' + info)
else:
self.buffer += time.strftime('%X') + ' ' + info
2020-06-22 20:11:07 +00:00
2020-06-22 16:30:52 +00:00
# Creating logger
2020-08-19 06:41:58 +00:00
logger = Logger()
log_file = logger.set_path
log = logger.log
2020-06-22 16:30:52 +00:00
2020-06-30 22:06:27 +00:00
def set_log(log_path: Path, temp):
2020-06-22 20:11:07 +00:00
"""Setting logging file location"""
2020-12-27 05:49:31 +00:00
2020-06-22 20:11:07 +00:00
if log_path:
log_path = Path(log_path)
if log_path.suffix == '':
log_path = log_path.with_suffix('.log')
2020-06-30 22:06:27 +00:00
log_file(log_path)
2020-12-27 05:49:31 +00:00
2020-06-22 20:11:07 +00:00
else:
2020-06-30 22:06:27 +00:00
log_file(temp / 'log.log')
2020-06-22 20:11:07 +00:00
2020-06-27 19:35:42 +00:00
log(f"\nAv1an Started\nCommand:\n{' '.join(sys.argv)}\n")