a
    %0_                     @   s8  d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZmZ dZi ZeeZi ZeeZed	d
dd ed	ddd ed	ddd ed	ddd ed	ddd d ed	ddd d ed	ddd e
 ZG dd deZdd Zdd Zed	dd d!ed"fged#ejd$d%d&d' ZdS )(a|  log repository events to a blackbox for debugging

Logs event information to .hg/blackbox.log to help debug and diagnose problems.
The events that get logged can be configured via the blackbox.track and
blackbox.ignore config keys.

Examples::

  [blackbox]
  track = *
  ignore = pythonhook
  # dirty is *EXPENSIVE* (slow);
  # each log entry indicates `+` if the repository is dirty, like :hg:`id`.
  dirty = True
  # record the source of log messages
  logsource = True

  [blackbox]
  track = command, commandfinish, commandexception, exthook, pythonhook

  [blackbox]
  track = incoming

  [blackbox]
  # limit the size of a log file
  maxsize = 1.5 MB
  # rotate up to N log files when the current one gets too big
  maxfiles = 3

  [blackbox]
  # Include nanoseconds in log entries with %f (see Python function
  # datetime.datetime.strftime)
  date-format = '%Y-%m-%d @ %H:%M:%S.%f'

    )absolute_importN)_)hex)encodingloggingutil	registrar)dateutilprocutils   ships-with-hg-core   blackbox   dirtyF)default   maxsizes   1 MB	   logsource   maxfiles      trackc                   C   s   dgS N   * r   r   r   0/usr/lib/python3/dist-packages/hgext/blackbox.py<lambda>W       r      ignorec                   C   s   g dS )N)s	   chgservers	   cmdservers	   extensionr   r   r   r   r   r   \   r      date-formats   %Y/%m/%d %H:%M:%Sc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
blackboxloggerc                 C   sP   || _ t|dd| _t|dd| _|dd| _|dd| _d| _	d S )Nr
   r   r   r   r   F)
_reposetZ
configlist_trackedevents_ignoredeventsZ	configint	_maxfilesZconfigbytes_maxsize_inlog)selfuirepor   r   r   __init__f   s    zblackboxlogger.__init__c                 C   s   d| j v r|| jvp|| j v S r   )r   r   )r"   eventr   r   r   trackedn   s    zblackboxlogger.trackedc                 C   s8   | j r
d S d| _ z| |||| W d| _ nd| _ 0 d S )NTF)r!   _log)r"   r#   r&   msgoptsr   r   r   logs   s    zblackboxlogger.logc              
   C   sT  | dd}t||dd}t }dt  }d}	| jd  }
|
 }d	dd	 |D }|
dd
r~|
jddddr~d}	|
ddrd| }nd}zdd}||||	|||f}tj|| jjd| j| jd}|||  W d    n1 s0    Y  W nL ttfyH } z.| j  |dt|j  W Y d }~d S d }~0 0 | t_d S )Ns   devels   default-dater
   r   s   %dr      +c                 S   s   g | ]}t | qS r   )r   Znode).0pr   r   r   
<listcomp>   r   z'blackboxlogger._log.<locals>.<listcomp>r   TF)Zmissingmergebranchr   s    [%s]s   %s %s @%s%s (%s)%s> %s   blackbox.log)nameZmaxfilesmaxsizes*   warning: cannot write to blackbox.log: %s
)Z
configdater   ZdatestrZconfigr	   Zgetusergetpidr   parentsjoinZ
configboolZdirtyr   Zopenlogfilevfsr   r    writeIOErrorOSErrorr   cleardebugr   Z
strtolocalstrerror_lastloggerlogger)r"   r#   r&   r)   r*   r   dateuserpidZchangedctxr6   ZrevsrcZfmtargsfperrr   r   r   r(      sH    

0

zblackboxlogger._logN)__name__
__module____qualname__r%   r'   r+   r(   r   r   r   r   r   e   s   r   c                 C   s   |  dt d S )Nr
   )	setloggerr?   )r#   r   r   r   
uipopulate   s    rM   c                 C   sB   |  sd S t| |}| d| tjd u r2|t_|jd d S )Nr
   r2   )Zlocalr   rL   r?   r@   Z_wlockfreeprefixadd)r#   r$   r@   r   r   r   	reposetup   s    

rO      ls   limit
   s   the number of events to shows   hg blackbox [OPTION]...T)ZhelpcategoryZ	helpbasicc           
      O   s   |j dsdS |d}| dd}| d}d}g }t|D ].}	||krT qttd|	rh|d7 }||	 qD| 	d
t| dS )	z&view the recent repository events
    r2   Nlimit   r   
r   s+   ^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*   )r8   existsgetreadsplitreversedrematchappendZstatusr7   )
r#   r$   Zrevsr*   rR   rG   linescountoutputliner   r   r   blackbox   s    
rb   )__doc__Z
__future__r   r[   Zmercurial.i18nr   Zmercurial.noder   Z	mercurialr   r   r   Zmercurial.utilsr   r	   Z
testedwithZcmdtableZcommandZconfigtableZ
configitemZproxyloggerr?   objectr   rM   rO   ZCATEGORY_MAINTENANCErb   r   r   r   r   <module>	   s\   $	


A