X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=music_sampler%2Fhelpers.py;h=2249746a5a928cf2c416915a0b587c6b10b704d8;hb=7d6df771f603e9642047264f967525422ef09e99;hp=1788084b59100ce028aa31c36fb37bc81525830d;hpb=6ebe62478a49df22c55ef6a2b1200473500a7f80;p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git diff --git a/music_sampler/helpers.py b/music_sampler/helpers.py index 1788084..2249746 100644 --- a/music_sampler/helpers.py +++ b/music_sampler/helpers.py @@ -5,6 +5,7 @@ import os import math import sounddevice as sd import logging +Logger = logging.getLogger("kivy") from . import sysfont @@ -28,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.") @@ -59,6 +60,11 @@ def parse_args(): sys.argv.extend(kivy_args) + os.environ["KIVY_NO_CONFIG"] = 'true' + sys.argv.extend(["-c", "kivy:log_level:warning"]) + sys.argv.extend(["-c", "kivy:log_dir:/tmp"]) + sys.argv.extend(["-c", "kivy:log_name:/tmp/music_sampler_%_.txt"]) + parser = argparse.ArgumentParser( description="A Music Sampler application.", formatter_class=argparse.ArgumentDefaultsHelpFormatter) @@ -117,14 +123,15 @@ def parse_args(): action=ListDevicesAction, help="List available sound devices" ) + parser.add_argument("--no-focus-warning", + action='store_true', + help="Don't show warning when focus is lost" + ) parser.add_argument('--', dest="args", help="Kivy arguments. All arguments after this are interpreted\ by Kivy. Pass \"-- --help\" to get Kivy's usage.") - from kivy.logger import Logger - Logger.setLevel(logging.WARN) - args = parser.parse_args(argv) Config.yml_file = args.config @@ -135,6 +142,7 @@ def parse_args(): Config.channels = args.channels Config.sample_width = args.sample_width Config.builtin_mixing = args.builtin_mixing + Config.no_focus_warning = args.no_focus_warning if args.music_path.endswith("/"): Config.music_path = args.music_path else: @@ -142,8 +150,7 @@ def parse_args(): class DebugModeAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): - from kivy.logger import Logger - Logger.setLevel(logging.DEBUG) + sys.argv.extend(["-c", "kivy:log_level:debug"]) class SelectDeviceAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): @@ -178,15 +185,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): - from kivy.logger import Logger +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): - from kivy.logger import Logger - 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): - from kivy.logger import Logger Logger.warn('MusicSampler: ' + message, exc_info=with_trace)