Skip to content

Commit 2014c59

Browse files
Added CrashReporter!
You can use Reporter classes to monitor your program and send crash reports to the developer. It can help you fix the bugs and improve your program before your users get upset about it. See some examples in the log21/CrashReporter/Reporters.py file.
1 parent 77c6994 commit 2014c59

12 files changed

Lines changed: 977 additions & 556 deletions

File tree

CHANGES-LOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ Help this project by [Donation](DONATE.md)
66
Changes log
77
-----------
88

9+
### 2.2.0
10+
11+
Added CrashReporter!
12+
13+
You can use Reporter classes to monitor your program and send crash reports to the developer. It can help you fix the
14+
bugs and improve your program before your users get upset about it. See some examples in
15+
the [log21/CrashReporter/Reporters.py](https://github.com/MPCodeWriter21/log21/blob/master/log21/CrashReporter/Reporters.py)
16+
file.
17+
918
### 2.1.8
1019

1120
Bug fixes.

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,14 @@ python setup.py install
5050
Changes
5151
-------
5252

53-
### 2.1.8
53+
### 2.2.0
5454

55-
Bug fixes.
55+
Added CrashReporter!
56+
57+
You can use Reporter classes to monitor your program and send crash reports to the developer. It can help you fix the
58+
bugs and improve your program before your users get upset about it. See some examples in
59+
the [log21/CrashReporter/Reporters.py](https://github.com/MPCodeWriter21/log21/blob/master/log21/CrashReporter/Reporters.py)
60+
file.
5661

5762
[Full Changes Log](https://github.com/MPCodeWriter21/log21/blob/master/CHANGES-LOG.md)
5863

log21/CrashReporter/Formatters.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# log21.CrashReporter.Formatters.py
2+
# CodeWriter21
3+
4+
import traceback
5+
6+
from datetime import datetime as _datetime
7+
8+
__all__ = ['Formatter', 'CONSOLE_REPORTER_FORMAT', 'FILE_REPORTER_FORMAT', 'EMAIL_REPORTER_FORMAT']
9+
10+
11+
class Formatter:
12+
def __init__(self, format_: str, style: str = '%', datefmt: str = '%Y-%m-%d %H:%M:%S'):
13+
self._format = format_
14+
15+
if style in ['%', '{']:
16+
self.__style = style
17+
else:
18+
raise ValueError('Invalid style: "' + str(style) + '" Valid styles: %, {')
19+
20+
self.datefmt = datefmt
21+
22+
def format(self, exception: Exception):
23+
exception_dict = {
24+
'__name__': __name__,
25+
'type': type(exception),
26+
'message': exception.args[0],
27+
'traceback': traceback.format_tb(exception.__traceback__.tb_next),
28+
'name': exception.__class__.__name__,
29+
'file': exception.__traceback__.tb_next.tb_frame.f_code.co_filename,
30+
'lineno': exception.__traceback__.tb_next.tb_lineno,
31+
'function': exception.__traceback__.tb_next.tb_frame.f_code.co_name,
32+
'asctime': _datetime.now().strftime(self.datefmt),
33+
}
34+
35+
if self.__style == '%':
36+
return self._format % exception_dict
37+
elif self.__style == '{':
38+
return self._format.format(**exception_dict)
39+
else:
40+
raise ValueError('Invalid style: "' + str(self.__style) + '" Valid styles: %, {')
41+
42+
43+
CONSOLE_REPORTER_FORMAT = dict(
44+
format_=f'\033[91m%(name)s: %(message)s\033[0m\n' # Name and message of the exception.
45+
f'\tFile\033[91m:\033[0m "%(file)s"\n' # The file that exception was raised in.
46+
f'\tLine\033[91m:\033[0m %(lineno)d', # The line that exception was raised on.
47+
style='%'
48+
)
49+
50+
FILE_REPORTER_FORMAT = dict(
51+
format_=f'[%(asctime)s] %(name)s: %(message)s' # Name and message of the exception.
52+
f'; File: "%(file)s"' # The file that exception was raised in.
53+
f'; Line: %(lineno)d\n', # The line that exception was raised on.
54+
style='%'
55+
)
56+
57+
EMAIL_REPORTER_FORMAT = dict(
58+
format_="""
59+
<html>
60+
<body>
61+
<h1>Crash Report: %(__name__)s</h1>
62+
<h2>%(name)s: %(message)s</h2>
63+
<p>
64+
<span style="bold">File:</span> "%(file)s"<br>
65+
<span style="bold">Line:</span> %(lineno)d<br>
66+
<span style="center">%(asctime)s</span><br>
67+
</p>
68+
<body>
69+
</html>
70+
""",
71+
style='%'
72+
)

0 commit comments

Comments
 (0)