a
    õ£Be1  ã                   @   s  d 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 zddl	Z	W n e
yZ   dZ	Y n0 zddlZW n e
y~   dZY n0 ddlmZmZmZmZ e d¡Ze d¡Ze d¡Zed	œd
d„Zeedœdd„ZG dd„ dejƒZdeeej ddœdd„Zdeddœdd„ZdS )aH  Logging support for Tornado.

Tornado uses three logger streams:

* ``tornado.access``: Per-request logging for Tornado's HTTP servers (and
  potentially other servers in the future)
* ``tornado.application``: Logging of errors from application code (i.e.
  uncaught exceptions from callbacks)
* ``tornado.general``: General-purpose logging, including any errors
  or warnings from Tornado itself.

These streams may be configured independently using the standard library's
`logging` module.  For example, you may wish to send ``tornado.access`` logs
to a separate file for analysis.
é    N)Ú_unicode)Úunicode_typeÚbasestring_type)ÚDictÚAnyÚcastÚOptionalztornado.accessztornado.applicationztornado.general)Úreturnc                   C   st   z\t tjdƒrZtj ¡ rZtr:t ¡  t d¡dkrZW dS n trZtjttj	dt
ƒ ƒu rZW dS W n tyn   Y n0 dS )NÚisattyÚcolorsr   TZwrapped_stderrF)ÚhasattrÚsysÚstderrr
   ÚcursesZ	setuptermZtigetnumÚcoloramaÚgetattrZ
initialiseÚobjectÚ	Exception© r   r   úN/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/log.pyÚ_stderr_supports_color7   s    
ÿ
r   )Úsr	   c                 C   s*   z
t | ƒW S  ty$   t| ƒ Y S 0 d S ©N)r   ÚUnicodeDecodeErrorÚrepr)r   r   r   r   Ú_safe_unicodeJ   s    
r   c                
   @   sv   e Zd ZdZdZdZejdejdej	dej
dejdiZeed	d
efeeeeeeef ddœdd„Zeedœdd„ZdS )ÚLogFormatterap  Log formatter used in Tornado.

    Key features of this formatter are:

    * Color support when logging to a terminal that supports it.
    * Timestamps on every log line.
    * Robust against str/bytes encoding problems.

    This formatter is enabled automatically by
    `tornado.options.parse_command_line` or `tornado.options.parse_config_file`
    (unless ``--logging=none`` is used).

    Color support on Windows versions that do not support ANSI color codes is
    enabled by use of the colorama__ library. Applications that wish to use
    this must first initialize colorama with a call to ``colorama.init``.
    See the colorama documentation for details.

    __ https://pypi.python.org/pypi/colorama

    .. versionchanged:: 4.5
       Added support for ``colorama``. Changed the constructor
       signature to be compatible with `logging.config.dictConfig`.
    zV%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)sz%y%m%d %H:%M:%Sé   é   é   é   é   ú%TN)ÚfmtÚdatefmtÚstyleÚcolorr   r	   c           
      C   sÊ   t jj| |d� || _i | _|rÀtƒ rÀtdur˜t d¡pDt d¡pDd}| ¡ D ] \}}t	t 
||¡dƒ| j|< qNt d¡}	|	dur�t	|	dƒ| _q¾d| _qÆ| ¡ D ]\}}d	| | j|< q d
| _nd| _dS )aX  
        :arg bool color: Enables color support.
        :arg str fmt: Log message format.
          It will be applied to the attributes dict of log records. The
          text between ``%(color)s`` and ``%(end_color)s`` will be colored
          depending on the level if color support is on.
        :arg dict colors: color mappings from logging level to terminal color
          code
        :arg str datefmt: Datetime format.
          Used for formatting ``(asctime)`` placeholder in ``prefix_fmt``.

        .. versionchanged:: 3.2

           Added ``fmt`` and ``datefmt`` arguments.
        )r$   NZsetafZsetfó    ÚasciiZsgr0Ú z[2;3%dmz[0m)ÚloggingÚ	FormatterÚ__init__Ú_fmtÚ_colorsr   r   ZtigetstrÚitemsr   ZtparmÚ_normal)
Úselfr#   r$   r%   r&   r   Zfg_colorÚlevelnoÚcodeÚnormalr   r   r   r,   t   s$    
ÿ
zLogFormatter.__init__)Úrecordr	   c              
   C   s  z$|  ¡ }t|tƒsJ ‚t|ƒ|_W n4 tyX } zd||jf |_W Y d }~n
