Repository: cocotb/cocotb

Issue:
Cleanup logger names
As a follow-on to #2953, there are some logger names that need some cleanup.

#2875 changed the GPI logger from `cocotb.gpi` to `gpi`, but a few places that don't use the `LOG_*` macros need to be updated.

The `Task` logger name is `cocotb.coroutine.Task <#>.<address of Task object>`.
I think it could be improved by removing `coroutine` and the object id. It may be useful to add the wrapped coroutine name.


Discussion:

Resolving patch:
diff --git a/cocotb/decorators.py b/cocotb/decorators.py
--- a/cocotb/decorators.py
+++ b/cocotb/decorators.py
@@ -101,8 +101,7 @@ def __init__(self, inst):
             )
         else:
             raise TypeError(
-                "%s isn't a valid coroutine! Did you forget to use the yield keyword?"
-                % inst
+                f"{inst} isn't a valid coroutine! Did you forget to use the yield keyword?"
             )
         self._coro = inst
         self._started = False
@@ -119,7 +118,7 @@ def __init__(self, inst):
     def log(self) -> SimLog:
         # Creating a logger is expensive, only do it if we actually plan to
         # log anything
-        return SimLog("cocotb.coroutine.%s" % self.__qualname__, id(self))
+        return SimLog(f"cocotb.{self.__qualname__}.{self._coro.__qualname__}")
 
     @property
     def retval(self) -> T:
@@ -360,6 +359,11 @@ class RunningTest(RunningCoroutine[T]):
 
     _name: str = "Test"
 
+    def __init__(self, inst, parent):
+        super().__init__(inst, parent)
+        self.__name__ = f"{type(self)._name} {self.funcname}"
+        self.__qualname__ = self.__name__
+
 
 class coroutine:
     """Decorator class that allows us to provide common coroutine mechanisms:
@@ -377,7 +381,7 @@ def __init__(self, func):
 
     @lazy_property
     def log(self):
-        return SimLog("cocotb.coroutine.%s" % self._func.__qualname__, id(self))
+        return SimLog(f"cocotb.coroutine.{self._func.__qualname__}.{id(self)}")
 
     def __call__(self, *args, **kwargs):
         return RunningCoroutine(self._func(*args, **kwargs), self)
@@ -409,7 +413,7 @@ def __init__(self, func):
 
     @lazy_property
     def log(self):
-        return SimLog("cocotb.function.%s" % self._coro.__qualname__, id(self))
+        return SimLog(f"cocotb.function.{self._coro.__qualname__}.{id(self)}")
 
     def __call__(self, *args, **kwargs):
         return cocotb.scheduler._queue_function(self._coro(*args, **kwargs))
@@ -432,7 +436,7 @@ class external:
 
     def __init__(self, func):
         self._func = func
-        self._log = SimLog("cocotb.external.%s" % self._func.__qualname__, id(self))
+        self._log = SimLog(f"cocotb.external.{self._func.__qualname__}.{id(self)}")
 
     def __call__(self, *args, **kwargs):
         return cocotb.scheduler._run_in_executor(self._func, *args, **kwargs)


Test patch:
diff --git a/tests/test_cases/test_cocotb/test_scheduler.py b/tests/test_cases/test_cocotb/test_scheduler.py
--- a/tests/test_cases/test_cocotb/test_scheduler.py
+++ b/tests/test_cases/test_cocotb/test_scheduler.py
@@ -478,6 +478,24 @@ async def coroutine_timer():
     log.info(repr(coro_task))
     assert re.match(r"<Task \d+ created coro=coroutine_outer\(\)>", repr(coro_task))
 
+    log.info(str(coro_task))
+    assert re.match(r"<Task \d+>", str(coro_task))
+
+
+@cocotb.test()
+async def test_test_repr(_):
+    """Test RunningTest.__repr__"""
+    log = logging.getLogger("cocotb.test")
+
+    current_test = cocotb.scheduler._test
+    log.info(repr(current_test))
+    assert re.match(
+        r"<Test test_test_repr running coro=test_test_repr\(\)>", repr(current_test)
+    )
+
+    log.info(str(current_test))
+    assert re.match(r"<Test test_test_repr>", str(current_test))
+
 
 @cocotb.test()
 async def test_start_soon_async(_):