
    g"                     .   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	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 e j(                  d        Z G d dej,                        Z G d	 d
ej,                        Z G d dej,                        Zy)    N)utf8)LogFormatterdefine_logging_optionsenable_pretty_logging)OptionParser)basestring_typec               #      K   t        j                         5  t        j                  dt               d  d d d        y # 1 sw Y   y xY ww)Nignore)category)warningscatch_warningssimplefilterBytesWarning     J/var/www/openai/venv/lib/python3.12/site-packages/tornado/test/log_test.pyignore_bytes_warningr       s0     		 	 	"h> 
#	"	"s   A A 	A A	Ac                   f    e Zd Z ej                  d      Zd Zd Zd Zd Z	d Z
d Zd Zd	 Zd
 Zy)LogFormatterTestsD   (?s)\[E [0-9]{6} [0-9]{2}:[0-9]{2}:[0-9]{2} log_test:[0-9]+\] (.*)c                 D   t        d      | _        t        j                  di| j                  _        d| j                  _        t        j                  d      | _        d| j                  _        t        j                         | _        t        j                  j                  | j                  d      | _        | j!                  | j                        | _        | j"                  j%                  | j                         | j                  j'                  | j"                         y )NF)colorr   zlog.out)r   	formatterloggingERROR_colors_normalLoggerlogger	propagatetempfilemkdtemptempdirospathjoinfilenamemake_handlerhandlersetFormatter
addHandlerselfs    r   setUpzLogFormatterTest.setUp.   s    %E2
 #*--!:!)nn%78 %'')T\\9=((7!!$..1t||,r   c                     | j                   j                          t        j                  | j                         t        j
                  | j                         y N)r*   closer%   unlinkr(   rmdirr$   r-   s    r   tearDownzLogFormatterTest.tearDown?   s2    
		$-- 
r   c                 0    t        j                  |d      S )Nutf-8encoding)r   FileHandler)r.   r(   s     r   r)   zLogFormatterTest.make_handlerD   s    ""8g>>r   c                    t        | j                  d      5 }|j                         j                         }t        j
                  j                  |      }|r|j                  d      cd d d        S t        d|z        # 1 sw Y   y xY w)Nrb   zoutput didn't match regex: %r)	openr(   readstripr   LINE_REmatchgroup	Exception)r.   flinems       r   
get_outputzLogFormatterTest.get_outputG   sl    $--&!668>>#D ((..t4Awwqz	 '&   ?$ FGG '&s   AA?1A??Bc                 z    | j                   j                  d       | j                  | j                         d       y )Nfoos   foo)r    errorassertEqualrH   r-   s    r   test_basic_loggingz#LogFormatterTest.test_basic_loggingP   s+    % *F3r   c           	          t               5  | j                  j                  d       | j                  | j	                         t        t        d                   d d d        y # 1 sw Y   y xY w)N   )r   r    rK   rL   rH   r   reprr-   s    r   test_bytes_loggingz#LogFormatterTest.test_bytes_loggingT   sF    !#KKg&T__.T']0CD $##s   AA""A+c           
         t               5  | j                  j                  dj                  d             d d d        t	        t
        t              r*| j                  | j                         t        d             y | j                  | j                         t        t        t        d                         y # 1 sw Y   xY w)N   ér   )r   r    rK   encode
issubclassbytesr   rL   rH   r   rP   r-   s    r   test_utf8_loggingz"LogFormatterTest.test_utf8_loggingZ   s    !#KKhoof56 $e_- T__.X?
 T__.T$x.5I0JK $#s   +B88Cc                     	 t        d      # t         $ r | j                  j                  d       Y nw xY w| j                         }| j	                  |d       | j                  d|       y )NrO   zcaught exceptions   Exception.*\\xe9s   \n)rD   r    	exceptionrH   assertRegexassertNotIn)r.   outputs     r   test_bytes_exception_loggingz-LogFormatterTest.test_bytes_exception_loggingg   sc    	6G$$ 	6KK!!"45	6 "!56(s    $44c                     | j                   j                  d       | j                  | j                         t	        d             y )NrS   )r    rK   rL   rH   r   r-   s    r   test_unicode_loggingz%LogFormatterTest.test_unicode_loggings   s/    (#*DN;r   N)__name__
__module____qualname__recompilerA   r/   r5   r)   rH   rM   rQ   rW   r]   r_   r   r   r   r   r   '   sG     bjjWG-"
?H4EL
)<r   r   c                   0     e Zd Z fdZd Zd Zd Z xZS )EnablePrettyLoggingTestc                     t         |           t               | _        t	        | j                         t        j                  d      | _        d| j                  _        y )Nz-tornado.test.log_test.EnablePrettyLoggingTestF)	superr/   r   optionsr   r   r   r    r!   )r.   	__class__s    r   r/   zEnablePrettyLoggingTest.setUpy   s>    #~t||,nn%TU %r   c                 |   t        j                         }	 |dz   | j                  _        t	        | j                  | j
                         | j                  dt        | j
                  j                               | j
                  j                  d       | j
                  j                  d   j                          t        j                  |dz         }| j                  dt        |             t        |d   d      5 }| j                  |j                         d	       d d d        | j
                  j                  D ]"  }|j                          |j                          $ t        j                  |dz         D ]  }t!        j"                  |        t!        j$                  |       y # 1 sw Y   xY w# | j
                  j                  D ]"  }|j                          |j                          $ t        j                  |dz         D ]  }t!        j"                  |        t!        j$                  |       w xY w)
