o
    ¯bþ&  ã                   @   s|   d Z ddlmZ ddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZmZ ddlmZmZ G d	d
„ d
ƒZeƒ Zdd„ ZdS )z
Logger class.
é    )Útime)ÚAnyÚOptionalÚcast)Úcurrentframe)ÚFailureé   )ÚILogObserverÚLogTrace)ÚInvalidLogLevelErrorÚLogLevelc                   @   sR  e Zd ZdZedefdd„ƒZ			d$dee dee ded	 ddfd
d„Z	d%dedee
 dd fdd„Zdefdd„Z	d%dedee deddfdd„Zdejfdedee dededdf
dd„Zd%dee deddfdd„Zd%dee deddfdd„Zd%dee deddfdd„Zd%dee deddfd d!„Zd%dee deddfd"d#„ZdS )&ÚLoggera~  
    A L{Logger} emits log messages to an observer.  You should instantiate it
    as a class or module attribute, as documented in L{this module's
    documentation <twisted.logger>}.

    @ivar namespace: the namespace for this logger
    @ivar source: The object which is emitting events via this logger
    @ivar observer: The observer that this logger will send events to.
    Úreturnc                   C   s,   zt ttdƒjd ƒW S  ty   Y dS w )z“
        Derive a namespace from the module containing the caller's caller.

        @return: the fully qualified python name of a module.
        é   Ú__name__z	<unknown>)r   Ústrr   Ú	f_globalsÚKeyError© r   r   ú8/usr/lib/python3/dist-packages/twisted/logger/_logger.pyÚ_namespaceFromCallingContext   s
   ÿz#Logger._namespaceFromCallingContextNÚ	namespaceÚsourceÚobserverr	   c                 C   sD   |du r|   ¡ }|| _|| _|du rddlm} || _dS || _dS )a3  
        @param namespace: The namespace for this logger.  Uses a dotted
            notation, as used by python modules.  If not L{None}, then the name
            of the module of the caller is used.
        @param source: The object which is emitting events via this
            logger; this is automatically set on instances of a class
            if this L{Logger} is an attribute of that class.
        @param observer: The observer that this logger will send events to.
            If L{None}, use the L{global log publisher <globalLogPublisher>}.
        Nr   )ÚglobalLogPublisher)r   r   r   Ú_globalr   r   )Úselfr   r   r   r   r   r   r   Ú__init__)   s   

zLogger.__init__ÚinstanceÚownerc                 C   s>   |dusJ ‚|du r|}n|}| j d |j|jg¡|| jd�S )a‡  
        When used as a descriptor, i.e.::

            # File: athing.py
            class Something:
                log = Logger()
                def hello(self):
                    self.log.info("Hello")

        a L{Logger}'s namespace will be set to the name of the class it is
        declared on.  In the above example, the namespace would be
        C{athing.Something}.

        Additionally, its source will be set to the actual object referring to
        the L{Logger}.  In the above example, C{Something.log.source} would be
        C{Something}, and C{Something().log.source} would be an instance of
        C{Something}.
        NÚ.)r   )Ú	__class__ÚjoinÚ
__module__r   r   )r   r   r   r   r   r   r   Ú__get__F   s   ýzLogger.__get__c                 C   s   d| j j› d| j›d�S )Nú<ú ú>)r!   r   r   )r   r   r   r   Ú__repr__f   s   zLogger.__repr__ÚlevelÚformatÚkwargsc                 K   sz   |t  ¡ vr| jdtt|ƒƒ|| d� dS |}|j| || j| j|tƒ d� d|v r6t	t
|d ƒ | | jf¡ |  |¡ dS )a  
        Emit a log event to all log observers at the given level.

        @param level: a L{LogLevel}
        @param format: a message format using new-style (PEP 3101)
            formatting.  The logging event (which is a L{dict}) is
            used to render this format string.
        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        z:Got invalid log level {invalidLevel!r} in {logger}.emit().)ÚinvalidLevelÚloggerN)Ú
