add ColorFormatter class for optional colorized log output
This commit is contained in:
parent
7e8e2403cd
commit
290587f449
@ -110,7 +110,7 @@ __complete_gpgsign() {
|
||||
|
||||
__complete_install() {
|
||||
opts="-v -q -a -p -n -y"
|
||||
lopts="--verbose --quiet --all --privacy-mode --no --yes"
|
||||
lopts="--verbose --quiet --all --color --no-color --privacy-mode --no-privacy-mode --no --yes"
|
||||
case "${cur}" in
|
||||
-*)
|
||||
__complete_options
|
||||
@ -251,7 +251,7 @@ __complete_btlog() {
|
||||
|
||||
__complete_mirror() {
|
||||
opts="-v"
|
||||
lopts="--all --archive --build-logs --pgp-signatures --src-tarballs --output-dir"
|
||||
lopts="--all --archive --build-logs --color --no-color --pgp-signatures --src-tarballs --output-dir"
|
||||
__complete_options
|
||||
}
|
||||
|
||||
@ -270,7 +270,7 @@ __complete_deploy() {
|
||||
|
||||
__complete_signatures() {
|
||||
opts="-v -q"
|
||||
lopts="--verbose --no-check-https"
|
||||
lopts="--verbose --color --no-color --no-check-https"
|
||||
case "${cur}" in
|
||||
-*)
|
||||
__complete_options
|
||||
@ -289,7 +289,7 @@ __complete_signindex() {
|
||||
__complete_init() {
|
||||
opts="-v -q -d"
|
||||
lopts="--verbose --quiet --distinguished-name --keystore
|
||||
--repo-keyalias --android-home --no-prompt"
|
||||
--repo-keyalias --android-home --no-prompt --color --no-color"
|
||||
__complete_options
|
||||
}
|
||||
|
||||
|
@ -56,6 +56,7 @@ from pathlib import Path
|
||||
|
||||
import defusedxml.ElementTree as XMLElementTree
|
||||
|
||||
from argparse import BooleanOptionalAction
|
||||
from asn1crypto import cms
|
||||
from base64 import urlsafe_b64encode
|
||||
from binascii import hexlify
|
||||
@ -241,9 +242,40 @@ def setup_global_opts(parser):
|
||||
default=False,
|
||||
help=_("Restrict output to warnings and errors"),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--color",
|
||||
action=BooleanOptionalAction,
|
||||
default=None,
|
||||
help=_("Color the log output"),
|
||||
)
|
||||
|
||||
|
||||
def set_console_logging(verbose=False):
|
||||
class ColorFormatter(logging.Formatter):
|
||||
|
||||
def __init__(self, msg):
|
||||
logging.Formatter.__init__(self, msg)
|
||||
|
||||
bright_black = "\x1b[90;20m"
|
||||
yellow = "\x1b[33;20m"
|
||||
red = "\x1b[31;20m"
|
||||
bold_red = "\x1b[31;1m"
|
||||
reset = "\x1b[0m"
|
||||
|
||||
self.FORMATS = {
|
||||
logging.DEBUG: bright_black + msg + reset,
|
||||
logging.INFO: reset + msg + reset, # use default color
|
||||
logging.WARNING: yellow + msg + reset,
|
||||
logging.ERROR: red + msg + reset,
|
||||
logging.CRITICAL: bold_red + msg + reset
|
||||
}
|
||||
|
||||
def format(self, record):
|
||||
log_fmt = self.FORMATS.get(record.levelno)
|
||||
formatter = logging.Formatter(log_fmt)
|
||||
return formatter.format(record)
|
||||
|
||||
|
||||
def set_console_logging(verbose=False, color=False):
|
||||
"""Globally set logging to output nicely to the console."""
|
||||
|
||||
class _StdOutFilter(logging.Filter):
|
||||
@ -255,13 +287,18 @@ def set_console_logging(verbose=False):
|
||||
else:
|
||||
level = logging.ERROR
|
||||
|
||||
if color or (color is None and sys.stdout.isatty()):
|
||||
formatter = ColorFormatter
|
||||
else:
|
||||
formatter = logging.Formatter
|
||||
|
||||
stdout_handler = logging.StreamHandler(sys.stdout)
|
||||
stdout_handler.addFilter(_StdOutFilter())
|
||||
stdout_handler.setFormatter(logging.Formatter('%(message)s'))
|
||||
stdout_handler.setFormatter(formatter('%(message)s'))
|
||||
|
||||
stderr_handler = logging.StreamHandler(sys.stderr)
|
||||
stderr_handler.setLevel(logging.ERROR)
|
||||
stderr_handler.setFormatter(logging.Formatter(_('ERROR: %(message)s')))
|
||||
stderr_handler.setFormatter(formatter(_('ERROR: %(message)s')))
|
||||
|
||||
logging.basicConfig(
|
||||
force=True, level=level, handlers=[stdout_handler, stderr_handler]
|
||||
|
@ -82,7 +82,7 @@ def main():
|
||||
)
|
||||
options = common.parse_args(parser)
|
||||
|
||||
common.set_console_logging(options.verbose)
|
||||
common.set_console_logging(options.verbose, options.color)
|
||||
|
||||
fdroiddir = os.getcwd()
|
||||
test_config = dict()
|
||||
|
@ -350,7 +350,7 @@ def main():
|
||||
)
|
||||
options = common.parse_args(parser)
|
||||
|
||||
common.set_console_logging(options.verbose)
|
||||
common.set_console_logging(options.verbose, options.color)
|
||||
logging.captureWarnings(True) # for SNIMissingWarning
|
||||
|
||||
common.get_config()
|
||||
|
@ -91,7 +91,7 @@ def main():
|
||||
)
|
||||
options = common.parse_args(parser)
|
||||
|
||||
common.set_console_logging(options.verbose)
|
||||
common.set_console_logging(options.verbose, options.color)
|
||||
|
||||
if options.all:
|
||||
options.archive = True
|
||||
|
@ -104,7 +104,7 @@ def main():
|
||||
)
|
||||
parser.add_argument("--no-check-https", action="store_true", default=False)
|
||||
options = common.parse_args(parser)
|
||||
common.set_console_logging(options.verbose)
|
||||
common.set_console_logging(options.verbose, options.color)
|
||||
common.read_config()
|
||||
|
||||
extract(options)
|
||||
|
Loading…
x
Reference in New Issue
Block a user