Skip to content

wrong justing of ID column in __str__ method of Message class #1630

Closed
@michalPradzynski

Description

@michalPradzynski

Describe the bug

We are using __ str __ method of Message class in the logging of our project and found out that when there are frames with short and extended Arbitration IDs, short ID ("ID: dddd") is justed to the right of its' column which makes the ID column distorted.
The "ID: " part for both short and extended AIDs is not in the same vertical line.

To Reproduce

  • create a logger which logs each CAN frame with Arbitration IDs with different lengths as str(can.Message)
  • create a virtual CAN bus
  • send some CAN frames

Expected behavior

Change arbitration_id_string.rjust() to arbitration_id_string.ljust in line 116 of can/message.py

python-can/can/message.py

Lines 110 to 116 in c5d7a7c

def __str__(self) -> str:
field_strings = [f"Timestamp: {self.timestamp:>15.6f}"]
if self.is_extended_id:
arbitration_id_string = f"ID: {self.arbitration_id:08x}"
else:
arbitration_id_string = f"ID: {self.arbitration_id:04x}"
field_strings.append(arbitration_id_string.rjust(12, " "))

Additional context

OS and version: Windows 10 Enterprise 21H2
Python version: 3.10
python-can version: 4.1.0

Traceback and logs

How the logs look right now:
obraz

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions