o
    mi!                     @   s   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
 d dlmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ed	d
ZG dd deZdS )    N)
namedtuple)Iterable)CallableClassVarDictListOptionalTuple   )AbstractAccessLogger)BaseRequest)StreamResponse	KeyMethodz
key methodc                       s  e Zd ZU dZdddddddd	d
dddZdZedZedZ	i Z
eeeeee f f ed< dZeeej  ed< dZee ed< efdejdeddf fddZdedeeee f fddZedededed edef
d!d"Zedededed edef
d#d$Z ededed edefd%d&Z!e"dejfd'd(Z#ededed edefd)d*Z$ededed edefd+d,Z%ededed edefd-d.Z&ededed ede'fd/d0Z(ededed ede'fd1d2Z)ededed edefd3d4Z*ededed edefd5d6Z+ededed edefd7d8Z,deded ede-eee.eeegef f  fd9d:Z/e0de1fd;d<Z2deded eddfd=d>Z3  Z4S )?AccessLoggera  Helper object to log access.

    Usage:
        log = logging.getLogger("spam")
        log_format = "%a %{User-Agent}i"
        access_logger = AccessLogger(log, log_format)
        access_logger.log(request, response, time)

    Format:
        %%  The percent sign
        %a  Remote IP-address (IP-address of proxy if using reverse proxy)
        %t  Time when the request was started to process
        %P  The process ID of the child that serviced the request
        %r  First line of request
        %s  Response status code
        %b  Size of response in bytes, including HTTP headers
        %T  Time taken to serve the request, in seconds
        %Tf Time taken to serve the request, in seconds with floating fraction
            in .06f format
        %D  Time taken to serve the request, in microseconds
        %{FOO}i  request.headers['FOO']
        %{FOO}o  response.headers['FOO']
        %{FOO}e  os.environ['FOO']

    remote_addressrequest_start_time
process_idfirst_request_lineresponse_statusresponse_sizerequest_timerequest_time_fracrequest_time_microrequest_headerresponse_header)atPrsbTTfDioz/%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"z.%(\{([A-Za-z0-9\-_]+)\}([ioe])|[atPrsbOD]|Tf?)z(%[^s])_FORMAT_CACHEN
_cached_tzg        _cached_tz_expireslogger
log_formatreturnc                    sD   t  j||d tj|}|s| |}|tj|< |\| _| _dS )zInitialise the logger.

        logger is a logger object to be used for logging.
        log_format is a string with apache compatible log format description.

        )r*   N)super__init__r   r&   getcompile_format_log_format_methods)selfr)   r*   _compiled_format	__class__ M/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/aiohttp/web_log.pyr-   C   s   

zAccessLogger.__init__c                 C   s   t  }| j|D ]B}|d dkr'| j|d  }ttd|d  }t||}n| j|d  |d f}ttd|d  }t|t||d }|	| q	| j
d|}| j
d|}||fS )a  Translate log_format into form usable by modulo formatting

        All known atoms will be replaced with %s
        Also methods for formatting of those atoms will be added to
        _methods in appropriate order

        For example we have log_format = "%a %t"
        This format will be translated to "%s %s"
        Also contents of _methods will be
        [self._format_a, self._format_t]
        These method will be called and results will be passed
        to translated string format.

        Each _format_* method receive 'args' which is list of arguments
        given to self.log

        Exceptions are _format_e, _format_i and _format_o methods which
        also receive key name (by functools.partial)

        r
    r   z
_format_%s   z%sz%\1)list	FORMAT_REfindallLOG_FORMAT_MAPgetattrr   r   	functoolspartialappendsub
CLEANUP_RE)r2   r*   methodsatomformat_key1m
key_methodformat_key2r6   r6   r7   r/   S   s   zAccessLogger.compile_formatkeyrequestresponsetimec                 C   s   |d u rdS |j | dS )Nz(no headers)-headersr.   rJ   rK   rL   rM   r6   r6   r7   	_format_i|   s   zAccessLogger._format_ic                 C   s   |j | dS NrN   rO   rQ   r6   r6   r7   	_format_o   s   zAccessLogger._format_oc                 C   s"   | d u rdS | j }|d ur|S dS rS   )remote)rK   rL   rM   ipr6   r6   r7   	_format_a   s   zAccessLogger._format_ac                 C   s   | j d u st | jkrAt j}ttj|d | _ }tj	|}|tjdd }|j
|jdkr4dndddd}| | _|S tj	| j S )Nseconds   )minutesr   )minutesecondmicrosecond)r'   time_modrM   r(   	localtime	tm_gmtoffdatetimetimezone	timedeltanowreplacer\   	timestamp)clsgmtofftzre   dr6   r6   r7   _get_local_time   s   

zAccessLogger._get_local_timec                 C   s"   t  }|tj|d }|dS )NrX   z[%d/%b/%Y:%H:%M:%S %z])r   rl   rb   rd   strftime)rK   rL   rM   re   
start_timer6   r6   r7   	_format_t   s   
zAccessLogger._format_tc                 C   s   dt   S )Nz<%s>)osgetpidrK   rL   rM   r6   r6   r7   	_format_P      zAccessLogger._format_Pc                 C   s(   | d u rdS d | j| j| jj| jjS )NrN   z{} {} HTTP/{}.{})formatmethodpath_qsversionmajorminorrr   r6   r6   r7   	_format_r   s   zAccessLogger._format_rc                 C      |j S N)statusrr   r6   r6   r7   	_format_s      zAccessLogger._format_sc                 C   r|   r}   )body_lengthrr   r6   r6   r7   	_format_b   r   zAccessLogger._format_bc                 C   s   t t|S r}   strroundrr   r6   r6   r7   	_format_T   rt   zAccessLogger._format_Tc                 C   s   d| S )Nz%06fr6   rr   r6   r6   r7   
_format_Tf   s   zAccessLogger._format_Tfc                 C   s   t t|d S )Ni@B r   rr   r6   r6   r7   	_format_D   s   zAccessLogger._format_Dc                    s    fdd| j D S )Nc                    s    g | ]\}}|| fqS r6   r6   ).0rJ   rv   rr   r6   r7   
<listcomp>   s     z-AccessLogger._format_line.<locals>.<listcomp>)r1   )r2   rK   rL   rM   r6   rr   r7   _format_line   s   zAccessLogger._format_linec                 C   s   | j tjS )zCheck if logger is enabled.)r)   isEnabledForloggingINFO)r2   r6   r6   r7   enabled   s   zAccessLogger.enabledc                 C   s   zE|  |||}t }t }|D ]%\}}|| |jtu r#|||< q|\}	}
||	i }|||
< |||	< q| jj| j	t
| |d W d S  tyU   | jd Y d S w )N)extrazError in logging)r   r:   dictrA   r5   r   r.   r)   infor0   tuple	Exception	exception)r2   rK   rL   rM   fmt_infovaluesr   rJ   valuek1k2dctr6   r6   r7   log   s    



 zAccessLogger.log)5__name__
__module____qualname____doc__r=   
LOG_FORMATrecompiler;   rC   r&   r   r   r	   r   r   __annotations__r'   r   r   rb   rc   r(   floatr   Loggerr-   r/   staticmethodr   r   rR   rT   rW   classmethodrl   ro   rs   r{   intr   r   r   r   r   r   r   r   propertyboolr   r   __classcell__r6   r6   r4   r7   r      s   
 

  )	

"r   )rb   r?   r   rp   r   rM   r_   collectionsr   collections.abcr   typingr   r   r   r   r   r	   abcr   web_requestr   web_responser   r   r   r6   r6   r6   r7   <module>   s     
