Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:18

0001 # This file is part of the ACTS project.
0002 #
0003 # Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 #
0005 # This Source Code Form is subject to the terms of the Mozilla Public
0006 # License, v. 2.0. If a copy of the MPL was not distributed with this
0007 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 import argparse
0010 import logging
0011 import sys
0012 from datetime import datetime
0013 
0014 # ------------------------------------------------------------------------------
0015 # Options parsing
0016 # ------------------------------------------------------------------------------
0017 
0018 """ Parent parser that contains common options """
0019 
0020 
0021 def common_options(prog_name=sys.argv[0]):
0022 
0023     parser = argparse.ArgumentParser(add_help=False, prog=prog_name)
0024 
0025     parser.add_argument(
0026         "--debug", "-d", help=("Enables debug logging"), action="store_true"
0027     )
0028     parser.add_argument("--logfile", help=("Write log in file"), default="", type=str)
0029 
0030     return parser
0031 
0032 
0033 """ Parse common options from commandline """
0034 
0035 
0036 def parse_common_options(args, prog_name=sys.argv[0]):
0037 
0038     # Set log level
0039     log_level = logging.INFO
0040     if args.debug:
0041         log_level = logging.DEBUG
0042 
0043     # Check logfile path
0044     if args.logfile != "":
0045         log_dir_name = os.path.dirname(args.logfile)
0046 
0047         if log_dir_name != "" and not os.path.isdir(log_dir_name):
0048             os.mkdir(log_dir_name, 0o755)
0049 
0050         # Write log in logfile
0051         logging.basicConfig(
0052             filename=args.logfile,
0053             format=("%(levelname)s (%(module)s): %(message)s"),
0054             level=log_level,
0055         )
0056     else:
0057         # Write log to terminal
0058         logging.basicConfig(
0059             format=("%(levelname)s (%(module)s): %(message)s"), level=log_level
0060         )
0061 
0062     logging.info(
0063         "\n--------------------------------------------------------\n"
0064         "Running "
0065         + prog_name
0066         + " "
0067         + str(datetime.now().strftime("%d/%m/%Y %H:%M"))
0068         + "\n--------------------------------------------------------\n"
0069     )
0070 
0071     return logging