U
    HZûa&  ã                   @   sú  d Z ddlZddlZddlZddlZddlmZmZ ej 	dd¡sšej
 ¡ rneej
ƒeejƒkrne ej
¡e_
ej ¡ ršeejƒeejƒkrše ej¡e_ddlZej 	dd¡Zej 	dd	¡Zg ZdZd
ddddddddddddœZdZze W n ek
�r
   dZY nX dd„ Zz
ejZW n ek
�r<   dd„ ZY nX de_ dd„ ZG dd „ d eƒZeƒ Z e !d!ej"¡Z#G d"d#„ d#ej$ƒZ%G d$d%„ d%ej&ƒZ'G d&d'„ d'ej(ƒZ)da*d(d)„ Z+d*d+„ Z,d,d-„ Z-d.d/„ Z.d0d1„ Z/d2d3„ Z0d=d5d6„Z1d7d8„ Z2d>d;d<„Z3dS )?z2
logging, colors, terminal width and pretty-print
é    N)ÚUtilsÚansitermZNOSYNCFZWAF_LOG_FORMATz,%(asctime)s %(c1)s%(zone)s%(c2)s %(message)sZWAF_HOUR_FORMATz%H:%M:%STz[01;1mz[01;31mz[32mz[33mz[35mz[01;34mz[36mz[37mz[0mz[?25hz[?25l)ÚUSEZBOLDÚREDÚGREENÚYELLOWÚPINKZBLUEZCYANZGREYÚNORMALZ	cursor_onZ
cursor_offz
[K%s%s%sc                 C   sx   | dkrZt j ¡ s t j ¡ s d} tjr@tjdkr@tj 	dd¡}ntj 	dd¡}|dkrZd} | dkrldtjd< | t
d	< d
S )a3  
	If *1* is given, then the system will perform a few verifications
	before enabling colors, such as checking whether the interpreter
	is running in a terminal. A value of zero will disable colors,
	and a value above *1* will force colors.

	:param use: whether to enable colors or not
	:type use: integer
	é   r   ÚjavaZTERMÚ Údumb)r   ZemacsZvt100r   N)ÚsysÚstderrÚisattyÚstdoutr   Úis_win32ÚosÚnameÚenvironÚgetÚ
colors_lst)ÚuseZterm© r   ú&/wcdo/src/waf-tools/waf/waflib/Logs.pyÚenable_colors:   s    

r   c                   C   s   dS )NéP   r   r   r   r   r   Úget_term_colsY   s    r   zf
	Returns the console width in characters.

	:return: the number of characters per line
	:rtype: int
	c                 C   s   t d rt  | d¡S dS )zÂ
	Returns the ansi sequence corresponding to the given color name.
	An empty string is returned when coloring is globally disabled.

	:param cl: color name in capital letters
	:type cl: string
	r   r   )r   r   )Zclr   r   r   Ú	get_colorc   s    r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
color_dictz-attribute-based color access, eg: colors.PINKc                 C   s   t |ƒS ©N©r   ©ÚselfÚar   r   r   Ú__getattr__q   s    zcolor_dict.__getattr__c                 C   s   t |ƒS r    r!   r"   r   r   r   Ú__call__s   s    zcolor_dict.__call__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r&   r   r   r   r   r   o   s   r   z(\w+): (.*)c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )	Ú
log_filterzù
	Waf logs are of the form 'name: message', and can be filtered by 'waf --zones=name'.
	For example, the following::

		from waflib import Logs
		Logs.debug('test: here is a message')

	Will be displayed only when executing::

		$ waf --zones=test
	r   c                 C   s   t j | |¡ d S r    )ÚloggingÚFilterÚ__init__)r#   r   r   r   r   r.   …   s    zlog_filter.__init__c                 C   sl   |j |_|jtjkrdS t |j¡}|r@| d¡|_| d¡|_t	r\t
|ddƒt	kpZdt	kS tdkshdS dS )zO
		Filters log records by zone and by logging level

		:param rec: log entry
		Tr
   é   Úzoner   Ú*F)Úmoduler0   Úlevelnor,   ÚINFOÚre_logÚmatchÚmsgÚgroupÚzonesÚgetattrÚverbose)r#   ÚrecÚmr   r   r   Úfilterˆ   s    zlog_filter.filterN)r   )r'   r(   r)   r*   r.   r>   r   r   r   r   r+   y   s   
