Search code examples
cocotb

How to print log message with cocotb


On the cocotb official quick start guide the method to print log message is to use _log.info() on the dut object :

import cocotb
from cocotb.triggers import Timer

@cocotb.test()
def my_first_test(dut):
    """
    Try accessing the design
    """
    dut._log.info("Running test!")
    for cycle in range(10):
        dut.clk = 0
        yield Timer(1000)
        dut.clk = 1
        yield Timer(1000)
    dut._log.info("Running test!")

If I do that with last master version of Cocotb I have a deprecated warning :

/opt/cocotb/cocotb/handle.py:134: UserWarning: Use of log attribute is deprecated

Then what is the good way to log information on last version of cocotb ?

thx


Solution

  • From the latest version it looks like _log is the appropriate attribute to be using to get at the logger.

    I don't think it's an issue with the actual code example you've pasted itself, but perhaps somewhere else in cocotb which is using the deprecated log attribute.

    I was seeing this myself, actually, and used a crude method to pinpoint where the calls were coming from by using the traceback module and modifying the __getattr__ and __setattr__ functions in the SimHandleBase class in cocotb/handle.py like so:

    import traceback
    class SimHandleBase(object):
    
    ...
    
        def __setattr__(self, name, value):
            if name in self._compat_mapping:
                if name not in _deprecation_warned:
                    warnings.warn("Use of %s attribute is deprecated" % name)
                    for line in traceback.format_stack():     # Inserted to print stack trace
                        print(line.strip())                   # Inserted to print stack trace
                    _deprecation_warned[name] = True
                return setattr(self, self._compat_mapping[name], value)
            else:
                return object.__setattr__(self, name, value)
    
        def __getattr__(self, name):
            if name in self._compat_mapping:
                if name not in _deprecation_warned:
                    warnings.warn("Use of %s attribute is deprecated" % name)
                    for line in traceback.format_stack():     # Inserted to print stack trace
                        print(line.strip())                   # Inserted to print stack trace
                    _deprecation_warned[name] = True
                return getattr(self, self._compat_mapping[name])
            else:
                return object.__getattr__(self, name)