Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable changing the library log level #4

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/someipy/__init__.py
Original file line number Diff line number Diff line change
@@ -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
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
25 changes: 16 additions & 9 deletions src/someipy/_internal/logging.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/someipy/_internal/someip_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions src/someipy/client_service_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}"
Expand All @@ -152,15 +151,15 @@ 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:
new_acks.append(expected_ack)

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}"
)

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 0 additions & 2 deletions src/someipy/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 11 additions & 12 deletions src/someipy/server_service_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
UDPSomeipEndpoint,
)

_logger = get_logger("server_service_instance")

_logger_name = "server_service_instance"

class ServerServiceInstance(ServiceDiscoveryObserver):
_service: Service
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}"
)

Expand Down Expand Up @@ -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"
)
Expand All @@ -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(
Expand Down Expand Up @@ -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}"
)

Expand All @@ -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}"
)

Expand Down
9 changes: 4 additions & 5 deletions src/someipy/service_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
Loading