-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
194 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from sentry_sdk.integrations.logging import LoggingIntegration | ||
from PyQt6.QtWidgets import QApplication | ||
from .constants import __version__ | ||
|
||
import sys | ||
import logging | ||
import platform | ||
import darkdetect | ||
import sentry_sdk | ||
|
@@ -14,13 +16,20 @@ | |
|
||
sentry_sdk.set_context('os', sys_info) | ||
|
||
def before_breadcrumbs(crumb, hint): | ||
if crumb['message'] == 'The following error occured:': | ||
return None | ||
return crumb | ||
|
||
sentry_sdk.init( | ||
# NOTE: This URL is the testing URL, don't forget to change to production URL | ||
'https://[email protected]/5867522', | ||
traces_sample_rate=1.0, | ||
environment='production' if getattr(sys, 'frozen', False) else 'development', | ||
release=__version__, | ||
default_integrations=False | ||
integrations=(LoggingIntegration(logging.DEBUG, None),), | ||
before_breadcrumb=before_breadcrumbs, | ||
default_integrations=False, | ||
traces_sample_rate=1.0, | ||
release=__version__ | ||
) | ||
|
||
from .gui import BeskarWindow | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,48 @@ | ||
from sentry_sdk.utils import event_from_exception | ||
from .settings import logging_dir | ||
from .popups import ErrorPopup | ||
from .logs import setUpLogger | ||
from sentry_sdk import Hub | ||
from . import app, window | ||
|
||
import sys | ||
import logging | ||
import inspect | ||
import traceback | ||
|
||
def handle_exception(exc_type, exc_value, trace): | ||
error_message = ''.join(traceback.format_exception(exc_type, exc_value, trace)) | ||
exc_tuple = (exc_type, exc_value, trace) | ||
error_message = ''.join(traceback.format_exception(*exc_tuple)) | ||
|
||
popup = ErrorPopup(window, error_message) | ||
popup.exec() | ||
|
||
# Get logger for the file that is responsible for error | ||
logger = logging.getLogger(inspect.getmodule(inspect.stack()[1].frame).__name__) | ||
logger.critical('The following error occured:', exc_info=exc_tuple) | ||
|
||
if popup.sending: | ||
hub = Hub.current | ||
if hub.client is not None: | ||
hub.capture_event(*event_from_exception((exc_type, exc_value, trace))) | ||
hub.capture_event(*event_from_exception(exc_tuple)) | ||
hub.flush() | ||
|
||
sys.exit(error_message) | ||
sys.exit(1) | ||
|
||
def main(): | ||
window.show() | ||
sys.exit(app.exec()) | ||
|
||
if __name__ == '__main__': | ||
for logger_name in map( | ||
lambda name: f'beskar.{name}', | ||
('gui', 'pages', 'popups', 'settings', 'utils') | ||
): | ||
setUpLogger( | ||
logger_name, | ||
'%(levelname)s | %(name)s: [%(funcName)s()] %(message)s', | ||
logging_dir | ||
) | ||
|
||
sys.excepthook = handle_exception | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import logging, logging.handlers | ||
import datetime | ||
import pathlib | ||
|
||
# NOTE: Adapted from: https://github.com/Ahsoka/bdaybot/blob/master/bdaybot/logs.py | ||
|
||
class PrettyFormatter(logging.Formatter): | ||
def __init__(self, *args, style='%', **kwargs): | ||
if style != '%': | ||
raise ValueError(f"__init__() does not currently accept {style} as valid style, please use %") | ||
super().__init__(*args, style=style, **kwargs) | ||
|
||
def levelname_in_front(self): | ||
loc = self._fmt.find('%(levelname)s') | ||
if loc == -1: | ||
return False | ||
return ')s' not in self._fmt[:loc] | ||
|
||
def format(self, record): | ||
unparsed = super().format(record) | ||
if not self.levelname_in_front(): | ||
return unparsed | ||
levels = ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL') | ||
max_length = max(map(len, levels)) | ||
for index, level in enumerate(levels): | ||
if level in unparsed: | ||
break | ||
end_loc = unparsed.find(level) + len(level) | ||
end = unparsed[end_loc] | ||
while end != ' ': | ||
end_loc += 1 | ||
end = unparsed[end_loc] | ||
spaces = max_length - len(level) | ||
returning = (" " * spaces) + unparsed[:end_loc] + unparsed[end_loc:] | ||
# print(f"returning == unparsed = {unparsed == returning}") | ||
return returning | ||
|
||
def file_renamer(filename: str): | ||
split = filename.split('.') | ||
return ".".join(split[:-3] + [split[-1], split[-2]]) | ||
|
||
def setUpLogger(name, fmt, logs_dir: pathlib.Path, files=True): | ||
# Init the logger | ||
logger = logging.getLogger(name) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
# Init the PrettyFormatter | ||
pretty = PrettyFormatter(fmt=fmt) | ||
|
||
# Create a handler that records all activity | ||
everything = logging.handlers.TimedRotatingFileHandler( | ||
logs_dir / f'{format(datetime.datetime.today(), "%Y-%m-%d")}.log', | ||
when='midnight', | ||
encoding='UTF-8' | ||
) | ||
# Do not use loggging.NOTSET, does not work for some reason | ||
# use logging.DEBUG if you want the lowest level | ||
everything.setLevel(logging.DEBUG) | ||
everything.setFormatter(pretty) | ||
|
||
# Rename files so .log is the file extension | ||
everything.namer = file_renamer | ||
|
||
# Add handlers to the logger | ||
logger.addHandler(everything) | ||
|
||
# Create a handler so we can see the output on the console | ||
console = logging.StreamHandler() | ||
console.setLevel(logging.DEBUG) | ||
console.setFormatter(pretty) | ||
|
||
# Add handler to the logger | ||
logger.addHandler(console) | ||
|
||
return logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.