diff --git a/a_sync/a_sync/method.pyx b/a_sync/a_sync/method.pyx index 1e3afe6d..d26c08be 100644 --- a/a_sync/a_sync/method.pyx +++ b/a_sync/a_sync/method.pyx @@ -9,10 +9,10 @@ asynchronously based on various conditions and configurations. # mypy: disable-error-code=valid-type # mypy: disable-error-code=misc import functools +import logging import weakref from inspect import isawaitable from libc.stdint cimport uintptr_t -from logging import DEBUG, getLogger from a_sync._typing import * from a_sync.a_sync._kwargs cimport get_flag_name, is_sync @@ -34,9 +34,15 @@ METHOD_CACHE_TTL = 300 cdef int _METHOD_CACHE_TTL = 300 -logger = getLogger(__name__) +logger = logging.getLogger(__name__) -cdef object c_logger = logger +cdef object _logger_is_enabled_for = logger.isEnabledFor +cdef object _logger_log = logger._log +cdef int DEBUG = logging.DEBUG + +cdef inline void _logger_debug(str msg, tuple args): + if _logger_is_enabled_for(DEBUG): + _logger_log(DEBUG, msg, args) cdef object get_event_loop = asyncio.get_event_loop @@ -96,12 +102,9 @@ class ASyncMethodDescriptor(ASyncDescriptor[I, P, T]): >>> await descriptor(instance, arg1, arg2, kwarg1=value1) """ # NOTE: This is only used by TaskMapping atm # TODO: use it elsewhere - c_logger.debug( + _logger_debug( "awaiting %s for instance: %s args: %s kwargs: %s", - self, - instance, - args, - kwargs, + (self, instance, args, kwargs), ) return await self.__get__(instance, None)(*args, **kwargs) @@ -166,7 +169,7 @@ class ASyncMethodDescriptor(ASyncDescriptor[I, P, T]): instance, self.__wrapped__, self.__is_async_def__, **self.modifiers ) instance.__dict__[self.field_name] = bound - c_logger.debug("new bound method: %s", bound) + _logger_debug("new bound method: %s", (bound,)) _update_cache_timer(self.field_name, instance, bound) return bound @@ -322,7 +325,7 @@ class ASyncMethodDescriptorSyncDefault(ASyncMethodDescriptor[I, P, T]): instance, self.__wrapped__, self.__is_async_def__, **self.modifiers ) instance.__dict__[self.field_name] = bound - c_logger.debug("new bound method: %s", bound) + _logger_debug("new bound method: %s", (bound,)) _update_cache_timer(self.field_name, instance, bound) return bound @@ -407,7 +410,7 @@ class ASyncMethodDescriptorAsyncDefault(ASyncMethodDescriptor[I, P, T]): instance, self.__wrapped__, self.__is_async_def__, **self.modifiers ) instance.__dict__[self.field_name] = bound - c_logger.debug("new bound method: %s", bound) + _logger_debug("new bound method: %s", (bound,)) _update_cache_timer(self.field_name, instance, bound) return bound @@ -596,8 +599,8 @@ class ASyncBoundMethod(ASyncFunction[P, T], Generic[I, P, T]): """ cdef object retval, coro cdef bint debug_logs - if debug_logs := c_logger.isEnabledFor(DEBUG): - c_logger._log(DEBUG, "calling %s with args: %s kwargs: %s", (self, args, kwargs)) + if debug_logs := _logger_is_enabled_for(DEBUG): + _logger_log(DEBUG, "calling %s with args: %s kwargs: %s", (self, args, kwargs)) # This could either be a coroutine or a return value from an awaited coroutine, # depending on if an overriding flag kwarg was passed into the function call. retval = coro = ASyncFunction.__call__(self, self.__self__, *args, **kwargs) @@ -608,12 +611,12 @@ class ASyncBoundMethod(ASyncFunction[P, T], Generic[I, P, T]): elif _should_await(self, kwargs): # The awaitable was not awaited, so now we need to check the flag as defined on 'self' and await if appropriate. if debug_logs: - c_logger._log( + _logger_log( DEBUG, "awaiting %s for %s args: %s kwargs: %s", (coro, self, args, kwargs) ) retval = _await(coro) if debug_logs: - c_logger._log( + _logger_log( DEBUG, "returning %s for %s args: %s kwargs: %s", (retval, self, args, kwargs) ) return retval # type: ignore [call-overload, return-value]