From 0192c7304c111212e392e7643e53537b2b42813c Mon Sep 17 00:00:00 2001 From: Sam Schott Date: Thu, 2 Apr 2020 16:01:29 +0100 Subject: [PATCH] added --verbose option to `maestral start|restart` --foreground no longer logs to stdout by default --- maestral/cli.py | 18 ++++++++++-------- maestral/daemon.py | 15 ++++++++------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/maestral/cli.py b/maestral/cli.py index 9752cbd9..7fd9a957 100755 --- a/maestral/cli.py +++ b/maestral/cli.py @@ -53,13 +53,13 @@ def pending_link_cli(config_name): 'to load Dropbox credentials.') -def start_daemon_subprocess_with_cli_feedback(config_name): +def start_daemon_subprocess_with_cli_feedback(config_name, log_to_stdout=False): """Wrapper around `daemon.start_maestral_daemon_process` with command line feedback.""" from maestral.daemon import start_maestral_daemon_process, Start click.echo('Starting Maestral...', nl=False) - res = start_maestral_daemon_process(config_name) + res = start_maestral_daemon_process(config_name, log_to_stdout=log_to_stdout) if res == Start.Ok: click.echo('\rStarting Maestral... ' + OK) else: @@ -322,8 +322,10 @@ def gui(config_name): @config_option @click.option('--foreground', '-f', is_flag=True, default=False, help='Starts Maestral in the foreground.') +@click.option('--verbose', '-v', is_flag=True, default=False, + help='Print log messages to stdout.') @catch_maestral_errors -def start(config_name: str, foreground: bool): +def start(config_name: str, foreground: bool, verbose: bool): """Starts the Maestral as a daemon.""" from maestral.daemon import get_maestral_pid @@ -360,11 +362,9 @@ def start(config_name: str, foreground: bool): # start daemon if foreground: from maestral.daemon import run_maestral_daemon - from maestral.constants import INVOCATION_ID - # don't log to stdout if started from systemd - run_maestral_daemon(config_name, run=True, log_to_stdout=not INVOCATION_ID) + run_maestral_daemon(config_name, run=True, log_to_stdout=verbose) else: - start_daemon_subprocess_with_cli_feedback(config_name) + start_daemon_subprocess_with_cli_feedback(config_name, log_to_stdout=verbose) @main.command(help_priority=2) @@ -378,8 +378,10 @@ def stop(config_name: str): @existing_config_option @click.option('--foreground', '-f', is_flag=True, default=False, help='Starts Maestral in the foreground.') +@click.option('--verbose', '-v', is_flag=True, default=False, + help='Print log messages to stdout.') @click.pass_context -def restart(ctx, config_name: str, foreground: bool): +def restart(ctx, config_name: str, foreground: bool, verbose: bool): """Restarts the Maestral daemon.""" stop_daemon_with_cli_feedback(config_name) ctx.forward(start) diff --git a/maestral/daemon.py b/maestral/daemon.py index f924ffea..3343f848 100644 --- a/maestral/daemon.py +++ b/maestral/daemon.py @@ -247,7 +247,7 @@ def run_maestral_daemon(config_name='maestral', run=True, log_to_stdout=False): lockfile.release() -def start_maestral_daemon_thread(config_name='maestral', run=True): +def start_maestral_daemon_thread(config_name='maestral', run=True, log_to_stdout=False): """ Starts the Maestral daemon in a thread (by calling `start_maestral_daemon`). This command will create a new daemon on each run. Take care not to sync the same @@ -257,14 +257,14 @@ def start_maestral_daemon_thread(config_name='maestral', run=True): :param str config_name: The name of the Maestral configuration to use. :param bool run: If ``True``, start syncing automatically. Defaults to ``True``. - :returns: ``True`` if started, ``False`` otherwise. - :rtype: bool + :param bool log_to_stdout: If ``True``, write logs to stdout. Defaults to ``False``. + :returns: ``Start.Ok`` if successful, ``Start.Failed`` otherwise. """ import threading t = threading.Thread( target=run_maestral_daemon, - args=(config_name, run), + args=(config_name, run, log_to_stdout), name=f'maestral-daemon-{config_name}', daemon=True, ) @@ -278,12 +278,13 @@ def start_maestral_daemon_thread(config_name='maestral', run=True): return _wait_for_startup(config_name, timeout=8) -def start_maestral_daemon_process(config_name='maestral', run=True): +def start_maestral_daemon_process(config_name='maestral', run=True, log_to_stdout=False): """ Starts the Maestral daemon as a separate process (by calling `start_maestral_daemon`). :param str config_name: The name of the Maestral configuration to use. :param bool run: If ``True``, start syncing automatically. Defaults to ``True``. + :param bool log_to_stdout: If ``True``, write logs to stdout. Defaults to ``False``. :returns: ``Start.Ok`` if successful, ``Start.Failed`` otherwise. """ import subprocess @@ -292,14 +293,14 @@ def start_maestral_daemon_process(config_name='maestral', run=True): STD_IN_OUT = subprocess.DEVNULL - # use nested Popen and multiprocessing.Process to effectively create double fork + # use nested Popen and multiprocessing.Process to effectively create double fork` # see Unix 'double-fork magic' def target(cc, r): cc = quote(cc) r = bool(r) subprocess.Popen( - [sys.executable, '-c', f'import maestral.daemon; maestral.daemon.run_maestral_daemon("{cc}", {r})'], + [sys.executable, '-c', f'import maestral.daemon; maestral.daemon.run_maestral_daemon("{cc}", {r}, {log_to_stdout})'], stdin=STD_IN_OUT, stdout=STD_IN_OUT, stderr=STD_IN_OUT, )