Skip to content

Commit

Permalink
Added pretty_index on logging entries
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinse committed Jun 4, 2024
1 parent 7129fed commit 883af8a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion canopen/node/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from canopen.nmt import NmtSlave
from canopen.emcy import EmcyProducer
from canopen.objectdictionary import ObjectDictionary
from canopen.utils import pretty_index
from canopen import objectdictionary

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -86,7 +87,7 @@ def get_data(
return obj.encode_raw(obj.default)

# Resource not available
logger.info("Resource unavailable for 0x%04X:%02X", index, subindex)
logger.info("Resource unavailable for %s", pretty_index(index, subindex))
raise SdoAbortedError(0x060A0023)

def set_data(
Expand Down
13 changes: 7 additions & 6 deletions canopen/node/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from canopen.pdo import TPDO, RPDO, PDO
from canopen.objectdictionary import ODRecord, ODArray, ODVariable, ObjectDictionary
from canopen.node.base import BaseNode
from canopen.utils import pretty_index

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -122,13 +123,13 @@ def __load_configuration_helper(self, index, subindex, name, value):
"""
try:
if subindex is not None:
logger.info('SDO [0x%04X][0x%02X]: %s: %#06x',
index, subindex, name, value)
logger.info('SDO [%s]: %s: %#06x',
pretty_index(index, subindex), name, value)
self.sdo[index][subindex].raw = value
else:
self.sdo[index].raw = value
logger.info('SDO [0x%04X]: %s: %#06x',
index, name, value)
logger.info('SDO [%s]: %s: %#06x',
pretty_index(index), name, value)
except SdoCommunicationError as e:
logger.warning(str(e))
except SdoAbortedError as e:
Expand All @@ -137,8 +138,8 @@ def __load_configuration_helper(self, index, subindex, name, value):
if e.code != 0x06010002:
# Abort codes other than "Attempt to write a read-only object"
# should still be reported.
logger.warning('[ERROR SETTING object 0x%04X:%02X] %s',
index, subindex, e)
logger.warning('[ERROR SETTING object %s] %s',
pretty_index(index, subindex), e)
raise

def load_configuration(self):
Expand Down
9 changes: 5 additions & 4 deletions canopen/pdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from canopen.sdo import SdoAbortedError
from canopen import objectdictionary
from canopen import variable
from canopen.utils import pretty_index

PDO_NOT_VALID = 1 << 31
RTR_NOT_ALLOWED = 1 << 30
Expand Down Expand Up @@ -398,8 +399,8 @@ def save(self) -> None:
self._fill_map(self.map_array[0].raw)
subindex = 1
for var in self.map:
logger.info("Writing %s (0x%04X:%02X, %d bits) to PDO map",
var.name, var.index, var.subindex, var.length)
logger.info("Writing %s (%s, %d bits) to PDO map",
var.name, pretty_index(var.index, var.subindex), var.length)
if hasattr(self.pdo_node.node, "curtis_hack") and self.pdo_node.node.curtis_hack: # Curtis HACK: mixed up field order
self.map_array[subindex].raw = (var.index |
var.subindex << 16 |
Expand Down Expand Up @@ -467,8 +468,8 @@ def add_variable(
# We want to see the bit fields within the PDO
start_bit = var.offset
end_bit = start_bit + var.length - 1
logger.info("Adding %s (0x%04X:%02X) at bits %d - %d to PDO map",
var.name, var.index, var.subindex, start_bit, end_bit)
logger.info("Adding %s (%s) at bits %d - %d to PDO map",
var.name, pretty_index(var.index, var.subindex), start_bit, end_bit)
self.map.append(var)
self.length += var.length
except KeyError as exc:
Expand Down
5 changes: 3 additions & 2 deletions canopen/profiles/p402.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from canopen.node import RemoteNode
from canopen.sdo import SdoCommunicationError
from canopen.utils import pretty_index

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -249,7 +250,7 @@ def _init_tpdo_values(self):
if tpdo.enabled:
tpdo.add_callback(self.on_TPDOs_update_callback)
for obj in tpdo:
logger.debug('Configured TPDO: %s', obj.index)
logger.debug('Configured TPDO: %s', pretty_index(obj.index))
if obj.index not in self.tpdo_values:
self.tpdo_values[obj.index] = 0
self.tpdo_pointers[obj.index] = obj
Expand All @@ -260,7 +261,7 @@ def _init_rpdo_pointers(self):
for rpdo in self.rpdo.values():
if rpdo.enabled:
for obj in rpdo:
logger.debug('Configured RPDO: %s', obj.index)
logger.debug('Configured RPDO: %s', pretty_index(obj.index))
if obj.index not in self.rpdo_pointers:
self.rpdo_pointers[obj.index] = obj

Expand Down
6 changes: 3 additions & 3 deletions canopen/sdo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def __init__(self, sdo_client, index, subindex=0):
self._toggle = 0
self.pos = 0

logger.debug("Reading 0x%04X:%02X from node %d", index, subindex,
logger.debug("Reading %s from node %d", pretty_index(index, subindex),
sdo_client.rx_cobid - 0x600)
request = bytearray(8)
SDO_STRUCT.pack_into(request, 0, REQUEST_UPLOAD, index, subindex)
Expand Down Expand Up @@ -474,7 +474,7 @@ def __init__(self, sdo_client, index, subindex=0, request_crc_support=True):
self._ackseq = 0
self._error = False

logger.debug("Reading 0x%04X:%02X from node %d", index, subindex,
logger.debug("Reading %s from node %d", pretty_index(index, subindex),
sdo_client.rx_cobid - 0x600)
# Initiate Block Upload
request = bytearray(8)
Expand Down Expand Up @@ -644,7 +644,7 @@ def __init__(self, sdo_client, index, subindex=0, size=None, request_crc_support
if request_crc_support:
command |= CRC_SUPPORTED
request = bytearray(8)
logger.info("Initiating block download for 0x%04X:%02X", index, subindex)
logger.info("Initiating block download for %s", pretty_index(index, subindex))
if size is not None:
logger.debug("Expected size of data is %d bytes", size)
command |= BLOCK_SIZE_SPECIFIED
Expand Down
12 changes: 7 additions & 5 deletions canopen/sdo/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from canopen.sdo.base import SdoBase
from canopen.sdo.constants import *
from canopen.sdo.exceptions import *
from canopen.utils import pretty_index

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,12 +67,12 @@ def init_upload(self, request):
data = self._node.get_data(index, subindex, check_readable=True)
size = len(data)
if size <= 4:
logger.info("Expedited upload for 0x%04X:%02X", index, subindex)
logger.info("Expedited upload for %s", pretty_index(index, subindex))
res_command |= EXPEDITED
res_command |= (4 - size) << 2
response[4:4 + size] = data
else:
logger.info("Initiating segmented upload for 0x%04X:%02X", index, subindex)
logger.info("Initiating segmented upload for %s", pretty_index(index, subindex))
struct.pack_into("<L", response, 4, size)
self._buffer = bytearray(data)
self._toggle = 0
Expand Down Expand Up @@ -115,7 +116,8 @@ def block_upload(self, data):
def request_aborted(self, data):
_, index, subindex, code = struct.unpack_from("<BHBL", data)
self.last_received_error = code
logger.info("Received request aborted for 0x%04X:%02X with code 0x%X", index, subindex, code)
logger.info("Received request aborted for %s with code 0x%X",
pretty_index(index, subindex), code)

def block_download(self, data):
# We currently don't support BLOCK DOWNLOAD
Expand All @@ -131,14 +133,14 @@ def init_download(self, request):
response = bytearray(8)

if command & EXPEDITED:
logger.info("Expedited download for 0x%04X:%02X", index, subindex)
logger.info("Expedited download for %s", pretty_index(index, subindex))
if command & SIZE_SPECIFIED:
size = 4 - ((command >> 2) & 0x3)
else:
size = 4
self._node.set_data(index, subindex, request[4:4 + size], check_writable=True)
else:
logger.info("Initiating segmented download for 0x%04X:%02X", index, subindex)
logger.info("Initiating segmented download for %s", pretty_index(index, subindex))
if command & SIZE_SPECIFIED:
size, = struct.unpack_from("<L", request, 4)
logger.info("Size is %d bytes", size)
Expand Down
5 changes: 2 additions & 3 deletions canopen/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ def raw(self) -> Union[int, bool, float, str, bytes]:

@raw.setter
def raw(self, value: Union[int, bool, float, str, bytes]):
logger.debug("Writing %r (0x%04X:%02X) = %r",
self.name, self.index,
self.subindex, value)
logger.debug("Writing %r (%s) = %r",
self.name, pretty_index(self.index, self.subindex), value)
self.data = self.od.encode_raw(value)

@property
Expand Down

0 comments on commit 883af8a

Please sign in to comment.