o
    b                  
   @   s   U d Z ddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZ dd
lmZ ejejejejejejejejejejiZee	ef ed< deee	f fddZ e  Z!eeG dd dZ"G dd dZ#dS )z3
Integration with Python standard library logging.
    N)MappingTuple)implementer)NamedConstant)currentframe   )formatEvent)ILogObserverLogEvent)LogLeveltoStdlibLogLevelMappingreturnc                  C   s0   i } t  D ]\}}|| |< || t|< q| S )z
    Reverse the above mapping, adding both the numerical keys used above and
    the corresponding string keys also used by python logging.
    @return: the reversed mapping
    )r   itemsstdlibLogginggetLevelName)mappinglogLevel
pyLogLevel r   8/usr/lib/python3/dist-packages/twisted/logger/_stdlib.py_reverseLogLevelMapping   s
   r   c                   @   sh   e Zd ZdZdZdefdededdfdd	Z	ddedede	eeedf fddZ
deddfddZdS )STDLibLogObserverae  
    Log observer that writes to the python standard library's C{logging}
    module.

    @note: Warning: specific logging configurations (example: network) can lead
        to this observer blocking.  Nothing is done here to prevent that, so be
        sure to not to configure the standard library logging module to block
        when used in conjunction with this module: code within Twisted, such as
        twisted.web, assumes that logging does not block.

    @cvar defaultStackDepth: This is the default number of frames that it takes
        to get from L{STDLibLogObserver} through the logging module, plus one;
        in other words, the number of frames if you were to call a
        L{STDLibLogObserver} directly.  This is useful to use as an offset for
        the C{stackDepth} parameter to C{__init__}, to add frames for other
        publishers.
       twistedname
stackDepthr   Nc                 C   s    t || _| j| j_|| _dS )z
        @param name: logger identifier.
        @param stackDepth: The depth of the stack to investigate for caller
            metadata.
        N)r   	getLoggerlogger_findCaller
findCallerr   )selfr   r   r   r   r   __init__E   s   

zSTDLibLogObserver.__init__Fr   	stackInfo
stackLevelc                 C   s(   t | j}|j}d}|j|j|jf| S )a  
        Based on the stack depth passed to this L{STDLibLogObserver}, identify
        the calling function.

        @param stackInfo: Whether or not to construct stack information.
            (Currently ignored.)
        @param stackLevel: The number of stack frames to skip when determining
            the caller (currently ignored; use stackDepth on the instance).

        @return: Depending on Python version, either a 3-tuple of (filename,
            lineno, name) or a 4-tuple of that plus stack information.
        N)r   r   f_codeco_filenamef_linenoco_name)r    r"   r#   fcoextrar   r   r   r   Q   s   
zSTDLibLogObserver._findCallereventc                 C   s`   | dtj}| d}|du rd}n	|j|j| f}t |tj}| j	j
|t||d dS )zB
        Format an event and bridge it to Python logging.
        	log_levellog_failureN)exc_info)getr   infotypevaluegetTracebackObjectr   r   INFOr   logStringifiableFromEvent)r    r,   levelfailureexcInfostdlibLevelr   r   r   __call__e   s   
zSTDLibLogObserver.__call__)Fr   )__name__
__module____qualname____doc__defaultStackDepthstrintr!   boolr   r   r
   r<   r   r   r   r   r   /   s(    

r   c                   @   s>   e Zd ZdZdeddfddZdefddZdefd	d
Z	dS )r7   z
    An object that implements C{__str__()} in order to defer the work of
    formatting until it's converted into a C{str}.
    r,   r   Nc                 C   s
   || _ dS )z)
        @param event: An event.
        N)r,   )r    r,   r   r   r   r!   y   s   
zStringifiableFromEvent.__init__c                 C   s
   t | jS r$   )r   r,   r    r   r   r   __str__   s   
zStringifiableFromEvent.__str__c                 C   s   t | dS )Nzutf-8)rB   encoderE   r   r   r   	__bytes__   s   z StringifiableFromEvent.__bytes__)
r=   r>   r?   r@   r
   r!   rB   rF   bytesrH   r   r   r   r   r7   s   s
    r7   )$r@   loggingr   typingr   r   zope.interfacer   
constantlyr   twisted.python.compatr   _formatr   _interfacesr	   r
   _levelsr   debugDEBUGr1   r5   warnWARNINGerrorERRORcriticalCRITICALr   rC   __annotations__r   fromStdlibLogLevelMappingr   r7   r   r   r   r   <module>   s(   	C