diff --git a/toot/console.py b/toot/console.py index a280e52..3076517 100644 --- a/toot/console.py +++ b/toot/console.py @@ -7,7 +7,7 @@ import sys from argparse import ArgumentParser, FileType, ArgumentTypeError, Action from collections import namedtuple from itertools import chain -from toot import config, commands, CLIENT_NAME, CLIENT_WEBSITE, __version__ +from toot import config, commands, CLIENT_NAME, CLIENT_WEBSITE, __version__, settings from toot.exceptions import ApiError, ConsoleError from toot.output import print_out, print_err from toot.settings import get_setting @@ -932,9 +932,8 @@ def run_command(app, user, name, args): def main(): - # Enable debug logging if --debug is in args - if "--debug" in sys.argv: - filename = os.getenv("TOOT_LOG_FILE") + if settings.get_debug(): + filename = settings.get_debug_file() logging.basicConfig(level=logging.DEBUG, filename=filename) logging.getLogger("urllib3").setLevel(logging.INFO) diff --git a/toot/settings.py b/toot/settings.py index 1f08afe..aa02c1b 100644 --- a/toot/settings.py +++ b/toot/settings.py @@ -1,8 +1,11 @@ +import os +import sys + from functools import lru_cache from os.path import exists, join from tomlkit import parse from toot.config import get_config_dir -from typing import Type +from typing import Optional, Type TOOT_SETTINGS_FILE_NAME = "settings.toml" @@ -51,3 +54,18 @@ def _get_setting(dct, keys, type: Type, default=None): return _get_setting(dct[key], keys[1:], type, default) return default + + +def get_debug() -> bool: + if "--debug" in sys.argv: + return True + + return get_setting("common.debug", bool, False) + + +def get_debug_file() -> Optional[str]: + from_env = os.getenv("TOOT_LOG_FILE") + if from_env: + return from_env + + return get_setting("common.debug_file", str)