o
    >h)                     @   s   d Z ddlmZmZ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 dd	lmZ dd
lmZmZ ddlmZmZ ddlmZ G dd deZG dd dZG dd dejZdS )z+
Test cases for L{twisted.logger._logger}.
    )ListOptionalTypecast)implementer)NamedConstant)Failure)unittest   )formatEvent)globalLogPublisher)ILogObserverLogEvent)InvalidLogLevelErrorLogLevelLoggerc                	   @   s2   e Zd ZdZ	d	dedee deddfddZdS )

TestLoggerz^
    L{Logger} with an overridden C{emit} method that keeps track of received
    events.
    Nlevelformatkwargsreturnc              	      sl   t tdtdd f fdd}t| ztj ||fi | W t| nt| w |||d _d S )Neventr   c                    s
   |  _ d S Nr   r   self /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/logger/test/test_logger.pyobserver    s   
z!TestLogger.emit.<locals>.observer)r   r   r   )	r   r   r   r   addObserverr   emitremoveObserveremitted)r   r   r   r   r   r   r   r   r!      s   
zTestLogger.emitr   )	__name__
__module____qualname____doc__r   r   strobjectr!   r   r   r   r   r      s    r   c                   @   s<   e Zd ZdZe Zd	dee ddfddZdefddZ	dS )
LogComposedObjectz3
    A regular object, with a logger attached.
    Nstater   c                 C   s
   || _ d S r   r+   )r   r+   r   r   r   __init__8   s   
zLogComposedObject.__init__c                 C   s   d| j  dS )Nz<LogComposedObject >r,   r   r   r   r   __str__;   s   zLogComposedObject.__str__r   )
r$   r%   r&   r'   r   logr   r(   r-   r/   r   r   r   r   r*   1   s
    r*   c                   @   s   e Zd ZdZd$ddZd$ddZd$dd	Zd$d
dZd$ddZd$ddZ	d$ddZ
d$ddZd$ddZd$ddZd$ddZd$ddZd$ddZd$ddZd$d d!Zd$d"d#ZdS )%LoggerTestsz
    Tests for L{Logger}.
    r   Nc                 C   s,   d}t |}| t|dt| d dS )z"
        repr() on Logger
        blearghz<Logger r.   N)r   assertEqualrepr)r   	namespacer0   r   r   r   	test_reprD   s    zLoggerTests.test_reprc                 C   s   t  }| |jt dS )z3
        Default namespace is module name.
        N)r   r3   r5   r$   r   r0   r   r   r   test_namespaceDefaultL   s   z!LoggerTests.test_namespaceDefaultc                 C   s.   g }t dttdt  | |d jd dS )z
        Default namespace is C{"<unknown>"} when a logger is created from a
        context in which is can't be determined automatically and no namespace
        was specified.
        zresult.append(Logger())r   r   z	<unknown>N)execdictr   localsr3   r5   )r   resultr   r   r   test_namespaceOMGItsTooHardS   s   z'LoggerTests.test_namespaceOMGItsTooHardc                 C   s   t  }d|j|jj}| tt|jj	| | tt
t t jj	| | tt
t t jjt  | tt|jj| | t j dS )z
        Default namespace for classes using L{Logger} as a descriptor is the
        class name they were retrieved from.
        z{}.{}N)r*   r   r%   	__class__r$   r3   r   r   r0   r5   r   assertIssourceassertIsNoner   )r   objexpectedNamespacer   r   r   test_namespaceAttributea   s   z#LoggerTests.test_namespaceAttributec                    sJ   g  G  fddd}|j d | t d |  d d d dS )zH
        When used as a descriptor, the observer is propagated.
        c                       s   e Zd Zeee jdZdS )z5LoggerTests.test_descriptorObserver.<locals>.MyObjectr   N)r$   r%   r&   r   r   r   appendr0   r   observedr   r   MyObject}       rI   hello   r   
log_formatN)r0   infor3   len)r   rI   r   rG   r   test_descriptorObserverw   s
   z#LoggerTests.test_descriptorObserverc                 C   sX   t d}tt|j}|d | d|j | |jd | t|j}| d| dS )z
        On instances that have a L{Logger} class attribute, the C{log_source}
        key is available to format strings.
        rK   zHello, {log_source}.
log_sourcez!Hello, <LogComposedObject hello>.N)	r*   r   r   r0   errorassertInr   r3   r   )r   rB   r0   stuffr   r   r   !test_sourceAvailableForFormatting   s   

z-LoggerTests.test_sourceAvailableForFormattingc                 C   s   t  }t D ]t}d}|j|jd}t||j}||||jd | |jd | | |jd | | |jd d | | t	|dd	 | |j
d
 | | |j
d | | |j
d t | |j
d  | |j
d | | t|j
| qdS )z]
        Test that log levels and messages are emitted correctly for
        Logger.
        zThis is a {level_name} message)
