Skip to content

Commit

Permalink
twister: setup logging per process
Browse files Browse the repository at this point in the history
Signed-off-by: Anas Nashif <anas.nashif@intel.com>
  • Loading branch information
nashif committed Feb 25, 2025
1 parent 7127757 commit 758c2ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
37 changes: 37 additions & 0 deletions scripts/pylib/twister/twisterlib/log_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

import platform
import shlex
import logging
import os

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

Check failure on line 13 in scripts/pylib/twister/twisterlib/log_helper.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

Python lint error (I001) see https://docs.astral.sh/ruff/rules/unsorted-imports

scripts/pylib/twister/twisterlib/log_helper.py:8 Import block is un-sorted or un-formatted

Check warning on line 13 in scripts/pylib/twister/twisterlib/log_helper.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

C0325

scripts/pylib/twister/twisterlib/log_helper.py:13 Unnecessary parens after '=' keyword (superfluous-parens)


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

def log_command(logger, msg, args):
'''Platform-independent helper for logging subprocess invocations.
Will log a command string that can be copy/pasted into a POSIX
Expand All @@ -25,3 +31,34 @@ 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:
fh = logging.FileHandler(log_file)
else:
fh = logging.FileHandler(os.path.join(outdir, "twister.log"))

fh.setLevel(logging.DEBUG)

# create console handler with a higher log level
ch = logging.StreamHandler()
ch.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"
)
ch.setFormatter(formatter)
fh.setFormatter(formatter_file)

# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)

Check failure on line 64 in scripts/pylib/twister/twisterlib/log_helper.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

Python lint error (W292) see https://docs.astral.sh/ruff/rules/missing-newline-at-end-of-file

scripts/pylib/twister/twisterlib/log_helper.py:64 No newline at end of file

Check warning on line 64 in scripts/pylib/twister/twisterlib/log_helper.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

C0304

scripts/pylib/twister/twisterlib/log_helper.py:64 Final newline missing (missing-final-newline)
6 changes: 5 additions & 1 deletion scripts/pylib/twister/twisterlib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
from twisterlib.environment import canonical_zephyr_base
from twisterlib.error import BuildError, ConfigurationError, StatusAttributeError
from twisterlib.statuses import TwisterStatus
from twisterlib.log_helper import setup_logging


if version.parse(elftools.__version__) < version.parse('0.24'):

Check failure on line 36 in scripts/pylib/twister/twisterlib/runner.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

Python lint error (I001) see https://docs.astral.sh/ruff/rules/unsorted-imports

scripts/pylib/twister/twisterlib/runner.py:7 Import block is un-sorted or un-formatted
sys.exit("pyelftools is out of date, need version 0.24 or later")
Expand Down Expand Up @@ -978,7 +980,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
30 changes: 1 addition & 29 deletions scripts/pylib/twister/twisterlib/twister_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,12 @@
from twisterlib.runner import TwisterRunner
from twisterlib.statuses import TwisterStatus
from twisterlib.testplan import TestPlan
from twisterlib.log_helper import setup_logging

logger = logging.getLogger("twister")

Check failure on line 25 in scripts/pylib/twister/twisterlib/twister_main.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

Python lint error (I001) see https://docs.astral.sh/ruff/rules/unsorted-imports

scripts/pylib/twister/twisterlib/twister_main.py:6 Import block is un-sorted or un-formatted
logger.setLevel(logging.DEBUG)


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

fh.setLevel(logging.DEBUG)

# create console handler with a higher log level
ch = logging.StreamHandler()
ch.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"
)
ch.setFormatter(formatter)
fh.setFormatter(formatter_file)

# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)


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,

Check failure on line 1485 in scripts/tests/twister/test_runner.py

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

Python lint error (W191) see https://docs.astral.sh/ruff/rules/tab-indentation

scripts/tests/twister/test_runner.py:1485 Indentation contains tabs
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 758c2ec

Please sign in to comment.