N	/test_logri   r    r=   hellor   
/test_log*r7   r8   ^\[E [^]]*\] hello$)r"   r#   ri   log_file_prefixr   r    rL   lenhandlersrK   flushglobr>   rZ   r?   r2   r%   r3   r4   r.   tmpdir	filenamesrE   r*   r(   s         r   test_log_filez%EnablePrettyLoggingTest.test_log_file   s   !!#	+1K+?DLL(!$,,t{{KQDKK$8$8 9:KKg&KK  #))+		&<"78IQI/ilW5  +AB 6  ;;// 0 !IIf|&;<		(# =HHV 65  ;;// 0 !IIf|&;<		(# =HHVs%   C)F7 ?!F+ F7 +F40F7 7BH;c                 @   t        j                         }	 |dz   | j                  _        d| j                  _        t        | j                  | j                         | j                  j                  d       | j                  j                  d   j                          t        j                  |dz         }| j                  dt        |             t        |d   d	      5 }| j                  |j                         d
       d d d        | j                  j                  D ]"  }|j                          |j!                          $ t        j                  |dz         D ]  }t#        j$                  |        t#        j&                  |       y # 1 sw Y   xY w# | j                  j                  D ]"  }|j                          |j!                          $ t        j                  |dz         D ]  }t#        j$                  |        t#        j&                  |       w xY w)Nrl   timerm   rn   r   ro   r=   r7   r8   rp   )r"   r#   ri   rq   log_rotate_moder   r    rK   rs   rt   ru   rL   rr   r>   rZ   r?   r2   r%   r3   r4   rv   s         r   !test_log_file_with_timed_rotatingz9EnablePrettyLoggingTest.test_log_file_with_timed_rotating   s   !!#	+1K+?DLL(+1DLL(!$,,t{{KKKg&KK  #))+		&<"78IQI/ilW5  +AB 6  ;;// 0 !IIf|&;<		(# =HHV 65  ;;// 0 !IIf|&;<		(# =HHVs%   CF !!FF FF BHc                    	 d| j                   _        d| j                   _        | j                  t        t
        | j                   | j                         | j                  j                  D ]"  }|j                          |j                          $ y # | j                  j                  D ]"  }|j                          |j                          $ w xY w)N	some_path
wrong_moderm   )
ri   rq   r|   assertRaises
ValueErrorr   r    rs   rt   r2   )r.   r*   s     r   test_wrong_rotate_mode_valuez4EnablePrettyLoggingTest.test_wrong_rotate_mode_value   s    	 +6DLL(+7DLL(%{{	    ;;// 04;;// 0s   AB =C)r`   ra   rb   r/   ry   r}   r   __classcell__)rj   s   @r   rf   rf   x   s    &(( r   rf   c                   B    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zy)LoggingOptionTestz?Test the ability to enable and disable Tornado's logging hooks.Nc                 8   d}d}dj                  |||g      }t        j                  t        j                  d|g|xs g z   t        j
                  t        j                        }|j                         \  }}| j                  |j                  dd|z         d|v S )	Nz7from tornado.options import options, parse_command_linez%import logging; logging.info("hello");z-c)stdoutstderrr   zprocess failed: %rs   hello)
r'   
subprocessPopensys
executablePIPESTDOUTcommunicaterL   
returncode)	r.   	statementargsIMPORTLOG_INFOprogramprocr   r   s	            r   logs_presentzLoggingOptionTest.logs_present   s     K:((FIx89^^T7+tzr:??$$

 ))+!-AF-JK6!!r   c                 D    | j                  | j                  d             y )NpassassertFalser   r-   s    r   test_defaultzLoggingOptionTest.test_default   s    **623r   c                 D    | j                  | j                  d             y )Nparse_command_line()
assertTruer   r-   s    r   test_tornado_defaultz&LoggingOptionTest.test_tornado_default   s    ))*@ABr   c                 H    | j                  | j                  ddg             y )Nr   z--logging=noner   r-   s    r   test_disable_command_linez+LoggingOptionTest.test_disable_command_line   "    **+ADTCUVWr   c                 H    | j                  | j                  ddg             y )Nr   z--logging=Noner   r-   s    r   *test_disable_command_line_case_insensitivez<LoggingOptionTest.test_disable_command_line_case_insensitive   r   r   c                 D    | j                  | j                  d             y )Nz.options.logging = "none"; parse_command_line()r   r-   s    r   test_disable_code_stringz*LoggingOptionTest.test_disable_code_string   s    NO	
r   c                 D    | j                  | j                  d             y )N,options.logging = None; parse_command_line()r   r-   s    r   test_disable_code_nonez(LoggingOptionTest.test_disable_code_none   s    LM	
r   c                 H    | j                  | j                  ddg             y )Nr   z--logging=infor   r-   s    r   test_disable_overridez'LoggingOptionTest.test_disable_override   s%    >AQ@R	
r   r1   )r`   ra   rb   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r      s0    I"$4CXX




r   r   )
contextlibru   r   r%   rc   r   r   r"   unittestr   tornado.escaper   tornado.logr   r   r   tornado.optionsr   tornado.utilr   contextmanagerr   TestCaser   rf   r   r   r   r   <module>r      s       	 	  
     S S ( (  N<x(( N<b= h// = @1
)) 1
r   