Skip to content

Commit

Permalink
Fix double logger adding - master (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
lekcyjna123 committed May 5, 2024
1 parent b900c88 commit 677ed53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 4 additions & 4 deletions test/transactron/testing/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def proc():

print(caplog.text)
assert (
"WARNING test_logger:logging.py:87 test/transactron/testing/test_log.py:22] "
"WARNING test_logger:logging.py:83 [test/transactron/testing/test_log.py:22] "
+ "Log triggered under Amaranth If value+3=0x2d"
in caplog.text
)
for i in range(0, 50, 2):
expected_msg = (
"WARNING test_logger:logging.py:87 test/transactron/testing/test_log.py:24] "
"WARNING test_logger:logging.py:83 [test/transactron/testing/test_log.py:24] "
+ f"Input is even! input={i}, counter={i + 2}"
)
assert expected_msg in caplog.text
Expand All @@ -101,7 +101,7 @@ def proc():
sim.add_sync_process(proc)

extected_out = (
"ERROR test_logger:logging.py:87 test/transactron/testing/test_log.py:41] "
"ERROR test_logger:logging.py:83 [test/transactron/testing/test_log.py:41] "
+ "Input is different than output! input=0x1 output=0x0"
)
assert extected_out in caplog.text
Expand All @@ -117,5 +117,5 @@ def proc():
with self.run_simulation(m) as sim:
sim.add_sync_process(proc)

extected_out = "ERROR test_logger:logging.py:87 test/transactron/testing/test_log.py:62] Output differs"
extected_out = "ERROR test_logger:logging.py:83 [test/transactron/testing/test_log.py:62] Output differs"
assert extected_out in caplog.text
13 changes: 11 additions & 2 deletions transactron/testing/infrastructure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import pytest
import logging
import os
import random
import functools
Expand All @@ -13,7 +14,7 @@
from .testbenchio import TestbenchIO
from .profiler import profiler_process, Profile
from .functions import TestGen
from .logging import make_logging_process, parse_logging_level
from .logging import make_logging_process, parse_logging_level, _LogFormatter
from .gtkw_extension import write_vcd_ext
from transactron import Method
from transactron.lib import AdapterTrans
Expand Down Expand Up @@ -205,6 +206,14 @@ def run(self) -> bool:
class TestCaseWithSimulator:
dependency_manager: DependencyManager

@pytest.fixture(scope="session")
def register_logging_handler(self):
root_logger = logging.getLogger()
ch = logging.StreamHandler()
formatter = _LogFormatter()
ch.setFormatter(formatter)
root_logger.handlers += [ch]

@pytest.fixture(autouse=True)
def configure_dependency_context(self, request):
self.dependency_manager = DependencyManager()
Expand Down Expand Up @@ -264,7 +273,7 @@ def f():
profile.encode(f"{profile_dir}/{profile_file}.json")

@pytest.fixture(autouse=True)
def configure_logging(self, fixture_sim_processes_to_add):
def configure_logging(self, fixture_sim_processes_to_add, register_logging_handler):
def on_error():
assert False, "Simulation finished due to an error"

Expand Down
6 changes: 1 addition & 5 deletions transactron/testing/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def make_logging_process(level: tlog.LogLevel, namespace_regexp: str, on_error:
records = tlog.get_log_records(level, namespace_regexp)

root_logger = logging.getLogger()
ch = logging.StreamHandler()
formatter = _LogFormatter()
ch.setFormatter(formatter)
root_logger.handlers += [ch]

def handle_logs():
if not (yield combined_trigger):
Expand All @@ -86,7 +82,7 @@ def handle_logs():
logger = root_logger.getChild(record.logger_name)
logger.log(
record.level,
"%s:%d] %s",
"[%s:%d] %s",
record.location[0],
record.location[1],
formatted_msg,
Expand Down

0 comments on commit 677ed53

Please sign in to comment.