B
    `d%3                 @   s  d dl mZ 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m	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ yd dlZW n ek
r   d dlZY nX yd dlmZ W n ek
r   dZY nX d d	lmZ eZe Ze	d
ZG dd deZer erdd Zndd Zner0dd Zndd Zej d%ddZ!dd Z"G dd dej#Z$dd Z%G dd dej&Z'G dd dej(j)Z*G dd  d eZ+G d!d" d"eZ,d#d$ Z-dS )&    )absolute_importN)Filter	getLogger)PY2)WINDOWS)DEPRECATION_MSG_PREFIX)
ensure_dir)colorama)Forezpip.subprocessorc               @   s   e Zd ZdZdS )BrokenStdoutLoggingErrorzO
    Raised if BrokenPipeError occurs for the stdout stream while logging.
    N)__name__
__module____qualname____doc__ r   r   ?/tmp/pip-unpacked-wheel-hu9qqap1/pip/_internal/utils/logging.pyr   :   s   r   c             C   s   | t ko|jtjtjfkS )z1See the docstring for non-Windows Python 3 below.)IOErrorerrnoEINVALEPIPE)	exc_classexcr   r   r   _is_broken_pipe_errorH   s    r   c             C   s"   | t kp | tko |jtjtjfkS )z1See the docstring for non-Windows Python 3 below.)BrokenPipeErrorOSErrorr   r   r   )r   r   r   r   r   r   N   s    c             C   s   | t ko|jtjkS )z1See the docstring for non-Windows Python 3 below.)r   r   r   )r   r   r   r   r   r   T   s    c             C   s   | t kS )z
        Return whether an exception is a broken pipe error.

        Args:
          exc_class: an exception class.
          exc: an exception instance.
        )r   )r   r   r   r   r   r   Y   s       c          	   c   s6   t  t_t j| 7  _z
dV  W dt j| 8  _X dS )zv
    A context manager which will cause the log output to be indented for any
    log messages emitted inside it.
    N)get_indentation
_log_stateindentation)numr   r   r   
indent_logd   s
    
r    c               C   s   t tddS )Nr   r   )getattrr   r   r   r   r   r   s   s    r   c                   s0   e Zd Z fddZdd Z fddZ  ZS )IndentingFormatterc                s$   | dd| _tt| j|| dS )z
        A logging.Formatter that obeys the indent_log() context manager.

        :param add_timestamp: A bool indicating output lines should be prefixed
            with their record's timestamp.
        add_timestampFN)popr#   superr"   __init__)selfargskwargs)	__class__r   r   r&   y   s    zIndentingFormatter.__init__c             C   s.   |t jk rdS |trdS |t jk r*dS dS )zv
        Return the start of the formatted log message (not counting the
        prefix to add to each line).
         z	WARNING: zERROR: )loggingWARNING
startswithr   ERROR)r'   	formattedlevelnor   r   r   get_message_start   s    


z$IndentingFormatter.get_message_startc                sz   t t| |}| ||j}|| }d | jrJ| |d}djf t   dt  7  d	 fdd|
dD }|S )z
        Calls the standard formatter, but will indent all of the log message
        lines by our current indentation level.
        r+   z%Y-%m-%dT%H:%M:%Sz{t},{record.msecs:03.0f}  c                s   g | ]} | qS r   r   ).0line)prefixr   r   
<listcomp>   s   z-IndentingFormatter.format.<locals>.<listcomp>T)r%   r"   formatr2   r1   r#   
formatTimelocalsr   join
splitlines)r'   recordr0   message_startt)r*   )r6   r   r8      s    
zIndentingFormatter.format)r   r   r   r&   r2   r8   __classcell__r   r   )r*   r   r"   w   s   
r"   c                 s    fdd}|S )Nc                s   d t | tjjg S )Nr+   )r;   listr	   Style	RESET_ALL)inp)colorsr   r   wrapped   s    z_color_wrap.<locals>.wrappedr   )rE   rF   r   )rE   r   _color_wrap   s    rG   c                   sh   e Zd Zer.ejeejfej	eej
fgZng ZdddZdd Zdd Zdd	 Z fd
dZ  ZS )ColorizedStreamHandlerNc             C   s.   t j| | || _tr*tr*t| j| _d S )N)r,   StreamHandlerr&   	_no_colorr   r	   AnsiToWin32stream)r'   rL   no_colorr   r   r   r&      s    zColorizedStreamHandler.__init__c             C   s"   t rtr| jjtjkS | jtjkS )zA
        Return whether the handler is using sys.stdout.
        )r   r	   rL   rF   sysstdout)r'   r   r   r   _using_stdout   s    z$ColorizedStreamHandler._using_stdoutc             C   sX   t r
| jrdS t| jt js"| jn| jj}t|dr@| r@dS tj	
ddkrTdS dS )NFisattyTTERMANSI)r	   rJ   
isinstancerL   rK   rF   hasattrrQ   osenvironget)r'   real_streamr   r   r   should_color   s    
z#ColorizedStreamHandler.should_colorc             C   sB   t j| |}|  r>x&| jD ]\}}|j|kr||}P qW |S )N)r,   rI   r8   rZ   COLORSr1   )r'   r=   msglevelcolorr   r   r   r8      s    
zColorizedStreamHandler.formatc                s@   t  d d \}}|r0|  r0t||r0t tt| |S )Nr   )rN   exc_inforP   r   r   r%   rH   handleError)r'   r=   r   r   )r*   r   r   r`      s
    