r+   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Úlog_handlerzDDispatches messages to stderr/stdout depending on the severity levelc              	   C   s–   zbz|j | _ W n> tk
rL   |jtjkr:tj |_ | _ ntj |_ | _ Y nX |  |¡ |  	¡  W n. t
tfk
r|   ‚ Y n   |  |¡ Y nX dS )zT
		Delegates the functionality to :py:meth:`waflib.Log.log_handler.emit_override`
		N)ÚstreamÚAttributeErrorr3   r,   ÚWARNINGr   r   r   Úemit_overrideÚflushÚKeyboardInterruptÚ
SystemExitZhandleError)r#   Úrecordr   r   r   ÚemitŸ   s    
zlog_handler.emitc                 K   sà   t |ddƒ| _| j}trÎ| j |¡}d| j }ztt|tƒr’t |ddƒr’| |j¡}z| 	|| ¡ W q  t
k
rŽ   | 	||  |j¡¡ Y q X n| 	|| ¡ W qÜ tk
rÊ   | 	||  d¡¡ Y qÜX ntj | |¡ dS )zA
		Writes the log record to the desired stream (stderr/stdout)
		Ú
terminatorÚ
z%sÚencodingNúutf-8)r:   rI   r@   ÚunicodeÚ	formatterÚformatÚ
isinstanceÚdecoderK   ÚwriteÚUnicodeEncodeErrorÚencodeÚUnicodeErrorr,   ÚStreamHandlerrH   )r#   rG   Úkwr@   r7   Zfsr   r   r   rC   ³   s     
zlog_handler.emit_overrideN)r'   r(   r)   r*   rH   rC   r   r   r   r   r?   �   s   r?   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )rN   z)Simple log formatter which handles colorsc                 C   s   t j | tt¡ d S r    )r,   Ú	Formatterr.   Ú
LOG_FORMATÚHOUR_FORMAT)r#   r   r   r   r.   Í   s    zformatter.__init__c                 C   s>  z|j  d¡}W n tk
r*   |j }Y nX td }|dkrF|j ¡ sN|dkr¾t|ddƒ}|dkr d}|jtj	krzt
j}n&|jtjkrŽt
j}n|jtjkr t
j}t|dt
jƒ}d	|||f }nt d
d|¡}|jtjk�r|j�rz||j W S  tk
�r   | d¡|j  Y S X |S ||_ t
j|_t
j|_tj | |¡S )z…
		Formats records and adds colors as needed. The records do not get
		a leading hour format if the logging level is above *INFO*.
		rL   r   r
   r/   Úc1Nr   Úc2z%s%s%sz\r(?!\n)|\x1B\[(K|.*?(m|h|l)))r7   rQ   Ú	Exceptionr   r@   r   r:   r3   r,   ZERRORÚcolorsr   rB   r   r4   r   r	   ÚreÚsubÚargsÚUnicodeDecodeErrorrT   r   r[   r\   rX   rO   )r#   r<   r7   r   r[   r\   r   r   r   rO   Ð   s:    zformatter.formatN)r'   r(   r)   r*   r.   rO   r   r   r   r   rN   Ë   s   rN   c                  O   s0   t r,t| ƒ} | d  dd¡| d< tj| |Ž dS )ui   
	Wraps logging.debug and discards messages if the verbosity level :py:attr:`waflib.Logs.verbose` â‰¤ 0
	r   rJ   ú N)r;   ÚlistÚreplaceÚlogÚdebug©ÚkrW   r   r   r   rg   þ   s    rg   c                  O   s„   t j| |Ž tdkr€t ¡ }|r€|dd… }g }|D ]6\}}}}| d|||f ¡ |r4| d| ¡  ¡ q4|r€t  d |¡¡ dS )uk   
	Wrap logging.errors, adds the stack trace when the verbosity level :py:attr:`waflib.Logs.verbose` â‰¥ 2
	r/   Néÿÿÿÿz  File %r, line %d, in %sz	%srJ   )rf   Úerrorr;   Ú	tracebackÚextract_stackÚappendÚstripÚjoin)ri   rW   ÚstÚbufÚfilenameÚlinenor   Úliner   r   r   rk     s    rk   c                  O   s   t j| |Ž dS )z
	Wraps logging.warning
	N)rf   Zwarningrh   r   r   r   Úwarn  s    rv   c                  O   s   t j| |Ž dS )z
	Wraps logging.info
	N)rf   Úinforh   r   r   r   rw     s    rw   c                  C   sN   t  d¡ag t_g t_tƒ } |  tƒ ¡ t | ¡ t 	t
