Skip to content

Commit

Permalink
check_compliance.py: simplify init_logs()
Browse files Browse the repository at this point in the history
As suggested by Ulf Magnusson in the previous PR zephyrproject-rtos#18, move all the
logging initialization in the init_logs() function.

Remove the switch/case parsing the log level name because Python does
that for free.
  • Loading branch information
marc-hb authored and nashif committed Mar 2, 2019
1 parent 1dbe2f4 commit e59e122
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions scripts/ci/check_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,33 +467,31 @@ def run(self):
self.case.result._elem.text = failure


def init_logs():
def init_logs(cli_arg):

"""
Initialize Logging
:return:
"""

# TODO: there may be a shorter version with something like:
# logging.basicConfig(level=getattr(logging, args.loglevel))
# TODO: there may be a shorter version thanks to:
# logging.basicConfig(...)

global logger
log_lev = os.environ.get('LOG_LEVEL', None)
level = logging.WARN
if log_lev == "DEBUG":
level = logging.DEBUG
elif log_lev == "ERROR":
level = logging.ERROR

level = os.environ.get('LOG_LEVEL', "WARN")

console = logging.StreamHandler()
format = logging.Formatter('%(levelname)-8s: %(message)s')
console.setFormatter(format)

logger = logging.getLogger('')
logger.addHandler(console)
logger.setLevel(level)
logger.setLevel(cli_arg if cli_arg else level)

logging.debug("Log init completed")
logging.info("Log init completed, level=%s",
logging.getLevelName(logger.getEffectiveLevel()))



Expand Down Expand Up @@ -666,12 +664,9 @@ def main():
:return:
"""

init_logs()
args = parse_args()

if args.loglevel:
logger.setLevel(level=args.loglevel)
# else: os.environ.get('LOG_LEVEL'); see init_logs()
init_logs(args.loglevel)

if args.list:
for testcase in ComplianceTest.__subclasses__():
Expand Down Expand Up @@ -724,6 +719,10 @@ def main():

failed_cases = []

# TODO maybe: move all the github-related code to a different .py
# file to draw a better line between developer code versus
# infrastructure-specific code, in other words keep this file
# 100% testable and maintainable by non-admins developers.
if args.github and 'GH_TOKEN' in os.environ:
errors = report_to_github(args.repo, args.pull_request, args.sha, suite, docs)
else:
Expand Down

0 comments on commit e59e122

Please sign in to comment.