U
    Eh2                     @   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 zd dlZW n ek
r   d dlZY nX zd dlmZ W n ek
r   dZY nX d d	lmZ eZe Zd e_e	d
ZG dd deZer&erdd Z ndd Z ner6d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   A./venv/lib/python3.8/site-packages/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_errorI   s    r   c                 C   s"   | t kp | tko |jtjtjfkS r   )BrokenPipeErrorOSErrorr   r   r   r   r   r   r   r   O   s    c                 C   s   | t ko|jtjkS r   )r   r   r   r   r   r   r   r   U   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   Z   s       c              	   c   s.   t  j| 7  _z
dV  W 5 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)
_log_stateindentation)Znumr   r   r   
indent_loge   s    
r    c                   C   s   t tddS )Nr   r   )getattrr   r   r   r   r   get_indentationr   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'   x   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   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%s,%03d  c                    s   g | ]} | qS r   r   ).0lineprefixr   r   
<listcomp>   s   z-IndentingFormatter.format.<locals>.<listcomp>T)r&   r#   formatr4   r3   r$   Z
formatTimeZmsecsr"   join
splitlines)r(   recordr2   Zmessage_starttr+   r8   r   r;      s    zIndentingFormatter.format)r   r   r   r'   r4   r;   __classcell__r   r   r+   r   r#   v   s   
r#   c                     s    fdd}|S )Nc                    s   d t | tjjg S )Nr-   )r<   listr	   ZStyleZ	RESET_ALL)Zinpcolorsr   r   wrapped   s    z_color_wrap.<locals>.wrappedr   )rC   rD   r   rB   r   _color_wrap   s    rE   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(   rK   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	   rK   rD   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isattyTZTERMZANSI)r	   rI   
isinstancerK   rJ   rD   hasattrrQ   osenvironget)r(   Zreal_streamr   r   r   should_color   s    
z#ColorizedStreamHandler.should_colorc                 C   s@   t j| |}|  r<| jD ]\}}|j|kr||} q<q|S rG   )r.   rH   r;   rW   COLORSr3   )r(   r>   msglevelZcolorr   r   r   r;      s    
zColorizedStreamHandler.formatc                    s@   t  d d \}}|r0|  r0t||r0t tt| |S )Nr   )rM   exc_inforP   r   r   r&   rF   handleError)r(   r>   r   r   r+   r   r   r\      s    z"ColorizedStreamHandler.handleError)NN)r   r   r   r	   r.   r1   rE   r
   ZREDr/   ZYELLOWrX   r'   rP   rW   r;   r\   r@   r   r   r+   r   rF      s   

rF   c                   @   s   e Zd Zdd ZdS )BetterRotatingFileHandlerc                 C   s    t tj| j tjj| S rG   )	r   rT   pathdirnameZbaseFilenamer.   handlersRotatingFileHandler_openrO   r   r   r   rb      s    zBetterRotatingFileHandler._openN)r   r   r   rb   r   r   r   r   r]      s   r]   c                   @   s   e Zd Zdd Zdd ZdS )MaxLevelFilterc                 C   s
   || _ d S rG   )rZ   )r(   rZ   r   r   r   r'     s    zMaxLevelFilter.__init__c                 C   s   |j | jk S rG   )r3   rZ   r(   r>   r   r   r   filter  s    zMaxLevelFilter.filterN)r   r   r   r'   re   r   r   r   r   rc      s   rc   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 rG   )r&   rf   re   rd   r+   r   r   re     s    zExcludeLoggerFilter.filter)r   r   r   r   re   r@   r   r   r+   r   rf   	  s   rf   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/   r1   ZCRITICALINFONz	/dev/null)rl   r1   zext://sys.stdoutzext://sys.stderr)rN   stderrz2pip._internal.utils.logging.ColorizedStreamHandlerz5pip._internal.utils.logging.BetterRotatingFileHandler)rK   fileconsoleconsole_errorsconsole_subprocessuser_logFz*pip._internal.utils.logging.MaxLevelFilter)()rZ   zlogging.Filter)rs   namez/pip._internal.utils.logging.ExcludeLoggerFilter)exclude_warningsrestrict_to_subprocessexclude_subprocessz%(message)s)rs   r;   T)rs   r;   r$   )indentindent_with_timestamprK   rN   rw   ru   rx   )rZ   classrL   rK   filters	formatterrm   rv   rn   ry   )rZ   rz   filenameZdelayr|   )ro   rp   rq   rr   )rZ   r`   zpip._vendorrZ   )versionZdisable_existing_loggersr{   Z
formattersr`   rootZloggers)r!   r.   ZconfigZ
dictConfigr/   subprocess_loggerrt   r#   )	verbosityrL   Zuser_log_filerZ   Zlevel_numberZinclude_user_logZadditional_log_fileZ
root_levelZvendored_log_levelZlog_streamsZhandler_classesr`   r   r   r   setup_logging  s    
		$ Jr   )r   )/Z
__future__r   
contextlibr   r.   Zlogging.handlersrT   rM   r   r   Zpip._vendor.sixr   Zpip._internal.utils.compatr   Zpip._internal.utils.deprecationr   Zpip._internal.utils.miscr   	threadingImportErrorZdummy_threadingZpip._vendorr	   Z	_colorama	ExceptionZpip._vendor.coloramar
   localr   r   r   r   r   contextmanagerr    r"   Z	Formatterr#   rE   rH   rF   r`   ra   r]   rc   rf   r   r   r   r   r   <module>   sT   

		


2K	