-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.py
More file actions
44 lines (36 loc) · 1.39 KB
/
Copy pathLogger.py
File metadata and controls
44 lines (36 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
from datetime import datetime
class Logger:
def __init__(self, log_file: str):
self.log_history = []
self.name = log_file
if os.path.exists(log_file):
#print("Loading history from log %s"%(log_file))
with open(log_file, "r") as rfp:
self.log_history=rfp.readlines()
self.log_fp = open(log_file, "a+")
self.log_history = [i.replace("\n", "").replace(" ", "") for i in self.log_history]
loglen = len(self.log_history)
#for i in range(max(loglen - 3, 0), loglen):
# print(self.log_history[i])
else:
print("No history, opening new log")
self.log_fp = open(log_file, "w+")
def get_last(self):
if len(self.log_history) > 1:
return self.log_history[-1]
else:
return None
def write(self, out: str) -> None:
date_string = f'{datetime.now():%Y-%m-%d/%H:%M:%S%z}, '
entry = date_string+out
self.log_fp.write(entry+"\n")
self.log_history.append(entry)
self.log_fp.flush()
def write_header(self, entry: str) -> None:
# don't append history this is a new log
self.log_fp.write(entry + "\n")
self.log_fp.flush()
def get(self, idx: int) -> str:
if idx < len(self.log_history):
return self.log_history[idx]