Skip to content

Commit 5bb4c45

Browse files
Minor fixes and improvements.
1 parent 3f53d58 commit 5bb4c45

6 files changed

Lines changed: 43 additions & 64 deletions

File tree

CHANGES-LOG.md

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

9+
### 2.3.9
10+
11+
Minor fixes and improvements.
12+
913
### 2.3.8
1014

1115
Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ python setup.py install
5353
Changes
5454
-------
5555

56-
### 2.3.8
56+
### 2.3.9
5757

58-
Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.
58+
Minor fixes and improvements.
5959

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

log21/CrashReporter/Reporters.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -87,54 +87,20 @@ def reporter(self, func):
8787
:return: Wrapped function.
8888
"""
8989

90-
exceptions_to_catch = tuple(self._exceptions_to_catch) if self._exceptions_to_catch else None
91-
exceptions_to_ignore = tuple(self._exceptions_to_ignore) if self._exceptions_to_ignore else None
92-
93-
if exceptions_to_catch and exceptions_to_ignore:
94-
@_wraps(func)
95-
def wrap(*args, **kwargs):
96-
try:
97-
return func(*args, **kwargs)
98-
except BaseException as e:
99-
if isinstance(e, exceptions_to_catch) and not isinstance(e, exceptions_to_ignore):
100-
self._reporter_function(e)
101-
if self.raise_after_report:
102-
raise e
103-
else:
104-
raise e
105-
elif self._exceptions_to_catch:
106-
@_wraps(func)
107-
def wrap(*args, **kwargs):
108-
try:
109-
return func(*args, **kwargs)
110-
except BaseException as e:
111-
if isinstance(e, exceptions_to_catch):
112-
self._reporter_function(e)
113-
if self.raise_after_report:
114-
raise e
115-
else:
116-
raise e
117-
elif self._exceptions_to_ignore:
118-
@_wraps(func)
119-
def wrap(*args, **kwargs):
120-
try:
121-
return func(*args, **kwargs)
122-
except BaseException as e:
123-
if not isinstance(e, exceptions_to_ignore):
124-
self._reporter_function(e)
125-
if self.raise_after_report:
126-
raise e
127-
else:
128-
raise e
129-
else:
130-
@_wraps(func)
131-
def wrap(*args, **kwargs):
132-
try:
133-
return func(*args, **kwargs)
134-
except BaseException as e:
90+
exceptions_to_catch = tuple(self._exceptions_to_catch) if self._exceptions_to_catch else BaseException
91+
exceptions_to_ignore = tuple(self._exceptions_to_ignore) if self._exceptions_to_ignore else tuple()
92+
93+
@_wraps(func)
94+
def wrap(*args, **kwargs):
95+
try:
96+
return func(*args, **kwargs)
97+
except BaseException as e:
98+
if isinstance(e, exceptions_to_catch) and not isinstance(e, exceptions_to_ignore):
13599
self._reporter_function(e)
136100
if self.raise_after_report:
137101
raise e
102+
else:
103+
raise e
138104

139105
return wrap
140106

@@ -144,19 +110,29 @@ def catch(self, exception: BaseException):
144110
145111
:param exception: Exception to catch.
146112
"""
113+
if not isinstance(exception, BaseException):
114+
raise TypeError('`exception` must be an instance of BaseException')
147115
if self._exceptions_to_catch is None:
148116
self._exceptions_to_catch = set()
149-
self._exceptions_to_catch.add(exception)
117+
if exception not in self._exceptions_to_catch:
118+
self._exceptions_to_catch.add(exception)
119+
else:
120+
raise ValueError('exception is already in the list of exceptions to catch')
150121

151122
def ignore(self, exception: BaseException):
152123
"""
153124
Add an exception to the list of exceptions to ignore.
154125
155126
:param exception: Exception to ignore.
156127
"""
128+
if not isinstance(exception, BaseException):
129+
raise TypeError('`exception` must be an instance of BaseException')
157130
if self._exceptions_to_ignore is None:
158131
self._exceptions_to_ignore = set()
159-
self._exceptions_to_ignore.add(exception)
132+
if exception not in self._exceptions_to_ignore:
133+
self._exceptions_to_ignore.add(exception)
134+
else:
135+
raise ValueError('exception is already in the list of exceptions to ignore')
160136

161137

162138
class ConsoleReporter(Reporter):

log21/StreamHandler.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ def check_cr(self, record):
4444
record.msg = _gc(*find.split(record.msg[:index])) + record.msg[index + 1:]
4545

4646
def check_nl(self, record):
47-
if record.msg:
48-
while record.msg[0] == '\n':
49-
file_descriptor = getattr(self.stream, 'fileno', None)
50-
if file_descriptor:
51-
file_descriptor = file_descriptor()
52-
if file_descriptor in (1, 2): # stdout or stderr
53-
self.stream.write('\n')
54-
record.msg = record.msg[1:]
47+
while record.msg and record.msg[0] == '\n':
48+
file_descriptor = getattr(self.stream, 'fileno', None)
49+
if file_descriptor:
50+
file_descriptor = file_descriptor()
51+
if file_descriptor in (1, 2): # stdout or stderr
52+
self.stream.write('\n')
53+
record.msg = record.msg[1:]
5554

5655
def emit(self, record):
5756
if self.HandleCR:

log21/__init__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
2222
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color
2323

24-
__version__ = "2.3.8"
24+
__version__ = "2.3.9"
2525
__author__ = "CodeWriter21 (Mehrad Pooryoussof)"
2626
__github__ = "Https://GitHub.com/MPCodeWriter21/log21"
2727
__all__ = ['ColorizingStreamHandler', 'DecolorizingFileHandler', 'ColorizingFormatter', 'DecolorizingFormatter',
@@ -31,7 +31,7 @@
3131
'ansi_escape', '__version__', '__author__', '__github__', 'debug', 'info', 'warning', 'warn', 'error',
3232
'critical', 'fatal', 'exception', 'log', 'basic_config', 'basicConfig', 'ProgressBar', 'progress_bar',
3333
'LoggingWindow', 'LoggingWindowHandler', 'get_logging_window', 'CrashReporter', 'console_crash_reporter',
34-
'file_crash_reporter']
34+
'file_crash_reporter', 'console_reporter', 'file_reporter']
3535

3636
_manager = Manager()
3737
_logging.setLoggerClass(Logger)
@@ -440,8 +440,8 @@ def progress_bar(progress: float, total: float, width: int = None, prefix: str =
440440
print(bar.get_bar(progress, total))
441441

442442

443-
__console_reporter = CrashReporter.ConsoleReporter()
444-
console_crash_reporter = __console_reporter.reporter
443+
console_reporter = CrashReporter.ConsoleReporter()
444+
console_crash_reporter = console_reporter.reporter
445445

446-
__file_reporter = CrashReporter.FileReporter(file='crash_report.log')
447-
file_crash_reporter = __file_reporter.reporter
446+
file_reporter = CrashReporter.FileReporter(file='crash_report.log')
447+
file_crash_reporter = file_reporter.reporter

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
long_description = file.read()
88

99
DESCRIPTION = 'A simple logging package that helps you log colorized messages in Windows console.'
10-
VERSION = '2.3.8'
10+
VERSION = '2.3.9'
1111

1212
setup(
1313
name='log21',

0 commit comments

Comments
 (0)