o
    >h?                     @  s   d Z ddlmZ ddlmZ ddlmZ ddlmZmZm	Z	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eZG dd dZG dd dZG dd dZe ZdddZdS )z
Logger class.
    )annotations)time)TracebackType)AnyCallableContextManagerOptionalProtocolcast)currentframe)Failure   )ILogObserverLogTrace)InvalidLogLevelErrorLogLevelc                   @  s:   e Zd ZdZedddZedddZedd	d
ZdS )	Operationz
    An L{Operation} represents the success (or lack thereof) of code performed
    within the body of the L{Logger.failureHandler} context manager.
    returnboolc                 C     dS )z
        Did the operation succeed?  C{True} iff the code completed without
        raising an exception; C{False} while the code is running and C{False}
        if it raises an exception.
        N selfr   r   v/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/logger/_logger.py	succeeded       zOperation.succeededFailure | Nonec                 C  r   )zj
        Did the operation raise an exception?  If so, this L{Failure} is that
        exception.
        Nr   r   r   r   r   failure#   r   zOperation.failurec                 C  r   )z
        Did the operation fail?  C{True} iff the code raised an exception;
        C{False} while the code is running and C{False} if it completed without
        error.
        Nr   r   r   r   r   failed*   r   zOperation.failedNr   r   )r   r   )__name__
__module____qualname____doc__propertyr   r   r   r   r   r   r   r      s    r   c                   @  sR   e Zd ZU dZded< dZded< dddZedddZdddZ	dddZ
dS )_FailCtxMgrFr   r   Nr   r   failCallable[[Failure], None]r   Nonec                 C  
   || _ d S N_failr   r&   r   r   r   __init__7      
z_FailCtxMgr.__init__c                 C  s
   | j d uS r*   r   r   r   r   r   r   :   s   
z_FailCtxMgr.failedr   c                 C  s   | S r*   r   r   r   r   r   	__enter__>      z_FailCtxMgr.__enter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                C  s,   |d urt  }|| _| | dS d| _dS NT)r   r   r,   r   r   r3   r5   r7   r   r   r   r   __exit__A   s   
z_FailCtxMgr.__exit__r&   r'   r   r(   r   )r   r   r3   r4   r5   r6   r7   r8   r   r   )r    r!   r"   r   __annotations__r   r.   r$   r   r1   r;   r   r   r   r   r%   3   s   
 

r%   c                   @  s*   e Zd ZdddZdddZdddZdS )_FastFailCtxMgrr&   r'   r   r(   c                 C  r)   r*   r+   r-   r   r   r   r.   R   r/   z_FastFailCtxMgr.__init__c                 C  s   d S r*   r   r   r   r   r   r1   U   r2   z_FastFailCtxMgr.__enter__r3   r4   r5   r6   r7   r8   r   c                C  s"   |d urt  }|| _| | dS r9   )r   r   r,   r:   r   r   r   r;   X   s
   
z_FastFailCtxMgr.__exit__Nr<   )r   r(   r=   )r    r!   r"   r.   r1   r;   r   r   r   r   r?   Q   s    

r?   c                   @  s   e Zd ZdZed4ddZ			d5d6ddZd7d8ddZd4ddZ	d7d9ddZ	de
jfd:d!d"Zd7d;d#d$Zd7d;d%d&Zd7d;d'd(Zd7d;d)d*Zd7d;d+d,Ze
jfd<d.d/Ze
jfd=d2d3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.
    r   strc                   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.
           r    z	<unknown>)r
   rA   r   	f_globalsKeyErrorr   r   r   r   _namespaceFromCallingContextq   s
   z#Logger._namespaceFromCallingContextN	namespaceOptional[str]sourceOptional[object]observerOptional['ILogObserver']r(   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)rE   rF   rH   _globalrL   rJ   )r   rF   rH   rJ   rL   r   r   r   r.   }   s   

