Skip to content

Commit

Permalink
Minor fixes and improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
MPCodeWriter21 committed Aug 18, 2022
1 parent 3f53d58 commit 5bb4c45
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 64 deletions.
4 changes: 4 additions & 0 deletions CHANGES-LOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Help this project by [Donation](DONATE.md)
Changes log
-----------

### 2.3.9

Minor fixes and improvements.

### 2.3.8

Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ python setup.py install
Changes
-------

### 2.3.8
### 2.3.9

Added `catch` and `ignore` methods to `log21.CrashReporter.Reporter`.
Minor fixes and improvements.

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

Expand Down
70 changes: 23 additions & 47 deletions log21/CrashReporter/Reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,54 +87,20 @@ def reporter(self, func):
:return: Wrapped function.
"""

exceptions_to_catch = tuple(self._exceptions_to_catch) if self._exceptions_to_catch else None
exceptions_to_ignore = tuple(self._exceptions_to_ignore) if self._exceptions_to_ignore else None

if exceptions_to_catch and exceptions_to_ignore:
@_wraps(func)
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseException as e:
if isinstance(e, exceptions_to_catch) and not isinstance(e, exceptions_to_ignore):
self._reporter_function(e)
if self.raise_after_report:
raise e
else:
raise e
elif self._exceptions_to_catch:
@_wraps(func)
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseException as e:
if isinstance(e, exceptions_to_catch):
self._reporter_function(e)
if self.raise_after_report:
raise e
else:
raise e
elif self._exceptions_to_ignore:
@_wraps(func)
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseException as e:
if not isinstance(e, exceptions_to_ignore):
self._reporter_function(e)
if self.raise_after_report:
raise e
else:
raise e
else:
@_wraps(func)
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseException as e:
exceptions_to_catch = tuple(self._exceptions_to_catch) if self._exceptions_to_catch else BaseException
exceptions_to_ignore = tuple(self._exceptions_to_ignore) if self._exceptions_to_ignore else tuple()

@_wraps(func)
def wrap(*args, **kwargs):
try:
return func(*args, **kwargs)
except BaseException as e:
if isinstance(e, exceptions_to_catch) and not isinstance(e, exceptions_to_ignore):
self._reporter_function(e)
if self.raise_after_report:
raise e
else:
raise e

return wrap

Expand All @@ -144,19 +110,29 @@ def catch(self, exception: BaseException):
:param exception: Exception to catch.
"""
if not isinstance(exception, BaseException):
raise TypeError('`exception` must be an instance of BaseException')
if self._exceptions_to_catch is None:
self._exceptions_to_catch = set()
self._exceptions_to_catch.add(exception)
if exception not in self._exceptions_to_catch:
self._exceptions_to_catch.add(exception)
else:
raise ValueError('exception is already in the list of exceptions to catch')

def ignore(self, exception: BaseException):
"""
Add an exception to the list of exceptions to ignore.
:param exception: Exception to ignore.
"""
if not isinstance(exception, BaseException):
raise TypeError('`exception` must be an instance of BaseException')
if self._exceptions_to_ignore is None:
self._exceptions_to_ignore = set()
self._exceptions_to_ignore.add(exception)
if exception not in self._exceptions_to_ignore:
self._exceptions_to_ignore.add(exception)
else:
raise ValueError('exception is already in the list of exceptions to ignore')


class ConsoleReporter(Reporter):
Expand Down
15 changes: 7 additions & 8 deletions log21/StreamHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ def check_cr(self, record):
record.msg = _gc(*find.split(record.msg[:index])) + record.msg[index + 1:]

def check_nl(self, record):
if record.msg:
while record.msg[0] == '\n':
file_descriptor = getattr(self.stream, 'fileno', None)
if file_descriptor:
file_descriptor = file_descriptor()
if file_descriptor in (1, 2): # stdout or stderr
self.stream.write('\n')
record.msg = record.msg[1:]
while record.msg and record.msg[0] == '\n':
file_descriptor = getattr(self.stream, 'fileno', None)
if file_descriptor:
file_descriptor = file_descriptor()
if file_descriptor in (1, 2): # stdout or stderr
self.stream.write('\n')
record.msg = record.msg[1:]

def emit(self, record):
if self.HandleCR:
Expand Down
12 changes: 6 additions & 6 deletions log21/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from log21.Formatters import ColorizingFormatter, DecolorizingFormatter
from log21.Colors import Colors, get_color, get_colors, ansi_escape, get_color_name, closest_color

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

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


__console_reporter = CrashReporter.ConsoleReporter()
console_crash_reporter = __console_reporter.reporter
console_reporter = CrashReporter.ConsoleReporter()
console_crash_reporter = console_reporter.reporter

__file_reporter = CrashReporter.FileReporter(file='crash_report.log')
file_crash_reporter = __file_reporter.reporter
file_reporter = CrashReporter.FileReporter(file='crash_report.log')
file_crash_reporter = file_reporter.reporter
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
long_description = file.read()

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

setup(
name='log21',
Expand Down

0 comments on commit 5bb4c45

Please sign in to comment.