
    g1                        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 	 ddl	Z		 ddlZddlmZmZmZmZ  ej"                  d      Z ej"                  d      Z ej"                  d      Zd	efd
Zded	efdZ G d dej2                        Z	 ddedeej6                     d	dfdZdded	dfdZy# e
$ r dZ	Y w xY w# e
$ r dZY w xY w)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                  n   	 t        t        j                  d      rt        j                  j                         rmt        r-t	        j
                          t	        j                  d      dkD  r;yt        r4t        j                  t        t        j                  dt                     u ryy# t        $ r Y yw xY w)Nisattycolorsr   Twrapped_stderrF)hasattrsysstderrr   curses	setuptermtigetnumcoloramagetattr
initialiseobject	Exception     @/var/www/openai/venv/lib/python3.12/site-packages/tornado/log.py_stderr_supports_colorr   7   s    3::x(SZZ->->-@  "??8,q0::'')968"   
 	   		s   A*B( -9B( (	B43B4sc                 N    	 t        |       S # t        $ r t        |       cY S w xY wN)r   UnicodeDecodeErrorrepr)r   s    r   _safe_unicoder#   J   s)    { Aws   
 $$c                       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dededededeeef   ddfdZdedefdZy)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               %Tfmtdatefmtstylecolorr   r
   Nc                 D   t         j                  j                  | |       || _        i | _        |rt               rt        t        j                  d      xs t        j                  d      xs d}|j                         D ]2  \  }}t        t        j                  ||      d      | j                  |<   4 t        j                  d      }	|	t        |	d      | _        yd| _        y|j                         D ]  \  }}d	|z  | j                  |<    d
| _        yd| _        y)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-   Nsetafsetfr   asciisgr0 z[2;3%dmz[0m)logging	Formatter__init___fmt_colorsr   r   tigetstritemsr   tparm_normal)
selfr,   r-   r.   r/   r   fg_colorlevelnocodenormals
             r   r8   zLogFormatter.__init__t   s    . 	""4"9	+-!!??73Uvv7NURU%+\\^MGT -9Xt4g-DLL)	 &4  0%#/#@DL#%DL &,\\^MGT,9D,@DLL) &4(DLr   recordc                 R   	 |j                         }t        |t              sJ t        |      |_        | j                  |t        t        | j                              |_        |j                  | j                  v r0| j                  |j                     |_        | j                  |_        ndx|_        |_        | j"                  |j                  z  }|j$                  r,|j&                  s | j)                  |j$                        |_        |j&                  rS|j+                         g}|j-                  d |j&                  j/                  d      D               dj1                  |      }|j3                  dd      S # t
        $ r"}d|d|j                  |_        Y d }~md }~ww xY w)NzBad message (z): r5   c              3   2   K   | ]  }t        |        y wr    )r#   ).0lns     r   	<genexpr>z&LogFormatter.format.<locals>.<genexpr>   s     Q5Prr*5Ps   
z
    )
getMessage
isinstancer   r#   messager   __dict__
formatTimer   strr-   asctimerA   r:   r/   r>   	end_colorr9   exc_infoexc_textformatExceptionrstripextendsplitjoinreplace)r?   rD   rM   e	formattedliness         r   formatzLogFormatter.format   sJ   	K'')Gg777" +73FN c4<<1HI>>T\\)<<7FL#||F.00FL6+II/	????"&"6"6v"G?? %%'(ELLQV__5J5J45PQQ		%(I  x00/  	K78&//JFNN	Ks   2E; ;	F&F!!F&)__name__
__module____qualname____doc__DEFAULT_FORMATDEFAULT_DATE_FORMATr6   DEBUGINFOWARNINGERRORCRITICALDEFAULT_COLORSrP   boolr   intr8   r   r^   r   r   r   r%   r%   Q   s    0 nN+qaq!N "*!/22 2 	2
 2 S#X2 
2h,1S ,1S ,1r   r%   optionsloggerc                    | ddl }|j                  j                  } | j                  | j                  j                         dk(  ry|t        j                         }|j                  t        t        | j                  j                                      | j                  r| j                  }|dk(  rBt        j                  j                  | j                  | j                  | j                  d      }ne|dk(  rMt        j                  j                  | j                  | j                  | j                   | j                  d      }nd	d
|z  z   }t#        |      |j%                  t'        d             |j)                  |       | j*                  s| j*                  L|j                  s?t        j,                         }|j%                  t'                      |j)                  |       yyy)zTurns 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)rr   whenintervalrt   ru   z.The value of log_rotate_mode option should be z"size" or "time", not "%s".F)r/   )tornado.optionsrm   r6   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)rm   rn   tornadorotate_modechannelerror_messages         r   enable_pretty_loggingr      s    //))'//"7"7"9V"C~""$
OOGGW__%:%:%<=>--& &&:: 00 22#88 	 ; G F"&&?? 00,, 44#88  @ G A/+=>  ]++\67'"!6!6!>v'')\^,'"	 HW!>r   c                      ddl }|j                  j                    j                  dddd        j                  dt        dd	
        j                  dt        ddd        j                  dt
        dd
        j                  dt
        dd
        j                  dt        dd
        j                  dt
        dd
        j                  dt        dd
        j                   fd       y)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   r6   infozSSet the Python log level. If 'none', tornado won't touch the logging configuration.zdebug|info|warning|error|none)defaulthelpmetavarr   zSend 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   r~   PATHzPath 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   r   i z%max size of log files before rolloverr   
   znumber of log files to keepr   midnightzcspecify the type of TimedRotatingFileHandler interval other options:('S', 'M', 'H', 'D', 'W0'-'W6')r   r)   z$The interval value of timed rotatingr   rq   z(The mode of rotating files(time or size)c                      t               S r    )r   )rm   s   r   <lambda>z(define_logging_options.<locals>.<lambda>W  s    '<W'Er   )ry   rm   definerk   rP   rl   add_parse_callback)rm   r   s   ` r   define_logging_optionsr   	  s.    //))NN% 0   NN.  	 NN'   NN!4	   NNS";X   NN<   NN3	   NN7	   EFr   )NNr    )rb   r6   logging.handlersr   tornado.escaper   tornado.utilr   r   r   ImportErrorr   typingr   r   r   r	   r{   
access_logapp_loggen_logrk   r   rP   r#   r7   r%   Loggerr   r   r   r   r   <module>r      s     
 # 6 - , W/0

'

1
2
'

-
. &S S C17$$ C1N =A/#/#!)'..!9/#	/#dNGC NG4 NGE  H
  Fs"   B/ B< /B98B9<CC