level_name)junkrV   r   r   r   rW   r   zNo event observed.rM   	log_levellog_namespacerQ   N)r   r   iterconstantsr   namegetattrr3   r#   
assertTruehasattrr   r$   rA   r   )r   r0   r   r   message	logMethodr   r   r   test_basicLogger   s"   zLoggerTests.test_basicLoggerc                    sF   t tdtddf fddG fddd tt j  dS )z>
        C{log_source} event key refers to the class.
        r   r   Nc                    s    | d   d S NrQ   r3   r   )Thingor   r   r   r         z0LoggerTests.test_sourceOnClass.<locals>.observerc                          e Zd Ze dZdS )z.LoggerTests.test_sourceOnClass.<locals>.ThingorE   Nr$   r%   r&   r   r0   r   rE   r   r   rd          rd   r   r   r   r   r   r0   rN   r   r   )rd   r   r   r   test_sourceOnClass   s   zLoggerTests.test_sourceOnClassc                    sL   t tdtddffdd G  fddd}| ttj  dS )zA
        C{log_source} event key refers to the instance.
        r   r   Nc                    s     | d  d S rb   rc   r   )r   thingor   r   r      re   z3LoggerTests.test_sourceOnInstance.<locals>.observerc                       rf   )z1LoggerTests.test_sourceOnInstance.<locals>.ThingorE   Nrg   r   rE   r   r   rd      rh   rd   ri   )r   rd   r   )r   r   rk   r   test_sourceOnInstance   s
   z!LoggerTests.test_sourceOnInstancec                    s4   t tdtddf fdd}t|d}|  dS )z5
        C{log_source} event key is L{None}.
        r   r   Nc                    s     | d  d S rb   )rA   r   r   r   r   r      s   z0LoggerTests.test_sourceUnbound.<locals>.observerrE   r   r   r   r   rN   )r   r   r0   r   r   r   test_sourceUnbound   s   
zLoggerTests.test_sourceUnboundc                 C   sp   t  }ztd ty   |d Y nw | t}| t|d | |jd tj | |jd d dS )z?
        Test that log.failure() emits the right data.
        zbaloney!WhoopsrL   r   r   N)	r   RuntimeErrorfailureflushLoggedErrorsr3   rO   r#   r   criticalr   r0   errorsr   r   r   test_defaultFailure   s   
zLoggerTests.test_defaultFailurec                 C   sj   t  }|jddtjddd | |jd d | |jd tj | |jd |j | |jd	  d
S )zQ
        Make sure that kwargs conflicting with args don't pass through.
        *#z*namespace*z*source*)rM   rX   rY   rQ   rM   rX   rY   rQ   N)r   warnr   rR   r3   r   r5   rA   r7   r   r   r   test_conflictingKwargs   s   z"LoggerTests.test_conflictingKwargsc                 C   s.   t  }|d | t}| t|d dS )zA
        Test passing in a bogus log level to C{emit()}.
        z*bogus*rL   N)r   r!   rr   r   r3   rO   rt   r   r   r   test_logInvalidLogLevel   s   

z#LoggerTests.test_logInvalidLogLevelc                    s\   t tdtddffddt tdtddf fddtd  jd	g d
 dS )zE
        Tracing keeps track of forwarding to the publisher.
        r   r   Nc                    s    |  d S r   r   r   rE   r   r   	publisher  s   z)LoggerTests.test_trace.<locals>.publisherc                    s    | d  fg d S )N	log_tracerc   r   )r0   r|   r   r   r   r     s   z(LoggerTests.test_trace.<locals>.observerrE   zHello.)r}   rm   r   r   )r0   r   r|   r   r   
test_trace  s   
zLoggerTests.test_tracec                    sn  g  t tdtddf fdd}t|d}dG fdd	d	}|jd
| d}dd  W d   n1 s8w   Y  | |jd | |jd | t d  \}g  dd< |d }| d | t	|d | d | |j
t |jd| d}| |jd | |jd W d   n1 sw   Y  | d | |jd | |jd dS )z
        The L{Logger.failuresHandled} context manager catches any
        L{BaseException} and converts it into a logged L{Failure}.
        r   r   Nc                         |  d S r   rF   r   eventsr   r   logged     z0LoggerTests.test_failuresHandled.<locals>.loggedrE   r   c                       s   e Zd Zdef fddZdS )z2LoggerTests.test_failuresHandled.<locals>.Reprabler   c                    s    d7  d  dS )NrL   z<repr r.   r   r   reprdr   r   __repr__&  s   z;LoggerTests.test_failuresHandled.<locals>.Reprable.__repr__N)r$   r%   r&   r(   r   r   r   r   r   Reprable%  rJ   r   z*while testing failure handling for {value})valuerL   FTlog_failurez+while testing failure handling for <repr 1>zsucceeding for {value})r   r   r   r   failuresHandledr3   	succeededfailedrO   r   typeZeroDivisionError)r   r   r0   r   	operationfop2r   )r   r   r   test_failuresHandled  s>   

z LoggerTests.test_failuresHandledc                    s   g  t tdtddf fdd}t|d}|d}d}|
}d	}W d   n1 s,w   Y  | |d | |d	 |  g  d}dd
d}|}|  W d   n1 sZw   Y  | t d  \}|d }| |jt	 dS )z
        The L{Logger.failureHandler} context manager can safely be shared
        amongst multiple invocations and converts L{BaseException} into logged
        L{Failure}s.
        r   r   Nc                    r   r   r   r   r   r   r   r   J  r   z/LoggerTests.test_failureHandler.<locals>.loggedrE   rK   FTc                   S   s   t  r   )KeyboardInterruptr   r   r   r   	raisebaseY  s   z2LoggerTests.test_failureHandler.<locals>.raisebaserL   r   r   N)
r   r   r   r   failureHandlerr?   r3   rO   r   r   )r   r   r0   r   successfhr   r   r   r   r   test_failureHandlerB  s*   


zLoggerTests.test_failureHandlerr   )r$   r%   r&   r'   r6   r8   r=   rD   rP   rU   ra   rj   rl   rn   rv   rz   r{   r~   r   r   r   r   r   r   r1   ?   s$    














+r1   N)r'   typingr   r   r   r   zope.interfacer   
constantlyr   twisted.python.failurer   twisted.trialr	   _formatr   _globalr   _interfacesr   r   _levelsr   r   _loggerr   r   r*   TestCaser1   r   r   r   r   <module>   s   