Skip to content

Commit cab4de5

Browse files
committed
the logger automatically exits on critical errors
1 parent 461789b commit cab4de5

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

src/core/app.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ def __init__(self, args: Namespace) -> None:
6868
self.log.debug('Received json config file path (%s)', self.args.cfg)
6969
if not os.path.isfile(self.args.cfg):
7070
self.log.critical('Invalid json config file path supplied (%s)', self.args.cfg)
71-
sys.exit(1)
7271

7372
__bar = bar_factory('\u2501', borders=(' ', ' '), background=' ')
7473
__spinner = frame_spinner_factory([colored(p, 'cyan') if supports_color else p for p in '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'])
@@ -144,7 +143,6 @@ def __get_json_config_path(self) -> str:
144143

145144
if len(found) == 0:
146145
self.log.critical('No json config file found in file tree')
147-
sys.exit(1)
148146
return found.pop()
149147

150148
def __on_end(self, sig: int, _: Any, /) -> None:
@@ -207,14 +205,12 @@ def __load_points(self, cfg: Config) -> None:
207205

208206
self.log.critical('Failed to parse line: %s (%s:%d)\n%s', line, cfg.file_path,
209207
len(self.points) + int(cfg.skip_first_line) - index, e)
210-
sys.exit(1)
211208

212209
except FileNotFoundError as e:
213210
self.log.error('Skipping unknown file: %s', e)
214211
return
215212
except Exception as e: # pylint: disable=broad-except
216213
self.log.critical('Failed to read file: %s\n%s', cfg.file_path, e)
217-
sys.exit(1)
218214
self.log.debug('Loaded %s points from file: \u2026/%s',
219215
format(len(self.points) + int(cfg.skip_first_line) - index, '_'), basename)
220216

@@ -277,25 +273,21 @@ def __setup(self) -> None:
277273
self.log.critical(
278274
'Failed to parse json config file : '
279275
'maximum nesting level could be reached, please check your file\n%s', e)
280-
sys.exit(1)
281276
if not raw_data:
282277
self.log.critical('Failed to parse %s : empty file', self.args.cfg)
283-
sys.exit(1)
284278
default: dict[str, Any] = None
285279
configs: list[dict[str, Any]] = None
286280
try:
287281
default = raw_data['default']
288282
configs = raw_data['configs']
289283
except KeyError as e:
290284
self.log.critical('Failed to parse %s : %s', self.args.cfg, e)
291-
sys.exit(1)
292285
cfgs: list[Config] = []
293286
try:
294287
for cfg in configs:
295288
cfgs.append(Config.from_json(json=cfg, **default))
296289
except ValueError as e:
297290
self.log.critical('Failed to parse config n°%d : %s', len(cfgs), e)
298-
sys.exit(1)
299291

300292
fset: list[int] = None
301293
if self.args.only and any(map(lambda x: x > len(cfgs), self.args.only)): # pylint: disable=bad-builtin

src/log/fmt.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

3+
import sys
4+
35
import logging
46
from typing import Any
57
from typing_extensions import override
68
from termcolor import colored
79

8-
__all__ = ['UsefulFormatter']
10+
__all__ = ['UsefulFormatter', 'UselessHandler']
911

1012

1113
def formatter(
@@ -42,3 +44,12 @@ def format(self, record: logging.LogRecord) -> str:
4244
log_fmt = formats.get(record.levelno)
4345
fmt = logging.Formatter(log_fmt, self.dt_fmt, style='%')
4446
return fmt.format(record)
47+
48+
49+
class UselessHandler(logging.StreamHandler):
50+
51+
@override
52+
def emit(self, record: logging.LogRecord) -> None:
53+
super().emit(record)
54+
if record.levelno >= logging.CRITICAL: # exit on critical errors
55+
sys.exit(1)

src/log/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def init_logger(log_lvl: int = logging.INFO) -> bool:
7575
color = supports_color()
7676

7777
# create console handler with a higher log level
78-
console_handler = logging.StreamHandler()
78+
console_handler = UselessHandler()
7979
console_handler.setLevel(log_lvl)
8080
console_handler.setFormatter(UsefulFormatter(colored_output=color))
8181

0 commit comments

Comments
 (0)