a
    &0_v                     @   s   d dl mZ d dlZddlmZ ddlmZmZmZ dddZ	dd	 Z
d
d ZG dd deZG dd deZG dd deZdS )    )absolute_importN   )pycompat)dateutilprocutil
stringutilc           	         s    fdd}|dkrz |}W n ty6   Y nf0 |j|kr|}t|d ddD ]$}|d||d f d||f d q^|||dko|d d |d	d
dS )z{Open log file in append mode, with optional rotation

    If maxsize > 0, the log file will be rotated up to maxfiles.
    c              
      s   z | W nD tyR } z,|jtjkr> d||jf  W Y d }~n
d }~0 0 z|rf| | W nF ty } z.|jtjkr d|| |jf  W Y d }~n
d }~0 0 d S )Ns    warning: cannot remove '%s': %s
s(   warning: cannot rename '%s' to '%s': %s
)unlinkOSErrorerrnoZENOENTdebugstrerrorrename)oldpathnewpatherruivfs 7/usr/lib/python3/dist-packages/mercurial/loggingutil.pyrotate   s&    
zopenlogfile.<locals>.rotater   r   s   %s.%d)r   r   s   .1   aF)Zmakeparentdirs)statr	   st_sizejoinr   Zxrange)	r   r   namemaxfilesmaxsizer   stpathir   r   r   openlogfile   s    


r"   c                 C   s"   t jdd}t }d||| f S )Ns   %Y/%m/%d %H:%M:%S)formats   %s (%d)> %s)r   Zdatestrr   getpid)msgdatepidr   r   r   _formatlogline@   s    r(   c                 C   s   d|v p| |v S )N   *r   )eventtrackedr   r   r   _matcheventF   s    r,   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )
fileloggerz;Basic logger backed by physical file with optional rotationr   c                 C   s&   || _ || _t|| _|| _|| _d S N)_vfs_nameset_trackedevents	_maxfiles_maxsize)selfr   r   r+   r   r   r   r   r   __init__M   s
    
zfilelogger.__init__c                 C   s   t || jS r.   r,   r2   r5   r*   r   r   r   r+   T   s    zfilelogger.trackedc              
   C   s   t |}zHt|| j| j| j| jd}|| W d    n1 sD0    Y  W n> ty } z&|d| jt	
|f  W Y d }~n
d }~0 0 d S )N)r   r      cannot write to %s: %s
)r(   r"   r/   r0   r3   r4   writeIOErrorr   r   forcebytestr)r5   r   r*   r%   optslinefpr   r   r   r   logW   s"    ,zfilelogger.logN)r   r   __name__
__module____qualname____doc__r6   r+   r@   r   r   r   r   r-   J   s   
r-   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	fileobjectloggerz'Basic logger backed by file-like objectc                 C   s   || _ t|| _d S r.   )_fpr1   r2   )r5   r?   r+   r   r   r   r6   l   s    zfileobjectlogger.__init__c                 C   s   t || jS r.   r7   r8   r   r   r   r+   p   s    zfileobjectlogger.trackedc              
   C   sn   t |}z| j| | j  W nF tyh } z.|dt| jjt|f  W Y d }~n
d }~0 0 d S )Nr9   )	r(   rG   r:   flushr;   r   r   r<   r   )r5   r   r*   r%   r=   r>   r   r   r   r   r@   s   s    zfileobjectlogger.logNrA   r   r   r   r   rF   i   s   rF   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	proxyloggerz4Forward log events to another logger to be set laterc                 C   s
   d | _ d S r.   )logger)r5   r   r   r   r6      s    zproxylogger.__init__c                 C   s   | j d uo| j |S r.   )rJ   r+   r8   r   r   r   r+      s    zproxylogger.trackedc                 C   s$   | j d usJ | j |||| d S r.   )rJ   r@   )r5   r   r*   r%   r=   r   r   r   r@      s    zproxylogger.logNrA   r   r   r   r   rI      s   rI   )r   r   )Z
__future__r   r
    r   Zutilsr   r   r   r"   r(   r,   objectr-   rF   rI   r   r   r   r   <module>	   s   
*