Skip to content

Commit ffe72a4

Browse files
LukaszMrugalafabiobaltieri
authored andcommitted
scripts: Add invariant log closing
If BB tests failed, crashed, etc., log handles could linger and crash test cleanup. Adding log closing to a `finally` section should prevent those issues. Loggers should not be set up as globals, as it makes testing much harder. Running multiple Twisters may cause for the logfiles to be still "in use". When exiting main, close all logfiles and remove their handlers from all loggers. Do that for conftest as well. Signed-off-by: Lukasz Mrugala <[email protected]>
1 parent 5ba659c commit ffe72a4

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

scripts/pylib/twister/twisterlib/log_helper.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212

1313
_WINDOWS = platform.system() == 'Windows'
1414

15-
16-
logger = logging.getLogger("twister")
17-
logger.setLevel(logging.DEBUG)
18-
1915
def log_command(logger, msg, args):
2016
'''Platform-independent helper for logging subprocess invocations.
2117
Will log a command string that can be copy/pasted into a POSIX
@@ -26,13 +22,19 @@ def log_command(logger, msg, args):
2622
:param msg: message to associate with the command
2723
:param args: argument list as passed to subprocess module
2824
'''
25+
logger = logging.getLogger("twister")
26+
logger.setLevel(logging.DEBUG)
27+
2928
msg = f'{msg}: %s'
3029
if _WINDOWS:
3130
logger.debug(msg, str(args))
3231
else:
3332
logger.debug(msg, shlex.join(args))
3433

3534
def setup_logging(outdir, log_file, log_level, timestamps):
35+
logger = logging.getLogger("twister")
36+
logger.setLevel(logging.DEBUG)
37+
3638
# create file handler which logs even debug messages
3739
if log_file:
3840
file_handler = logging.FileHandler(log_file)
@@ -60,3 +62,19 @@ def setup_logging(outdir, log_file, log_level, timestamps):
6062
# add the handlers to logger
6163
logger.addHandler(console_handler)
6264
logger.addHandler(file_handler)
65+
66+
67+
def close_logging():
68+
logger = logging.getLogger("twister")
69+
handlers = logger.handlers[:]
70+
71+
for handler in handlers:
72+
logger.removeHandler(handler)
73+
handler.close()
74+
75+
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
76+
for logg in loggers:
77+
handls = logg.handlers[:]
78+
for handl in handls:
79+
logg.removeHandler(handl)
80+
handl.close()

scripts/pylib/twister/twisterlib/twister_main.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@
1515
from twisterlib.coverage import run_coverage
1616
from twisterlib.environment import TwisterEnv
1717
from twisterlib.hardwaremap import HardwareMap
18-
from twisterlib.log_helper import setup_logging
18+
from twisterlib.log_helper import close_logging, setup_logging
1919
from twisterlib.package import Artifacts
2020
from twisterlib.reports import Reporting
2121
from twisterlib.runner import TwisterRunner
2222
from twisterlib.statuses import TwisterStatus
2323
from twisterlib.testplan import TestPlan
2424

25-
logger = logging.getLogger("twister")
26-
logger.setLevel(logging.DEBUG)
2725

2826
def init_color(colorama_strip):
2927
colorama.init(strip=colorama_strip)
3028

3129

32-
def main(options: argparse.Namespace, default_options: argparse.Namespace):
30+
def twister(options: argparse.Namespace, default_options: argparse.Namespace):
3331
start_time = time.time()
3432

3533
# Configure color output
@@ -78,6 +76,7 @@ def main(options: argparse.Namespace, default_options: argparse.Namespace):
7876
fp.write(previous_results)
7977

8078
setup_logging(options.outdir, options.log_file, options.log_level, options.timestamps)
79+
logger = logging.getLogger("twister")
8180

8281
env = TwisterEnv(options, default_options)
8382
env.discover()
@@ -222,3 +221,11 @@ def main(options: argparse.Namespace, default_options: argparse.Namespace):
222221

223222
logger.info("Run completed")
224223
return 0
224+
225+
226+
def main(options: argparse.Namespace, default_options: argparse.Namespace):
227+
try:
228+
return_code = twister(options, default_options)
229+
finally:
230+
close_logging()
231+
return return_code

scripts/tests/twister_blackbox/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,10 @@ def provide_out(tmp_path, request):
8484
# After
8585
# We're operating in temp, so it is not strictly necessary
8686
# but the files can get large quickly as we do not need them after the test.
87+
loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
88+
for logg in loggers:
89+
handls = logg.handlers[:]
90+
for handl in handls:
91+
logg.removeHandler(handl)
92+
handl.close()
8793
shutil.rmtree(out_container_path)

0 commit comments

Comments
 (0)