o
    0 i                     @  s~   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dlZdZ	G dd dej
ZG dd	 d	eZG d
d deZdS )    )annotationsN)copy)Literal   c                      s   e Zd ZdZedd ejdd ejdd ejdd ej	dd ej
dd iZ					
		d"d# fddZd$ddZd%ddZd& fd d!Z  ZS )'ColourizedFormatterz
    A custom log formatter class that:

    * Outputs the LOG_LEVEL with an appropriate color.
    * If a log call includes an `extra={"color_message": ...}` it will be used
      for formatting the output, instead of the plain text message.
    c                 C     t jt| ddS )NblueZfgclickstylestr
level_name r   [/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/uvicorn/logging.py<lambda>       zColourizedFormatter.<lambda>c                 C  r   )Ncyanr	   r
   r   r   r   r   r      r   c                 C  r   Ngreenr	   r
   r   r   r   r   r      r   c                 C  r   Nyellowr	   r
   r   r   r   r   r      r   c                 C  r   Nredr	   r
   r   r   r   r   r      r   c                 C  r   N
bright_redr	   r
   r   r   r   r   r      r   N%fmt
str | Nonedatefmtr   Literal['%', '{', '$']
use_colorsbool | Nonec                   s2   |dv r|| _ ntj | _ t j|||d d S )N)TF)r   r    r   )r"   sysstdoutisattysuper__init__)selfr   r    r   r"   	__class__r   r   r(       s   zColourizedFormatter.__init__r   r   level_nointreturnc                 C  s    ddd}| j ||}||S )Nr   r   r.   c                 S  s   t | S N)r   r   r   r   r   default.   s   z5ColourizedFormatter.color_level_name.<locals>.default)r   r   r.   r   )level_name_colorsget)r)   r   r,   r0   funcr   r   r   color_level_name-   s   
z$ColourizedFormatter.color_level_nameboolc                 C  s   dS )NTr   r)   r   r   r   should_use_colors4      z%ColourizedFormatter.should_use_colorsrecordlogging.LogRecordc                   sv   t |}|j}ddt|j  }| jr,| ||j}d|jv r,|jd |_| |jd< |d | |jd< t	 
|S )N    Zcolor_messagemessage:Zlevelprefix)r   	levelnamelenr"   r4   levelno__dict__msg
getMessager'   formatMessage)r)   r9   
recordcopyr?   Z	seperatorr*   r   r   rE   7   s   
z!ColourizedFormatter.formatMessage)NNr   N)r   r   r    r   r   r!   r"   r#   )r   r   r,   r-   r.   r   r.   r5   r9   r:   r.   r   )__name__
__module____qualname____doc__TRACE_LOG_LEVELloggingDEBUGINFOWARNINGERRORCRITICALr1   r(   r4   r7   rE   __classcell__r   r   r*   r   r      s"    	






r   c                   @  s   e Zd ZdddZdS )DefaultFormatterr.   r5   c                 C  s
   t j S r/   )r$   stderrr&   r6   r   r   r   r7   E   s   
z"DefaultFormatter.should_use_colorsNrG   )rI   rJ   rK   r7   r   r   r   r   rU   D   s    rU   c                      sL   e Zd Zdd dd dd dd dd dZdddZd fddZ  ZS )AccessFormatterc                 C  r   )Nbright_whiter	   r
   coder   r   r   r   K   r   zAccessFormatter.<lambda>c                 C  r   r   r
   rY   r   r   r   r   L   r   c                 C  r   r   r
   rY   r   r   r   r   M   r   c                 C  r   r   r
   rY   r   r   r   r   N   r   c                 C  r   r   r
   rY   r   r   r   r   O   r   )            r   status_coder-   r.   r   c                   sh   zt |j}W n ty   d}Y nw | d|  | jr2d
 fdd}| j|d	 |}| S  S )N r;   rZ   r-   r.   r   c                   s    S r/   r   rY   Zstatus_and_phraser   r   r0   Z   r8   z0AccessFormatter.get_status_code.<locals>.defaultd   )rZ   r-   r.   r   )http
HTTPStatusphrase
ValueErrorr"   status_code_coloursr2   )r)   r_   Zstatus_phraser0   r3   r   ra   r   get_status_codeR   s   zAccessFormatter.get_status_coder9   r:   c           	        sn   t |}|j\}}}}}| t|}| d| d| }| jr'tj|dd}|j|||d t	 
|S )Nr;   z HTTP/T)bold)client_addrrequest_liner_   )r   argsrh   r-   r"   r   r   rB   updater'   rE   )	r)   r9   rF   rj   method	full_pathhttp_versionr_   rk   r*   r   r   rE   a   s&   zAccessFormatter.formatMessage)r_   r-   r.   r   rH   )rI   rJ   rK   rg   rh   rE   rT   r   r   r*   r   rW   I   s    
rW   )
__future__r   rc   rN   r$   r   typingr   r   rM   	Formatterr   rU   rW   r   r   r   r   <module>   s    6