U
    
W[                     @   s\   d Z ddlmZmZ ddlmZ ddlmZ dZG dd deZ	ee	G d	d
 d
e
ZdS )z
Basic log observers.
    )	Interfaceimplementer)Failure   )LoggerzITemporarily disabling observer {observer} due to exception: {log_failure}c                   @   s   e Zd ZdZdd ZdS )ILogObservera  
    An observer which can handle log events.

    Unlike most interfaces within Twisted, an L{ILogObserver} I{must be
    thread-safe}.  Log observers may be called indiscriminately from many
    different threads, as any thread may wish to log a message at any time.
    c                 C   s   dS )a  
        Log an event.

        @type event: C{dict} with (native) C{str} keys.
        @param event: A dictionary with arbitrary keys as defined by the
            application emitting logging events, as well as keys added by the
            logging system.  The logging system reserves the right to set any
            key beginning with the prefix C{"log_"}; applications should not
            use any key so named.  Currently, the following keys are used by
            the logging system in some way, if they are present (they are all
            optional):

                - C{"log_format"}: a PEP-3101-style format string which draws
                  upon the keys in the event as its values, used to format the
                  event for human consumption.

                - C{"log_flattened"}: a dictionary mapping keys derived from
                  the names and format values used in the C{"log_format"}
                  string to their values.  This is used to preserve some
                  structured information for use with
                  L{twisted.logger.extractField}.

                - C{"log_trace"}: A L{list} designed to capture information
                  about which L{LogPublisher}s have observed the event.

                - C{"log_level"}: a L{log level
                  <twisted.logger.LogLevel>} constant, indicating the
                  importance of and audience for this event.

                - C{"log_namespace"}: a namespace for the emitter of the event,
                  given as a unicode string.

                - C{"log_system"}: a string indicating the network event or
                  method call which resulted in the message being logged.
        N )eventr   r   :/usr/lib/python3/dist-packages/twisted/logger/_observer.py__call__   s    zILogObserver.__call__N)__name__
__module____qualname____doc__r   r   r   r   r
   r      s   r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )LogPublisherz
    I{ILogObserver} that fans out events to other observers.

    Keeps track of a set of L{ILogObserver} objects and forwards
    events to each.
    c                 G   s   t || _t| d| _d S )Nobserver)list
_observersr   log)selfZ	observersr   r   r
   __init__O   s    
zLogPublisher.__init__c                 C   s0   t |std||| jkr,| j| dS )zq
        Registers an observer with this publisher.

        @param observer: An L{ILogObserver} to add.
        zObserver is not callable: {0!r}N)callable	TypeErrorformatr   appendr   r   r   r   r
   addObserverT   s    
zLogPublisher.addObserverc                 C   s*   z| j | W n tk
r$   Y nX dS )zv
        Unregisters an observer with this publisher.

        @param observer: An L{ILogObserver} to remove.
        N)r   remove
ValueErrorr   r   r   r
   removeObserver`   s    zLogPublisher.removeObserverc              	      s   d kr fdd}nd}g }j D ]F}|dk	r:|| z|  W q& tk
rj   ||t f Y q&X q&|D ]"\}}|}|jt||d qrdS )z8
        Forward events to contained observers.
        	log_tracec                    s    d  | f dS )z
                Add tracing information for an observer.

                @param observer: an observer being forwarded to
                @type observer: L{ILogObserver}
                r!   N)r   r   r	   r   r   r
   traceq   s    z$LogPublisher.__call__.<locals>.traceN)failurer   )r   	Exceptionr   r   _errorLoggerForObserverr$   OBSERVER_DISABLED)r   r	   r#   ZbrokenObserversr   ZbrokenObserverr$   ZerrorLoggerr   r"   r
   r   l   s$    	

zLogPublisher.__call__c                    s"   t  fdd| jD  }t|dS )z
        Create an error-logger based on this logger, which does not contain the
        given bad observer.

        @param observer: The observer which previously had an error.
        @type observer: L{ILogObserver}

        @return: L{None}
        c                    s   g | ]}| k	r|qS r   r   ).0Zobsr   r   r
   
<listcomp>   s   z8LogPublisher._errorLoggerForObserver.<locals>.<listcomp>r   )r   r   r   )r   r   ZerrorPublisherr   r   r
   r&      s    
z$LogPublisher._errorLoggerForObserverN)	r   r   r   r   r   r   r    r   r&   r   r   r   r
   r   F   s   $r   N)r   Zzope.interfacer   r   Ztwisted.python.failurer   Z_loggerr   r'   r   objectr   r   r   r   r
   <module>   s   0