2023-10-29 15:56:52 +02:00
|
|
|
import datetime
|
2023-10-30 13:30:36 +02:00
|
|
|
import logging
|
2023-10-29 15:56:52 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2023-10-30 15:58:20 +02:00
|
|
|
import __main__
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
main_dir = Path(__main__.__file__).parent
|
|
|
|
botdata_dir = main_dir.joinpath('botdata')
|
|
|
|
errlog_dir = botdata_dir.joinpath('errlogs')
|
|
|
|
|
2023-10-29 15:56:52 +02:00
|
|
|
|
2023-10-30 13:30:36 +02:00
|
|
|
class ErrLogger:
|
|
|
|
def __init__(self, handler: logging.Handler=None):
|
|
|
|
if handler is None:
|
|
|
|
handler = logging.StreamHandler()
|
2023-10-29 15:56:52 +02:00
|
|
|
|
2023-10-30 13:30:36 +02:00
|
|
|
self.log = logging.Logger('ErrLogger')
|
|
|
|
self.log.addHandler(handler)
|
2023-10-29 15:56:52 +02:00
|
|
|
|
2023-10-30 13:30:36 +02:00
|
|
|
def new_errlog(self, err: BaseException):
|
|
|
|
self.log.error('An error has occurred! Check the latest ERRLOG file for more info.')
|
|
|
|
e: BaseException = err
|
|
|
|
time = datetime.datetime.now()
|
2023-10-30 15:58:20 +02:00
|
|
|
path = os.path.join(errlog_dir,
|
2023-10-30 13:30:36 +02:00
|
|
|
f'ERRLOG_{time.strftime("%Y-%m-%d_%H-%M-%S")}.txt')
|
|
|
|
tb_str = '\n'.join(traceback.format_tb(e.__traceback__))
|
|
|
|
|
|
|
|
data = f"""An error has occurred! Don't worry, I saved an automatic log for ya :)
|
2023-10-29 15:56:52 +02:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
Rough DateTime: {time.strftime("%Y-%m-%d %H:%M:%S")}
|
|
|
|
|
|
|
|
Error Info:
|
|
|
|
-----------
|
|
|
|
{type(e).__name__}: {e}
|
|
|
|
|
|
|
|
Context:
|
|
|
|
{e.__context__}
|
|
|
|
|
|
|
|
Caused by:
|
|
|
|
{e.__cause__}
|
|
|
|
|
|
|
|
Full Traceback:
|
|
|
|
---------------
|
|
|
|
{tb_str}
|
|
|
|
"""
|
|
|
|
|
2023-10-30 13:30:36 +02:00
|
|
|
with open(path, 'w') as f:
|
|
|
|
f.write(data)
|
|
|
|
|
|
|
|
def errlog(self, func):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
res = func(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
self.new_errlog(e)
|
|
|
|
else:
|
|
|
|
return res
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
def async_errlog(self, func):
|
|
|
|
async def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
res = await func(*args, **kwargs)
|
|
|
|
except Exception as e:
|
|
|
|
self.new_errlog(e)
|
|
|
|
else:
|
|
|
|
return res
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def generate_errlog_folder():
|
2023-10-30 15:58:20 +02:00
|
|
|
if not botdata_dir.is_dir():
|
|
|
|
botdata_dir.mkdir()
|
2023-10-30 13:30:36 +02:00
|
|
|
|
2023-10-30 15:58:20 +02:00
|
|
|
if not errlog_dir.is_dir():
|
|
|
|
errlog_dir.mkdir()
|
2023-10-30 13:30:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def new_errlog(err: BaseException):
|
|
|
|
ErrLogger().new_errlog(err)
|
2023-10-29 15:56:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def errlog(func):
|
2023-10-30 13:30:36 +02:00
|
|
|
return ErrLogger().errlog(func)
|
|
|
|
|
|
|
|
|
|
|
|
def async_errlog(func):
|
|
|
|
return ErrLogger().async_errlog(func)
|
2023-10-29 15:56:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|