Skip to content

Commit

Permalink
Make CLI logging more sane
Browse files Browse the repository at this point in the history
Dragonfly's setup_log() function is now called by default and the
-q/--quiet option only suppresses loader-related informational log
messages.
  • Loading branch information
drmfinlay committed Oct 9, 2023
1 parent a972c1f commit 0452fca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 22 additions & 16 deletions dragonfly/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@

from dragonfly import get_engine, MimicFailure, EngineError
from dragonfly.loader import CommandModule, CommandModuleDirectory
from dragonfly.log import setup_log

LOG = logging.getLogger("command")


#---------------------------------------------------------------------------
# CLI helper functions.

def _set_logging_level(args):
if args.quiet:
args.log_level = "WARNING"
def _setup_logging(args):
# Use the specified logging defaults.
if args.log_level == "DFLY":
setup_log() # Use Dragonfly defaults.
else:
logging.basicConfig(level=getattr(logging, args.log_level))

# Set up logging with the specified logging level.
logging.basicConfig(level=getattr(logging, args.log_level))
# Suppress loader-related informational messages.
if args.quiet:
for logger_name in ("command", "module", "directory"):
logging.getLogger(logger_name).setLevel(logging.WARNING)


def _init_engine(args):
Expand Down Expand Up @@ -156,8 +162,8 @@ def _do_recognition(engine, args):
# Main CLI functions.

def cli_cmd_test(args):
# Set the logging level.
_set_logging_level(args)
# Setup logging.
_setup_logging(args)

# Initialise the specified engine. Return early if there was an error.
engine = _init_engine(args)
Expand Down Expand Up @@ -217,8 +223,8 @@ def cli_cmd_test(args):


def cli_cmd_load(args):
# Set the logging level.
_set_logging_level(args)
# Setup logging.
_setup_logging(args)

# Initialise the specified engine. Return early if there was an error.
engine = _init_engine(args)
Expand All @@ -245,8 +251,8 @@ def cli_cmd_load(args):


def cli_cmd_load_directory(args):
# Set the logging level.
_set_logging_level(args)
# Setup logging.
_setup_logging(args)

# Initialise the specified engine. Return early if there was an error.
engine = _init_engine(args)
Expand Down Expand Up @@ -400,14 +406,14 @@ def make_arg_parser():
"reading input from stdin or recognizing speech."
)
log_level_argument = _build_argument(
"-l", "--log-level", default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Log level to use."
"-l", "--log-level", default="DFLY",
choices=["DFLY", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="Logging defaults/level to use. By default, Dragonfly's "
"setup_log() function is called."
)
quiet_argument = _build_argument(
"-q", "--quiet", default=False, action="store_true",
help="Equivalent to '-l WARNING' -- suppresses INFO and DEBUG "
"logging."
help="Suppress loader-related informational messages."
)

# Create the parser for the "test" command.
Expand Down
2 changes: 2 additions & 0 deletions dragonfly/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@
"context.match": (_warning, _info),
"rule": (_warning, _info),
"clipboard": (_warning, _info),
"command": (_info, _info),
"config": (_warning, _info),
"module": (_info, _info),
"directory": (_info, _info),
"monitor.init": (_warning, _info),
"dfly.test": (_debug, _debug),
"accessibility": (_info, _info),
Expand Down

0 comments on commit 0452fca

Please sign in to comment.