From 2010311b76299bb3f0803a52510ab41d64d8e41a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Thu, 28 Jul 2016 03:30:00 +0200 Subject: Improve error message and handling --- music_sampler/helpers.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'music_sampler/helpers.py') diff --git a/music_sampler/helpers.py b/music_sampler/helpers.py index 68c1328..6acaba4 100644 --- a/music_sampler/helpers.py +++ b/music_sampler/helpers.py @@ -29,11 +29,11 @@ def register_fonts(): symbola = find_font("Symbola") if ubuntu_regular is None: - error_print("Font Ubuntu regular could not be found, please install it.") - sys.exit() + error_print("Font Ubuntu regular could not be found, " + "please install it.", exit=True) if symbola is None: - error_print("Font Symbola could not be found, please install it.") - sys.exit() + error_print("Font Symbola could not be found, please install it.", + exit=True) if ubuntu_bold is None: warn_print("Font Ubuntu Bold could not be found.") @@ -180,12 +180,29 @@ def gain(volume, old_volume=None): 20 * math.log10(max(volume, 0.1) / max(old_volume, 0.1)), max(volume, 0)] -def debug_print(message, with_trace=False): +def debug_print(message, with_trace=None): + if with_trace is None: + with_trace = (Logger.getEffectiveLevel() < logging.WARN) + with_trace &= (sys.exc_info()[0] is not None) + Logger.debug('MusicSampler: ' + message, exc_info=with_trace) -def error_print(message, with_trace=False): - Logger.error('MusicSampler: ' + message, exc_info=with_trace) +def error_print(message, exit=False, with_trace=None): + if with_trace is None: + with_trace = (Logger.getEffectiveLevel() < logging.WARN) + with_trace &= (sys.exc_info()[0] is not None) + + # FIXME: handle it correctly when in a thread + if exit: + Logger.critical('MusicSampler: ' + message, exc_info=with_trace) + sys.exit(1) + else: + Logger.error('MusicSampler: ' + message, exc_info=with_trace) + +def warn_print(message, with_trace=None): + if with_trace is None: + with_trace = (Logger.getEffectiveLevel() < logging.WARN) + with_trace &= (sys.exc_info()[0] is not None) -def warn_print(message, with_trace=False): Logger.warn('MusicSampler: ' + message, exc_info=with_trace) -- cgit v1.2.3