o
    4˜Žj'!  ã                   @   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
mZmZ ddlmZ ddlmZ ddlmZ G dd	„ d	eƒZG d
d„ deƒZdS )é    N)ÚIterable)ÚCallableÚClassVarÚ
NamedTupleé   )ÚAbstractAccessLogger)ÚBaseRequest)ÚStreamResponsec                   @   s8   e Zd ZU eeeef B ed< eeee	gef ed< dS )Ú	KeyMethodÚkeyÚmethodN)
Ú__name__Ú
__module__Ú__qualname__ÚstrÚtupleÚ__annotations__r   r   r	   Úfloat© r   r   úL/var/www/kodo/Anonymous/send/lib/python3.10/site-packages/aiohttp/web_log.pyr
      s   
 r
   c                       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jdB  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f B ef  fd9d:„Z-e.de/fd;d<„ƒZ0deded eddfd=d>„Z1‡  Z2S )?Ú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 )z©Initialise the logger.

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

        )r1   N)ÚsuperÚ__init__r   r-   ÚgetÚcompile_formatÚ_log_formatÚ_methods)Úselfr0   r1   Ú_compiled_format©Ú	__class__r   r   r4   E   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)r9   r1   ÚmethodsÚatomÚformat_key1ÚmÚ
key_methodÚformat_key2r   r   r   r6   U   s   zAccessLogger.compile_formatr   ÚrequestÚresponseÚtimec                 C   s   |d u rdS |j  | d¡S )Nz(no headers)ú-©Úheadersr5   ©r   rO   rP   rQ   r   r   r   Ú	_format_i~   s   zAccessLogger._format_ic                 C   s   |j  | d¡S ©NrR   rS   rU   r   r   r   Ú	_format_oˆ   s   zAccessLogger._format_oc                 C   s"   | d u rdS | j }|d ur|S dS rW   )Úremote)rO   rP   rQ   Úipr   r   r   Ú	_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_modrQ   r/   Ú	localtimeÚ	tm_gmtoffÚdatetimeÚtimezoneÚ	timedeltaÚnowÚreplacer`   Ú	timestamp)ÚclsÚgmtoffÚtzri   Údr   r   r   Ú_get_local_time–   s   

zAccessLogger._get_local_timec                 C   s"   t  ¡ }|tj|d� }| d¡S )Nr\   z[%d/%b/%Y:%H:%M:%S %z])r   rp   rf   rh   Ústrftime)rO   rP   rQ   ri   Ú
start_timer   r   r   Ú	_format_t¥   s   
zAccessLogger._format_tc                 C   s   dt  ¡  S )Nz<%s>)ÚosÚgetpid©rO   rP   rQ   r   r   r   Ú	_format_P«   ó   zAccessLogger._format_Pc                 C   s2   | d u rdS | j › d| j› d| jj› d| jj› �S )NrR   ú z HTTP/Ú.)r   Úpath_qsÚversionÚmajorÚminorrv   r   r   r   Ú	_format_r¯   s   &zAccessLogger._format_rc                 C   ó   |j S ©N)Ústatusrv   r   r   r   Ú	_format_sµ   ó   zAccessLogger._format_sc                 C   r€   r�   )Úbody_lengthrv   r   r   r   Ú	_format_b¹   r„   zAccessLogger._format_bc                 C   s   t t|ƒƒS r�   ©r   Úroundrv   r   r   r   Ú	_format_T½   rx   zAccessLogger._format_Tc                 C   s   d| S )Nz%06fr   rv   r   r   r   Ú
_format_TfÁ   s   zAccessLogger._format_Tfc                 C   s   t t|d ƒƒS )Ni@B r‡   rv   r   r   r   Ú	_format_DÅ   s   zAccessLogger._format_Dc                    s   ‡ ‡‡fdd„| j D ƒS )Nc                    s    g | ]\}}||ˆ ˆˆƒf‘qS r   r   )Ú.0r   r   rv   r   r   Ú
<listcomp>Ì   s     z-AccessLogger._format_line.<locals>.<listcomp>)r8   )r9   rO   rP   rQ   r   rv   r   Ú_format_lineÉ   s   zAccessLogger._format_linec                 C   s   | j  tj¡S )zCheck if logger is enabled.)r0   ÚisEnabledForÚloggingÚINFO)r9   r   r   r   ÚenabledÎ   s   zAccessLogger.enabledc                 C   s¬   zE|   |||¡}tƒ }tƒ }|D ]%\}}| |¡ t|tƒ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?   ÚdictrF   Ú
isinstancer   r5   r0   Úinfor7   r   Ú	ExceptionÚ	exception)r9   rO   rP   rQ   Úfmt_infoÚvaluesr“   r   ÚvalueÚk1Úk2Údctr   r   r   ÚlogÔ   s    



 ÿzAccessLogger.log)3r   r   r   Ú__doc__rB   Ú
LOG_FORMATÚreÚcompiler@   rH   r-   r”   r   r   r?   r
   r   r.   r   rf   rg   r/   r   r�   ÚLoggerr4   r6   Ústaticmethodr   r	   rV   rX   r[   Úclassmethodrp   rs   rw   r   Úintrƒ   r†   r‰   rŠ   r‹   r   rŽ   ÚpropertyÚboolr’   rŸ   Ú__classcell__r   r   r;   r   r      sœ   
 õ

  )ÿÿÿÿþ	ÿÿÿÿþÿÿÿ
þ"r   )rf   rD   r�   rt   r¢   rQ   rc   Úcollections.abcr   Útypingr   r   r   Úabcr   Úweb_requestr   Úweb_responser	   r
   r   r   r   r   r   Ú<module>   s    