Add color support + switch for logging

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-08-08 09:27:52 +02:00
parent 48427a967c
commit 9b055ef356
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
3 changed files with 43 additions and 6 deletions

View File

@ -33,8 +33,10 @@ def parse_args():
import argparse
# parse input arguments
parser = argparse.ArgumentParser(description = "repo-cloner initialization wizzard")
parser.add_argument('--base-dir', help = 'path to directory containing whole cloner structure', required = True,
parser.add_argument(
'--base-dir', help = 'path to directory containing whole cloner structure', required = True,
default = None, type = str)
parser.add_argument('--colored', help = "enable colored log output even tty() is not detected")
args = parser.parse_args()
base_dir = Path(args.base_dir)
@ -69,7 +71,7 @@ def gen_config_file(
"# cloner interval (in minutes, default=0 - run always)\n"
f"cloner_interval = {cloner_interval}\n\n"
"# do you need submodules support? (1/0)\n"
f"cloner_submodules = {cloner_submodules}\n\n"
f"cloner_submodules = {1 if cloner_submodules else 0}\n\n"
"# max depth of submodule scan (default = unlimited, uncomment to use)\n"
f"{'' if cloner_submodules else '# '}cloner_submodule_depth = {cloner_submodule_depth}\n\n"
)

View File

@ -1,9 +1,43 @@
import logging
import sys
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
light_blue = "\x1b[34;1m"
yellow = "\x1b[33;20m"
red = "\x1b[31;20m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
# format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
format = "%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)"
time_format = "%Y-%m-%d %H:%M:%S"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: light_blue + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, self.time_format)
return formatter.format(record)
console_logger = logging.StreamHandler()
# formatter for stdout - colored
if sys.stdout.isatty() or "--colored" in sys.argv:
console_formatter = CustomFormatter()
else:
# b&w for rest
console_formatter = logging.Formatter(
"%(asctime)-15s.%(msecs)03d :: [%(levelname)8s] :: %(name)-15s :: %(message)s (%(filename)s:%(lineno)s)",
"%Y-%m-%d %H:%M:%S")
# setup logger
console_logger.setFormatter(console_formatter)
log = logging.getLogger("rc")

View File

@ -90,6 +90,7 @@ def main() -> int:
parser.add_argument('--base-dir', help = 'path to directory containing whole cloner structure', required = True,
default = None, type = str)
parser.add_argument('--debug', '-d', help = "enable debug output", action = 'store_true')
parser.add_argument('--colored', help = "enable colored log output even tty() is not detected")
args = parser.parse_args()
if args.debug:
log.setLevel(logging.DEBUG)