log_loggerÚ	log_levelÚlog_namespaceÚ
log_sourceÚ
log_formatÚlog_timeÚ	log_trace)r   ÚiterconstantsÚfailurer   r   Úupdater   r   r   r   r
   Úappendr   )r   r)   r*   r+   Úeventr   r   r   Úemiti   s(   
üú	zLogger.emitr6   c                 K   s*   |du rt ƒ }| j||fd|i|¤Ž dS )a2  
        Log a failure and emit a traceback.

        For example::

            try:
                frob(knob)
            except Exception:
                log.failure("While frobbing {knob}", knob=knob)

        or::

            d = deferredFrob(knob)
            d.addErrback(lambda f: log.failure("While frobbing {knob}",
                                               f, knob=knob))

        This method is generally meant to capture unexpected exceptions in
        code; an exception that is caught and handled somehow should be logged,
        if appropriate, via L{Logger.error} instead.  If some unknown exception
        occurs and your code doesn't know how to handle it, as in the above
        example, then this method provides a means to describe the failure in
        nerd-speak.  This is done at L{LogLevel.critical} by default, since no
        corrective guidance can be offered to an user/administrator, and the
        impact of the condition is unknown.

        @param format: a message format using new-style (PEP 3101) formatting.
            The logging event (which is a L{dict}) is used to render this
            format string.
        @param failure: a L{Failure} to log.  If L{None}, a L{Failure} is
            created from the exception in flight.
        @param level: a L{LogLevel} to use.
        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        NÚlog_failure)r   r:   )r   r*   r6   r)   r+   r   r   r   r6   �   s   +zLogger.failurec                 K   ó   | j tj|fi |¤Ž dS )að  
        Emit a log event at log level L{LogLevel.debug}.

        @param format: a message format using new-style (PEP 3101) formatting.
            The logging event (which is a L{dict}) is used to render this
            format string.

        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        N)r:   r   Údebug©r   r*   r+   r   r   r   r=   À   ó   zLogger.debugc                 K   r<   )aï  
        Emit a log event at log level L{LogLevel.info}.

        @param format: a message format using new-style (PEP 3101) formatting.
            The logging event (which is a L{dict}) is used to render this
            format string.

        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        N)r:   r   Úinfor>   r   r   r   r@   Ï   r?   zLogger.infoc                 K   r<   )aï  
        Emit a log event at log level L{LogLevel.warn}.

        @param format: a message format using new-style (PEP 3101) formatting.
            The logging event (which is a L{dict}) is used to render this
            format string.

        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        N)r:   r   Úwarnr>   r   r   r   rA   Þ   r?   zLogger.warnc                 K   r<   )að  
        Emit a log event at log level L{LogLevel.error}.

        @param format: a message format using new-style (PEP 3101) formatting.
            The logging event (which is a L{dict}) is used to render this
            format string.

        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        N)r:   r   Úerrorr>   r   r   r   rB   í   r?   zLogger.errorc                 K   r<   )aó  
        Emit a log event at log level L{LogLevel.critical}.

        @param format: a message format using new-style (PEP 3101) formatting.
            The logging event (which is a L{dict}) is used to render this
            format string.

        @param kwargs: additional key/value pairs to include in the event.
            Note that values which are later mutated may result in
            non-deterministic behavior from observers that schedule work for
            later execution.
        N)r:   r   Úcriticalr>   r   r   r   rC   ü   r?   zLogger.critical)NNN©N)r   r#   Ú__qualname__Ú__doc__Ústaticmethodr   r   r   Úobjectr   Útyper$   r(   r   r:   rC   r   r6   r=   r@   rA   rB   r   r   r   r   r      s\    
üþýü
û ÿÿÿÿ
þ*üþýüû
ú0 r   c                 C   s   t  | | j¡S rD   )Ú_logr$   r!   )Úobjr   r   r   Ú<lambda>  s    rL   N)rF   r   Útypingr   r   r   Útwisted.python.compatr   Útwisted.python.failurer   Ú_interfacesr	   r
   Ú_levelsr   r   r   rJ   Ú
_loggerForr   r   r   r   Ú<module>   s    {