d }~0 0 |  |tt	| j
ƒ¡|_|j| jv r”| j|j |_| j|_nd |_|_| j|j }|jrÆ|jsÆ|  |j¡|_|jrü| ¡ g}| dd„ |j d¡D ƒ¡ d |¡}| dd¡S )NzBad message (%r): %rr)   c                 s   s   | ]}t |ƒV  qd S r   )r   )Ú.0Úlnr   r   r   Ú	<genexpr>Ò   r'   z&LogFormatter.format.<locals>.<genexpr>Ú
z
    )Ú
getMessageÚ
isinstancer   r   Úmessager   Ú__dict__Ú
formatTimer   Ústrr$   Úasctimer2   r.   r&   r0   Z	end_colorr-   Úexc_infoÚexc_textÚformatExceptionÚrstripÚextendÚsplitÚjoinÚreplace)r1   r5   r<   ÚeÚ	formattedÚlinesr   r   r   Úformat¨   s(    &


zLogFormatter.format)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_FORMATZDEFAULT_DATE_FORMATr*   ÚDEBUGÚINFOÚWARNINGÚERRORÚCRITICALZDEFAULT_COLORSr?   Úboolr   Úintr,   r   rL   r   r   r   r   r   Q   s.   û
ú
ù4r   )ÚoptionsÚloggerr	   c                 C   s  | du rddl }|jj} | jdu s0| j ¡ dkr4dS |du rDt ¡ }| tt| j ¡ ƒ¡ | jrà| j	}|dkrŠtj
j| j| j| jdd�}n<|dkr²tj
j| j| j| j| jdd�}nd	d
|  }t|ƒ‚| tdd�¡ | |¡ | jsú| jdu �r|j
�st ¡ }| tƒ ¡ | |¡ dS )z¬Turns on formatted logging output as configured.

    This is called automatically by `tornado.options.parse_command_line`
    and `tornado.options.parse_config_file`.
    Nr   ÚnoneÚsizezutf-8)ÚfilenameÚmaxBytesÚbackupCountÚencodingÚtime)r]   ÚwhenÚintervalr_   r`   z.The value of log_rotate_mode option should be z"size" or "time", not "%s".F)r&   )Útornado.optionsrY   r*   ÚlowerÚ	getLoggerÚsetLevelr   ÚupperÚlog_file_prefixÚlog_rotate_modeÚhandlersÚRotatingFileHandlerÚlog_file_max_sizeÚlog_file_num_backupsÚTimedRotatingFileHandlerÚlog_rotate_whenÚlog_rotate_intervalÚ
ValueErrorÚsetFormatterr   Ú
addHandlerÚlog_to_stderrÚStreamHandler)rY   rZ   ÚtornadoZrotate_modeZchannelÚerror_messager   r   r   Úenable_pretty_logging×   sH    üû	ÿÿ
ry   )rY   r	   c                    sÀ   ˆ du rddl }|jj‰ ˆ jddddd� ˆ jdtdd	d
� ˆ jdtdddd� ˆ jdtddd
� ˆ jdtddd
� ˆ jdtddd
� ˆ jdtddd
� ˆ jdtddd
� ˆ  ‡ fdd„¡ dS ) aB  Add logging-related flags to ``options``.

    These options are present automatically on the default options instance;
    this method is only necessary if you have created your own `.OptionParser`.

    .. versionadded:: 4.2
        This function existed in prior versions but was broken and undocumented until 4.2.
    Nr   r*   ÚinfozSSet the Python log level. If 'none', tornado won't touch the logging configuration.zdebug|info|warning|error|none)ÚdefaultÚhelpÚmetavarru   zŒSend log output to stderr (colorized if possible). By default use stderr if --log_file_prefix is not set and no other logging is configured.)Útyper{   r|   ri   ÚPATHz¥Path prefix for log files. Note that if you are running multiple tornado processes, log_file_prefix must be different for each of them (e.g. include the port number))r~   r{   r}   r|   rm   i áõz%max size of log files before rolloverrn   é
   znumber of log files to keeprp   Zmidnightzcspecify the type of TimedRotatingFileHandler interval other options:('S', 'M', 'H', 'D', 'W0'-'W6')rq   r    z$The interval value of timed rotatingrj   r\   z(The mode of rotating files(time or size)c                      s   t ˆ ƒS r   )ry   r   ©rY   r   r   Ú<lambda>W  r'   z(define_logging_options.<locals>.<lambda>)rd   rY   ÚdefinerW   r?   rX   Zadd_parse_callback)rY   rw   r   r�   r   Údefine_logging_options	  sd    	ù	û
úüÿû	üür„   )NN)N)rP   r*   Úlogging.handlersr   Ztornado.escaper   Ztornado.utilr   r   r   ÚImportErrorr   Útypingr   r   r   r   rf   Z
access_logZapp_logZgen_logrW   r   r?   r   r+   r   ÚLoggerry   r„   r   r   r   r   Ú<module>   s6   




  ÿ
þ2