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    {