ƒ ¡ t t j¡ dS )z5
	Initializes the logger :py:attr:`waflib.Logs.log`
	ÚwaflibN)r,   Ú	getLoggerrf   ÚhandlersÚfiltersr?   ÚsetFormatterrN   Ú
addHandlerZ	addFilterr+   ÚsetLevelÚDEBUG)Úhdlrr   r   r   Úinit_log$  s    

r�   c                 C   s`   t  |¡}tjdkrtjj}nd}t j| d|d�}t  d¡}| |¡ | 	|¡ | 
t j¡ |S )a8  
	Creates a simple logger, which is often used to redirect the context command output::

		from waflib import Logs
		bld.logger = Logs.make_logger('test.log', 'build')
		bld.check(header_name='sadlib.h', features='cxx cprogram', mandatory=False)

		# have the file closed immediately
		Logs.free_logger(bld.logger)

		# stop logging
		bld.logger = None

	The method finalize() of the command will try to free the logger, if any

	:param path: file name to write the log output to
	:type path: string
	:param name: logger name (loggers are reused)
	:type name: string
	i   NÚw)rK   ú%(message)s)r,   ry   r   Ú
hexversionr   rK   ZFileHandlerrX   r|   r}   r~   r   )Úpathr   ÚloggerrK   r€   rN   r   r   r   Úmake_logger2  s    





r‡   é    c                 C   sV   ddl m} t | ¡}|||d�}t d¡}| |¡ | |¡ ||_| tj	¡ |S )zL
	Creates a memory logger to avoid writing concurrently to the main logger
	r   )ÚMemoryHandler)Útargetrƒ   )
Zlogging.handlersr‰   r,   ry   rX   r|   r}   Z
memhandlerr~   r   )r   Úto_logÚsizer‰   r†   r€   rN   r   r   r   Úmake_mem_loggerS  s    



r�   c                 C   s<   z"| j D ]}| ¡  |  |¡ qW n tk
r6   Y nX dS )z°
	Frees the resources held by the loggers created through make_logger or make_mem_logger.
	This is used for file cleanup and for handler removal (logger objects are re-used).
	N)rz   ÚcloseZremoveHandlerr]   )r†   Úxr   r   r   Úfree_loggera  s    
r�   r   rJ   c                 C   s"   t dt| ƒ|tj|d|id� dS )aÊ  
	Prints messages in color immediately on stderr::

		from waflib import Logs
		Logs.pprint('RED', 'Something bad just happened')

	:param col: color name to use in :py:const:`Logs.colors_lst`
	:type col: string
	:param msg: message to display
	:type msg: string or a value that can be printed by %s
	:param label: a message to add after the colored output
	:type label: string
	:param sep: a string to append at the end (line separator)
	:type sep: string
	z	%s%s%s %srI   )ZextraN)rw   r^   r	   )Úcolr7   ÚlabelÚsepr   r   r   Úpprintm  s    r”   )rˆ   )r   rJ   )4r*   r   r_   rl   r   rx   r   r   r   r   r   r   ÚidÚ
__stdout__ZAnsiTermr   Ú
__stderr__r,   rY   rZ   r9   r;   r   Z	indicatorrM   Ú	NameErrorr   r   rA   r   Úobjectr   r^   ÚcompileÚMr5   r-   r+   rV   r?   rX   rN   rf   rg   rk   rv   rw   r�   r‡   r�   r�   r”   r   r   r   r   Ú<module>   sj    ô

$.0	!
