From aa3479abeb72f0048e737a05fe312dfef10fa123 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 11 Dec 2022 10:54:03 +0100 Subject: [PATCH] Added [-v|--verbose] and --version options to the command line. --- CHANGELOG.md | 10 +++++++++- platypush/__init__.py | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ac59c2f9..b0737db1b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,17 @@ All notable changes to this project will be documented in this file. Given the high speed of development in the first phase, changes are being reported only starting from v0.20.2. +## [Unreleased] + +### Added + +- Added `[-v|--verbose]` command-line option to override the default logging + configuration and enable debug logging. +- Added `--version` command-line option to print the current version and exit. + ## [0.24.2] - 2022-12-10 -## Fixed +### Fixed - The `main.db` configuration should use the configured `workdir` when no values are specified. diff --git a/platypush/__init__.py b/platypush/__init__.py index edc1f12d83..78ffb1b3cc 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -59,6 +59,7 @@ class Daemon: no_capture_stdout=False, no_capture_stderr=False, redis_queue=None, + verbose=False, ): """ Constructor @@ -74,6 +75,7 @@ class Daemon: no_capture_stderr -- Set to true if you want to disable the stderr capture by the logging system redis_queue -- Name of the (Redis) queue used for dispatching messages (default: platypush/bus). + verbose -- Enable debug/verbose logging, overriding the stored configuration (default: False). """ if pidfile: @@ -84,7 +86,10 @@ class Daemon: self.redis_queue = redis_queue or self._default_redis_queue self.config_file = config_file Config.init(self.config_file) - logging.basicConfig(**Config.get('logging')) + logging_conf = Config.get('logging') or {} + if verbose: + logging_conf['level'] = logging.DEBUG + logging.basicConfig(**logging_conf) redis_conf = Config.get('backend.redis') or {} self.bus = RedisBus( @@ -116,6 +121,21 @@ class Daemon: default=None, help=cls.config_file.__doc__, ) + parser.add_argument( + '--version', + dest='version', + required=False, + action='store_true', + help="Print the current version and exit", + ) + parser.add_argument( + '--verbose', + '-v', + dest='verbose', + required=False, + action='store_true', + help="Enable verbose/debug logging", + ) parser.add_argument( '--pidfile', '-P', @@ -154,12 +174,18 @@ class Daemon: ) opts, args = parser.parse_known_args(args) + + if opts.version: + print(__version__) + sys.exit(0) + return cls( config_file=opts.config, pidfile=opts.pidfile, no_capture_stdout=opts.no_capture_stdout, no_capture_stderr=opts.no_capture_stderr, redis_queue=opts.redis_queue, + verbose=opts.verbose, ) def on_message(self):