z"ColorizedStreamHandler.handleError)NN)r   r   r   r	   r,   r/   rG   r
   REDr-   YELLOWr[   r&   rP   rZ   r8   r`   r@   r   r   )r*   r   rH      s   

rH   c               @   s   e Zd Zdd ZdS )BetterRotatingFileHandlerc             C   s    t tj| j tjj| S )N)	r   rV   pathdirnamebaseFilenamer,   handlersRotatingFileHandler_open)r'   r   r   r   ri      s    zBetterRotatingFileHandler._openN)r   r   r   ri   r   r   r   r   rc      s   rc   c               @   s   e Zd Zdd Zdd ZdS )MaxLevelFilterc             C   s
   || _ d S )N)r]   )r'   r]   r   r   r   r&     s    zMaxLevelFilter.__init__c             C   s   |j | jk S )N)r1   r]   )r'   r=   r   r   r   filter  s    zMaxLevelFilter.filterN)r   r   r   r&   rk   r   r   r   r   rj     s   rj   c                   s    e Zd ZdZ fddZ  ZS )ExcludeLoggerFilterzQ
    A logging Filter that excludes records from a logger (or its children).
    c                s   t t| | S )N)r%   rl   rk   )r'   r=   )r*   r   r   rk     s    zExcludeLoggerFilter.filter)r   r   r   r   rk   r@   r   r   )r*   r   rl   
  s   rl   c             C   sf  | dkrd}n.| dkrd}n | dkr*d}n| dkr8d}nd	}t t|}|d
k	}|r\|}d}nd}|}|dkrpdnd}ddd}	ddd}
dddg|rdgng  }tjdddtjddtjddtjddtddtdd d!d"||
d# ||	d$ d%d&gd'd(d|
d# ||	d) d%gd'd(||
d# ||	d) d*gd'd(d|
d+ |d d,d-d.||d/d0d1|iid2 |S )3znConfigures and sets up all of the logging

    Returns the requested logging level, as its integer value.
       DEBUGr-   r/   CRITICALINFONz	/dev/null)rs   r/   zext://sys.stdoutzext://sys.stderr)rO   stderrz2pip._internal.utils.logging.ColorizedStreamHandlerz5pip._internal.utils.logging.BetterRotatingFileHandler)rL   fileconsoleconsole_errorsconsole_subprocessuser_logFz*pip._internal.utils.logging.MaxLevelFilter)z()r]   zlogging.Filter)z()namez/pip._internal.utils.logging.ExcludeLoggerFilter)exclude_warningsrestrict_to_subprocessexclude_subprocessz%(message)s)z()r8   T)z()r8   r#   )indentindent_with_timestamprL   rO   r}   r{   r~   )r]   classrM   rL   filters	formatterrt   r|   ru   r   )r]   r   filenamedelayr   )rv   rw   rx   ry   )r]   rg   zpip._vendorr]   )versiondisable_existing_loggersr   
formattersrg   rootloggers)r!   r,   config
dictConfigr-   subprocess_loggerrz   r"   )	verbosityrM   user_log_filer]   level_numberinclude_user_logadditional_log_file
root_levelvendored_log_levellog_streamshandler_classesrg   r   r   r   setup_logging  s    


r   )r   ).
__future__r   
contextlibr   r,   logging.handlersrV   rN   r   r   Zpip._vendor.sixr   pip._internal.utils.compatr   pip._internal.utils.deprecationr   pip._internal.utils.miscr   	threadingImportErrorZdummy_threadingpip._vendorr	   	_colorama	ExceptionZpip._vendor.coloramar
   localr   r   r   r   contextmanagerr    r   	Formatterr"   rG   rI   rH   rg   rh   rc   rj   rl   r   r   r   r   r   <module>   sR   

		


2K	