Skip to content

Commit

Permalink
twister: setup logging per process
Browse files Browse the repository at this point in the history
Setup logging per process to fix issue on both mac and windows where
handlers are not available to the processes.

Fixes #86237

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
  • Loading branch information
nashif authored and kartben committed Feb 28, 2025
1 parent 3a996c5 commit a7e2846
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 33 deletions.
37 changes: 36 additions & 1 deletion scripts/pylib/twister/twisterlib/log_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
Common code used when logging that is needed by multiple modules.
'''

import logging
import os
import platform
import shlex

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


logger = logging.getLogger("twister")
logger.setLevel(logging.DEBUG)

def log_command(logger, msg, args):
'''Platform-independent helper for logging subprocess invocations.
Expand All @@ -25,3 +31,32 @@ def log_command(logger, msg, args):
logger.debug(msg, str(args))
else:
logger.debug(msg, shlex.join(args))

def setup_logging(outdir, log_file, log_level, timestamps):
# create file handler which logs even debug messages
if log_file:
file_handler = logging.FileHandler(log_file)
else:
file_handler = logging.FileHandler(os.path.join(outdir, "twister.log"))

file_handler.setLevel(logging.DEBUG)

# create console handler with a higher log level
console_handler = logging.StreamHandler()
console_handler.setLevel(getattr(logging, log_level))

# create formatter and add it to the handlers
if timestamps:
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
else:
formatter = logging.Formatter("%(levelname)-7s - %(message)s")

formatter_file = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter_file)

# add the handlers to logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
5 changes: 4 additions & 1 deletion scripts/pylib/twister/twisterlib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from twisterlib.cmakecache import CMakeCache
from twisterlib.environment import canonical_zephyr_base
from twisterlib.error import BuildError, ConfigurationError, StatusAttributeError
from twisterlib.log_helper import setup_logging
from twisterlib.statuses import TwisterStatus

if version.parse(elftools.__version__) < version.parse('0.24'):
Expand Down Expand Up @@ -978,7 +979,9 @@ def process(self, pipeline, done, message, lock, results):
additionals = {}

op = message.get('op')

options = self.options
if not logger.handlers:
setup_logging(options.outdir, options.log_file, options.log_level, options.timestamps)
self.instance.setup_handler(self.env)

if op == "filter":
Expand Down
32 changes: 1 addition & 31 deletions scripts/pylib/twister/twisterlib/twister_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from twisterlib.coverage import run_coverage
from twisterlib.environment import TwisterEnv
from twisterlib.hardwaremap import HardwareMap
from twisterlib.log_helper import setup_logging
from twisterlib.package import Artifacts
from twisterlib.reports import Reporting
from twisterlib.runner import TwisterRunner
Expand All @@ -24,37 +25,6 @@
logger = logging.getLogger("twister")
logger.setLevel(logging.DEBUG)


def setup_logging(outdir, log_file, log_level, timestamps):
# create file handler which logs even debug messages
if log_file:
file_handler = logging.FileHandler(log_file)
else:
file_handler = logging.FileHandler(os.path.join(outdir, "twister.log"))

file_handler.setLevel(logging.DEBUG)

# create console handler with a higher log level
console_handler = logging.StreamHandler()
console_handler.setLevel(getattr(logging, log_level))

# create formatter and add it to the handlers
if timestamps:
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
else:
formatter = logging.Formatter("%(levelname)-7s - %(message)s")

formatter_file = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter_file)

# add the handlers to logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)


def init_color(colorama_strip):
colorama.init(strip=colorama_strip)

Expand Down
4 changes: 4 additions & 0 deletions scripts/tests/twister/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ def mock_getsize(filename, *args, **kwargs):
def test_projectbuilder_process(
caplog,
mocked_jobserver,
tmp_path,
message,
instance_status,
instance_reason,
Expand Down Expand Up @@ -1529,6 +1530,9 @@ def mock_determine_testcases(res):
pb.options.prep_artifacts_for_testing = options_prep_artifacts
pb.options.runtime_artifact_cleanup = options_runtime_artifacts
pb.options.cmake_only = options_cmake_only
pb.options.outdir = tmp_path
pb.options.log_file = None
pb.options.log_level = "DEBUG"

pb.cmake = mock.Mock(return_value=cmake_res)
pb.build = mock.Mock(return_value=build_res)
Expand Down

0 comments on commit a7e2846

Please sign in to comment.