diff --git a/cog_notificator.py b/cog_notificator.py index 6a5762a..d2f3155 100644 --- a/cog_notificator.py +++ b/cog_notificator.py @@ -369,7 +369,7 @@ class Notificator(commands.Cog): try: await self.send_new_alert(current_alert, new_districts) - except BaseException as e: + except Exception as e: self.log.error(f'Could not send message!\nError info: {e.__str__()}') self.active_districts = data @@ -405,6 +405,7 @@ class Notificator(commands.Cog): view.add_item(button) return view + @errlogging.async_errlog async def send_new_alert(self, alert_data: dict, new_districts: list[str]): """ Push an alert to all registered channels @@ -427,11 +428,15 @@ class Notificator(commands.Cog): asyncio.create_task(self.send_alerts_to_channels(embed_ls)) + @errlogging.async_errlog async def send_alerts_to_channels(self, embed_ls): """ Send the embeds in embed_ls to all channels :param embed_ls: List of AlertEmbeds to send to channels """ + + raise Exception('sdgh') + for channel_tup in self.db.get_all_channels(): channel = Channel.from_tuple(channel_tup) if channel.server_id is not None: @@ -456,7 +461,7 @@ class Notificator(commands.Cog): try: await dc_ch.send(embed=emb.embed, view=self.hfc_button_view()) await asyncio.sleep(0.02) - except BaseException as e: + except Exception as e: self.log.warning(f'Failed to send alert in channel id={channel.district_id}:\n' f'{e}') diff --git a/errlogging.py b/errlogging.py index 8986db6..10e2d2f 100644 --- a/errlogging.py +++ b/errlogging.py @@ -1,24 +1,27 @@ import datetime +import logging import os import sys import traceback -def generate_errlog_folder(): - botdata_path = os.path.join(os.path.realpath(__file__), '..', 'botdata') - if not os.path.isdir(botdata_path): - os.mkdir(botdata_path) +class ErrLogger: + def __init__(self, handler: logging.Handler=None): + if handler is None: + handler = logging.StreamHandler() - botdata_backup_path = os.path.join(botdata_path, 'backups') - if not os.path.isdir(botdata_backup_path): - os.mkdir(botdata_backup_path) -def new_errlog(err: BaseException): - e: BaseException = err - time = datetime.datetime.now() - path = os.path.join(os.path.realpath(__file__), '..', 'botdata', 'backups', f'ERRLOG_{time.strftime("%Y-%m-%d_%H-%M-%S")}.txt') - tb_str = '\n'.join(traceback.format_tb(e.__traceback__)) + self.log = logging.Logger('ErrLogger') + self.log.addHandler(handler) - data = f"""An error has occurred! Don't worry, I saved an automatic log for ya :) + 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() + path = os.path.join(os.path.realpath(__file__), '..', 'botdata', 'errlogs', + 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 :) ---------------------------------------------------------------------- Rough DateTime: {time.strftime("%Y-%m-%d %H:%M:%S")} @@ -37,21 +40,52 @@ Full Traceback: {tb_str} """ - with open(path, 'w') as f: - f.write(data) + 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(): + botdata_path = os.path.join(os.path.realpath(__file__), '..', 'botdata') + if not os.path.isdir(botdata_path): + os.mkdir(botdata_path) + + botdata_backup_path = os.path.join(botdata_path, 'errlogs') + if not os.path.isdir(botdata_backup_path): + os.mkdir(botdata_backup_path) + + +def new_errlog(err: BaseException): + ErrLogger().new_errlog(err) def errlog(func): - def wrapper(*args, **kwargs): - try: - res = func(*args, **kwargs) - except Exception as e: - new_errlog(e) - else: - print('sdgg') - return res + return ErrLogger().errlog(func) - return wrapper + +def async_errlog(func): + return ErrLogger().async_errlog(func)