From e0c578405fda27ff3c8bff6c03cdb9aa1a5e5bf0 Mon Sep 17 00:00:00 2001 From: chrizog Date: Sat, 4 May 2024 17:05:50 +0200 Subject: [PATCH] Enable changing the library log level --- src/someipy/__init__.py | 8 ++++---- src/someipy/_internal/logging.py | 25 +++++++++++++++--------- src/someipy/_internal/someip_endpoint.py | 2 +- src/someipy/client_service_instance.py | 14 ++++++------- src/someipy/logging.py | 2 -- src/someipy/server_service_instance.py | 23 +++++++++++----------- src/someipy/service_discovery.py | 9 ++++----- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/someipy/__init__.py b/src/someipy/__init__.py index 8a7b7f0..e1fe4b0 100644 --- a/src/someipy/__init__.py +++ b/src/someipy/__init__.py @@ -1,4 +1,4 @@ -from ._internal.someip_sd_header import TransportLayerProtocol -from .server_service_instance import ServerServiceInstance, construct_server_service_instance -from .service import Service, ServiceBuilder, EventGroup -from ._internal.someip_message import SomeIpMessage \ No newline at end of file +from ._internal.someip_sd_header import TransportLayerProtocol # noqa: F401 +from .server_service_instance import ServerServiceInstance, construct_server_service_instance # noqa: F401 +from .service import Service, ServiceBuilder, EventGroup # noqa: F401 +from ._internal.someip_message import SomeIpMessage # noqa: F401 diff --git a/src/someipy/_internal/logging.py b/src/someipy/_internal/logging.py index 27f4a5f..d1cdcbc 100644 --- a/src/someipy/_internal/logging.py +++ b/src/someipy/_internal/logging.py @@ -1,16 +1,23 @@ import logging from someipy.logging import get_someipy_log_level -def get_logger(name: str) -> logging.Logger: - logger = logging.getLogger("someipy." + name) - logger.setLevel(get_someipy_log_level()) +def setup_console_handler(formatter: logging.Formatter, level: int) -> logging.StreamHandler: console_handler = logging.StreamHandler() - console_handler.setLevel(get_someipy_log_level()) - console_handler.setFormatter( - logging.Formatter( - "%(asctime)s.%(msecs)03d %(name)s [%(levelname)s]: %(message)s", - datefmt="%Y-%m-%d,%H:%M:%S", - ) + console_handler.setLevel(level) + console_handler.setFormatter(formatter) + return console_handler + +def get_logger(name: str) -> logging.Logger: + logger = logging.getLogger(f"someipy.{name}") + log_level = get_someipy_log_level() + + formatter = logging.Formatter( + "%(asctime)s.%(msecs)03d %(name)s [%(levelname)s]: %(message)s", + datefmt="%Y-%m-%d,%H:%M:%S", ) + + console_handler = setup_console_handler(formatter, log_level) logger.addHandler(console_handler) + logger.setLevel(log_level) + return logger diff --git a/src/someipy/_internal/someip_endpoint.py b/src/someipy/_internal/someip_endpoint.py index 3c30075..015ba11 100644 --- a/src/someipy/_internal/someip_endpoint.py +++ b/src/someipy/_internal/someip_endpoint.py @@ -57,7 +57,7 @@ def sendto(self, data: bytes, addr: EndpointType) -> None: self._transport.sendto(data, addr) def sendtoall(self, data: bytes) -> None: - # TODO: Implement later for multicast support + # TODO: Implement for multicast support raise NotImplementedError("No implementation for UDP yet") def shutdown(self) -> None: diff --git a/src/someipy/client_service_instance.py b/src/someipy/client_service_instance.py index 83357f7..0ecca46 100644 --- a/src/someipy/client_service_instance.py +++ b/src/someipy/client_service_instance.py @@ -23,8 +23,7 @@ SomeIpMessage, ) -_logger = get_logger("client_service_instance") - +_logger_name = "client_service_instance" class ExpectedAck: def __init__(self, eventgroup_id: int) -> None: @@ -83,7 +82,7 @@ def someip_message_received( def subscribe_eventgroup(self, eventgroup_id: int): if eventgroup_id in self._eventgroups_to_subscribe: - _logger.debug( + get_logger(_logger_name).debug( f"Eventgroup ID {eventgroup_id} is already in subscription list." ) self._eventgroups_to_subscribe.add(eventgroup_id) @@ -125,10 +124,10 @@ def offer_service_update(self, offered_service: SdService): session_id=session_id, reboot_flag=reboot_flag, endpoint=self._endpoint, - protocol=TransportLayerProtocol.UDP, + protocol=self._protocol, ) - _logger.debug( + get_logger(_logger_name).debug( f"Send subscribe for instance 0x{self._instance_id:04X}, service: 0x{self._service.id:04X}, " f"eventgroup ID: {eventgroup_to_subscribe} TTL: {self._ttl}, version: " f"session ID: {session_id}" @@ -152,7 +151,7 @@ def subscribe_ack_eventgroup_update( for expected_ack in self._expected_acks: if expected_ack.eventgroup_id == event_group_entry.eventgroup_id: ack_found = True - _logger.debug( + get_logger(_logger_name).debug( f"Received expected subscribe ACK for instance 0x{event_group_entry.sd_entry.instance_id:04X}, service 0x{event_group_entry.sd_entry.service_id:04X}, eventgroup 0x{event_group_entry.eventgroup_id:04X}" ) else: @@ -160,7 +159,7 @@ def subscribe_ack_eventgroup_update( self._expected_acks = new_acks if not ack_found: - _logger.warn( + get_logger(_logger_name).warn( f"Received unexpected subscribe ACK for instance 0x{event_group_entry.sd_entry.instance_id:04X}, service 0x{event_group_entry.sd_entry.service_id:04X}, eventgroup 0x{event_group_entry.eventgroup_id:04X}" ) @@ -196,6 +195,7 @@ async def construct_client_service_instance( return client_instance elif protocol == TransportLayerProtocol.TCP: + tcp_client_manager = TcpClientManager() loop = asyncio.get_running_loop() server = await loop.create_server( diff --git a/src/someipy/logging.py b/src/someipy/logging.py index 0d76925..d7218d3 100644 --- a/src/someipy/logging.py +++ b/src/someipy/logging.py @@ -2,12 +2,10 @@ _log_level = logging.DEBUG - def set_someipy_log_level(log_level: int): global _log_level _log_level = log_level - def get_someipy_log_level() -> int: global _log_level return _log_level diff --git a/src/someipy/server_service_instance.py b/src/someipy/server_service_instance.py index 7f7884c..dd2f7a4 100644 --- a/src/someipy/server_service_instance.py +++ b/src/someipy/server_service_instance.py @@ -38,8 +38,7 @@ UDPSomeipEndpoint, ) -_logger = get_logger("server_service_instance") - +_logger_name = "server_service_instance" class ServerServiceInstance(ServiceDiscoveryObserver): _service: Service @@ -95,7 +94,7 @@ def send_event(self, event_group_id: int, event_id: int, payload: bytes) -> None for sub in self._subscribers.subscribers: if sub.eventgroup_id == event_group_id: - _logger.debug( + get_logger(_logger_name).debug( f"Send event for instance 0x{self._instance_id:04X}, service: 0x{self._service.id:04X} to {sub.endpoint[0]}:{sub.endpoint[1]}" ) self._someip_endpoint.sendto( @@ -114,7 +113,7 @@ def send_response(): ) if header.service_id != self.service_id: - _logger.warn( + get_logger(_logger_name).warn( f"Unknown service ID received from {addr}: ID 0x{header.service_id:04X}" ) header_to_return.message_type = MessageType.RESPONSE.value @@ -123,7 +122,7 @@ def send_response(): return if header.method_id not in self.methods.keys(): - _logger.warn( + get_logger(_logger_name).warn( f"Unknown method ID received from {addr}: ID 0x{header.method_id:04X}" ) header_to_return.message_type = MessageType.RESPONSE.value @@ -143,19 +142,19 @@ def send_response(): ].method_handler(payload_in) if not success: - _logger.debug( + get_logger(_logger_name).debug( f"Return ERROR message type to {addr} for service and instance ID: 0x{self.service_id:04X} / 0x{self._instance_id:04X}" ) header_to_return.message_type = MessageType.ERROR.value else: - _logger.debug( + get_logger(_logger_name).debug( f"Return RESPONSE message type to {addr} for service and instance ID: 0x{self.service_id:04X} / 0x{self._instance_id:04X}" ) header_to_return.message_type = MessageType.RESPONSE.value send_response() else: - _logger.warn( + get_logger(_logger_name).warn( f"Unknown message type received from {addr}: Type 0x{header.message_type:04X}" ) @@ -185,7 +184,7 @@ def subscribe_eventgroup_update( return if ipv4_endpoint_option.protocol != self._protocol: - _logger.warn( + get_logger(_logger_name).warn( f"Subscribing a different protocol (TCP/UDP) than offered is not supported. Received subscribe for instance 0x{self._instance_id:04X}, service: 0x{self._service.id:04X} " "from {ipv4_endpoint_option.ipv4_address}/{ipv4_endpoint_option.port} with wrong protocol" ) @@ -196,7 +195,7 @@ def subscribe_eventgroup_update( reboot_flag, ) = self._sd_sender.get_unicast_session_handler().update_session() - _logger.debug( + get_logger(_logger_name).debug( f"Send Subscribe ACK for instance 0x{self._instance_id:04X}, service: 0x{self._service.id:04X}, TTL: {sd_event_group.sd_entry.ttl}" ) ack_entry = build_subscribe_eventgroup_ack_entry( @@ -236,7 +235,7 @@ def offer_timer_callback(self): reboot_flag, ) = self._sd_sender.get_multicast_session_handler().update_session() - _logger.debug( + get_logger(_logger_name).debug( f"Offer service for instance 0x{self._instance_id:04X}, service: 0x{self._service.id:04X}, TTL: {self._ttl}, version: {self._service.major_version}.{self._service.minor_version}, session ID: {session_id}" ) @@ -261,7 +260,7 @@ def start_offer(self): self._offer_timer.start() async def stop_offer(self): - _logger.debug( + get_logger(_logger_name).debug( f"Stop offer for instance 0x{self._instance_id:04X}, service: 0x{self._service.id:04X}" ) diff --git a/src/someipy/service_discovery.py b/src/someipy/service_discovery.py index 34b87d4..16080e7 100644 --- a/src/someipy/service_discovery.py +++ b/src/someipy/service_discovery.py @@ -22,8 +22,7 @@ ) from someipy._internal.logging import get_logger -_logger = get_logger("service_discovery") - +_logger_name = "service_discovery" class ServiceDiscoveryProtocol(ServiceDiscoverySubject, ServiceDiscoverySender): attached_observers: List[ServiceDiscoveryObserver] @@ -77,7 +76,7 @@ def datagram_received(self, data: bytes, addr: Tuple[Union[str, Any], int]) -> N someip_sd_header = SomeIpSdHeader.from_buffer(data) for offered_service in extract_offered_services(someip_sd_header): - _logger.debug( + get_logger(_logger_name).debug( f"Received offer for instance 0x{offered_service.instance_id:04X}, service 0x{offered_service.service_id:04X}" ) for o in self.attached_observers: @@ -87,7 +86,7 @@ def datagram_received(self, data: bytes, addr: Tuple[Union[str, Any], int]) -> N event_group_entry, ipv4_endpoint_option, ) in extract_subscribe_eventgroup_entries(someip_sd_header): - _logger.debug( + get_logger(_logger_name).debug( f"Received subscribe for instance 0x{event_group_entry.sd_entry.instance_id:04X}, service 0x{event_group_entry.sd_entry.service_id:04X}, eventgroup 0x{event_group_entry.eventgroup_id:04X}" ) for o in self.attached_observers: @@ -96,7 +95,7 @@ def datagram_received(self, data: bytes, addr: Tuple[Union[str, Any], int]) -> N for event_group_entry in extract_subscribe_ack_eventgroup_entries( someip_sd_header ): - _logger.debug( + get_logger(_logger_name).debug( f"Received subscribe ACK for instance 0x{event_group_entry.sd_entry.instance_id:04X}, service 0x{event_group_entry.sd_entry.service_id:04X}, eventgroup 0x{event_group_entry.eventgroup_id:04X}" ) # for o in self.attached_observers: