pythonpython-3.xdatetimeloggingpython-can

Python-can Logging File Time


I just got started with Python-can and I need to edit the reporting format of the built in Logging feature. Below is a snippet of the code that I am referring too.

logger = can.Logger('log3.asc')

This is the resulting header from the log file:

date Mon Jun 06 04:56:40.184122 PM 2020
base hex  timestamps absolute
internal events logged
Begin Triggerblock Wed Dec 12 10:17:48.038 PM 1969
0.000000 Start of measurement
0.000000 1  300             Rx   d 6 00 00 14 CF BE FF

The issue is in the first line, I can't have the time reported in microseconds, milliseconds is the furthest I can go. Is there anyway to edit that value within python-can? Or does this require extra code to manually go into the log file and stripe those values?

Thank you for the time,


Solution

  • I think you cannot change it. In the source code of python-can, it use %f to generate microsecond and no constant variable there for redefinition.

    class ASCWriter(BaseIOHandler, Listener):
        ...
        def __init__(self, file, channel=1):
            ...
            # write start of file header
            now = datetime.now().strftime("%a %b %m %I:%M:%S.%f %p %Y")
            self.file.write("date %s\n" % now)
            self.file.write("base hex  timestamps absolute\n")
            self.file.write("internal events logged\n")
            ...
    

    If you try to change the format of %f in datetime.strftime, the same situation, it set the digit as '%06d', also no constant variable there for redifinition.

    def _wrap_strftime(object, format, timetuple):
        ...
                    if ch == 'f':
                        if freplace is None:
                            freplace = '%06d' % getattr(object,
                                                        'microsecond', 0)
        ...
    

    I think the only way is to change the source code of py-can or datetime, but it is not suggested !!