forked from ArknightsAutoHelper/ArknightsAutoHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
richlog.py
30 lines (23 loc) · 831 Bytes
/
richlog.py
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
import os
from base64 import b64encode
from functools import lru_cache
from io import BytesIO
import config
class RichLogger:
def __init__(self, file, overwrite=False):
self.f = open(file, 'wb' if overwrite else 'ab')
if self.f.tell() == 0:
self.loghtml('<html><head><meta charset="utf-8"></head><body>')
def logimage(self, image):
bio = BytesIO()
image.save(bio, format='PNG')
imgb64 = b64encode(bio.getvalue())
self.f.write(b'<p><img src="data:image/png;base64,%s" /></p>\n' % imgb64)
def logtext(self, text):
self.loghtml('<pre>%s</pre>\n' % text)
def loghtml(self, html):
self.f.write(html.encode())
@lru_cache(maxsize=None)
def get_logger(file):
logger = RichLogger(os.path.join(config.logs, file), True)
return logger