zLogger.__init__instanceobjectownerOptional[type]'Logger'c                 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.)rJ   )	__class__joinr!   r    rJ   )r   rN   rP   rH   r   r   r   __get__   s   zLogger.__get__c                 C  s   d| j j d| jdS )N< >)rT   r    rF   r   r   r   r   __repr__   s   zLogger.__repr__levelr   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   iterconstantsr   r   r   updaterF   rH   r   r
   r   appendrJ   )r   r[   r\   r]   eventr   r   r   emit   s(   
	zLogger.emitr   Optional[Failure]c                 K  s*   |du rt  }| j||fd|i| dS )am  
        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 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.
        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.

        @see: L{Logger.failureHandler}

        @see: L{Logger.failuresHandled}
        Nlog_failure)r   rk   )r   r\   r   r[   r]   r   r   r   r      s   2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)rk   r   debugr   r\   r]   r   r   r   ro        zLogger.debugc                 K  rn   )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)rk   r   inforp   r   r   r   rr   *  rq   zLogger.infoc                 K  rn   )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)rk   r   warnrp   r   r   r   rs   9  rq   zLogger.warnc                 K  rn   )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)rk   r   errorrp   r   r   r   rt   H  rq   zLogger.errorc                 K  rn   )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)rk   r   criticalrp   r   r   r   ru   W  rq   zLogger.criticalContextManager[Operation]c                   s   t  fddS )a/  
        Run some application code, logging a failure and emitting a traceback
        in the event that any of it fails, but continuing on.  For example::

            log = Logger(...)

            def frameworkCode() -> None:
                with log.failuresHandled("While frobbing {knob}:", knob=knob) as op:
                    frob(knob)
                if op.succeeded:
                    log.info("frobbed {knob} successfully", knob=knob)

        This method is meant to capture unexpected exceptions from application
        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.
        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 level: a L{LogLevel} to use.

        @param kwargs: additional key/value pairs to include in the event, if
            it is emitted.  Note that values which are later mutated may result
            in non-deterministic behavior from observers that schedule work for
            later execution.

        @see: L{Logger.failure}
        @see: L{Logger.failureHandler}

        @return: A context manager which yields an L{Operation} which will have
            either its C{succeeded} or C{failed} attribute set to C{True} upon
            completion of the code within the code within the C{with} block.
        c                   s   j  | fi S r*   r0   fr\   r]   r[   r   r   r   <lambda>  s    z(Logger.failuresHandled.<locals>.<lambda>)r%   )r   r\   r[   r]   r   ry   r   failuresHandledf  s   *zLogger.failuresHandledstaticMessageContextManager[None]c                   s   t  fddS )a  
        For performance-sensitive frameworks that needs to handle potential
        failures from frequently-called application code, and do not need to
        include detailed structured information about the failure nor inspect
        the result of the operation, this method returns a context manager that
        will log exceptions and continue, that can be shared across multiple
        invocations.  It should be instantiated at module scope to avoid
        additional object creations.

        For example::

            log = Logger(...)
            ignoringFrobErrors = log.failureHandler("while frobbing:")

            def hotLoop() -> None:
                with ignoringFrobErrors:
                    frob()

        This method is meant to capture unexpected exceptions from application
        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 level: a L{LogLevel} to use.

        @see: L{Logger.failure}

        @return: A context manager which does not return a value, but will
            always exit from exceptions.
        c                   s    |  S r*   r0   rw   r[   r   r|   r   r   rz     s    z'Logger.failureHandler.<locals>.<lambda>)r?   )r   r|   r[   r   r~   r   failureHandler  s   +zLogger.failureHandler)r   rA   )NNN)rF   rG   rH   rI   rJ   rK   r   r(   r*   )rN   rO   rP   rQ   r   rR   )r[   r   r\   rG   r]   rO   r   r(   )
r\   rA   r   rl   r[   r   r]   rO   r   r(   )r\   rG   r]   rO   r   r(   )r\   rA   r[   r   r]   rO   r   rv   )r|   rA   r[   r   r   r}   )r    r!   r"   r#   staticmethodrE   r.   rV   rZ   rk   r   ru   r   ro   rr   rs   rt   r{   r   r   r   r   r   r@   f   s0    

 *7/r@   objrO   r   c                 C  s   t | | jS )z?
    Get a L{Logger} instance attached to the given class.
    )_logrV   rT   )r   r   r   r   
_loggerFor  s   r   N)r   rO   r   r@   )r#   
__future__r   r   typesr   typingr   r   r   r   r	   r
   twisted.python.compatr   twisted.python.failurer   _interfacesr   r   _levelsr   r   r   r%   r?   r@   r   r   r   r   r   r   <module>   s"      \