]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler/helpers.py
Improve error message and handling
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler / helpers.py
index 68c1328998379a9d5f2c1deb61f5174d0359741a..6acaba4f7e9300523ef6c9a20c389b2eb9d79484 100644 (file)
@@ -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)