Av1an/av1an/logger.py

52 lines
1.2 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
2021-03-28 04:24:49 +00:00
# Todo: Add self testing on startup
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)
2021-02-21 04:44:36 +00:00
def log(self, *info):
2020-06-22 16:30:52 +00:00
"""Default logging function, write to file."""
2021-02-21 04:44:36 +00:00
for i in info:
if self.set_file and self.buffer:
with open(self.set_file, 'a') as logf:
logf.write(self.buffer)
self.buffer = None
2021-02-21 04:44:36 +00:00
if self.set_file:
with open(self.set_file, 'a') as logf:
logf.write(f'[{time.strftime("%X")}] {i}\n')
else:
self.buffer += f'[{time.strftime("%X")}] {i}\n'
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
2021-02-21 04:44:36 +00:00
log(f"Av1an Started")
log(f"Command:")
log(f"{' '.join(sys.argv)}")