3
^Ã_^&  ã               @   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j	ddƒsšej
jƒ rneej
ƒeejƒkrnejej
ƒe_
ejjƒ ršeejƒeejƒkršejejƒe_ddlZejj	ddƒZejj	dd	ƒZg ZdZd
ddddddddddddœZdZye W n ek
�r
   dZY nX dd„ Zy
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j!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jƒ pt jjƒ s d} tjr@tjdkr@tjj	ddƒ}ntj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ÚemacsZvt100r   N)r   r   )ÚsysÚstderrÚisattyÚstdoutr   Úis_win32ÚosÚnameÚenvironÚgetÚ
colors_lst)ÚuseZterm© r   ú6/home/yuhw/wc/wct-package/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 j| 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 )N)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j| |ƒ d S )N)Ú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|jƒ}|r@|jdƒ|_|j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)Úmoduler.   Ú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–   yby|j | _ W n> tk
rL   |jtjkr:tj |_ | _ ntj |_ | _ Y nX | j|ƒ | j	ƒ  W n. t
tfk
r|   ‚ Y n   | j|ƒ Y nX dS )zT
		Delegates the functionality to :py:meth:`waflib.Log.log_handler.emit_override`
		N)ÚstreamÚAttributeErrorr1   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j|ƒ}d| j }ytt|tƒr’t |ddƒr’|j|jƒ}y|j	|| ƒ W q  t
k
rŽ   |j	|| j|jƒƒ Y q X n|j	|| ƒ W qÜ tk
rÊ   |j	|| jdƒƒ Y qÜX ntjj| |ƒ dS )zA
		Writes the log record to the desired stream (stderr/stdout)
		Ú
terminatorÚ
z%sÚencodingNzutf-8)r8   rG   r>   ÚunicodeÚ	formatterÚformatÚ
isinstanceÚdecoderI   ÚwriteÚUnicodeEncodeErrorÚencodeÚUnicodeErrorr*   ÚStreamHandlerrF   )r!   rE   Úkwr>   r5   Zfsr   r   r   rA   ³   s     
zlog_handler.emit_overrideN)r%   r&   r'   r(   rF   rA   r   r   r   r   r=   �   s   r=   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )rK   z)Simple log formatter which handles colorsc             C   s   t jj| ttƒ d S )N)r*   Ú	Formatterr,   Ú
LOG_FORMATÚHOUR_FORMAT)r!   r   r   r   r,   Í   s    zformatter.__init__c             C   s8  y|j jdƒ}W n tk
r*   |j }Y nX td }|dkrF|j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jd
d|ƒ}|jtjk�r|j�ry
||j S  tk
�r   |jdƒ|j S X |S ||_ t
j|_t
j|_tj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*.
		zutf-8r   r
   r-   Úc1Nr   Úc2z%s%s%sz\r(?!\n)|\x1B\[(K|.*?(m|h|l)))r5   rN   Ú	Exceptionr   r>   r   r8   r1   r*   ZERRORÚcolorsr   r@   r   r2   r   r	   ÚreÚsubÚargsÚUnicodeDecodeErrorrQ   r   rX   rY   rU   rL   )r!   r:   r5   r   rX   rY   r   r   r   rL   Ð   s:    
zformatter.formatN)r%   r&   r'   r(   r,   rL   r   r   r   r   rK   Ë   s   rK   c              O   s0   t r,t| ƒ} | d j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   rH   ú N)r9   ÚlistÚreplaceÚlogÚdebug)ÚkrT   r   r   r   rd   þ   s    rd   c              O   sˆ   t j| |Ž tdkr„tjƒ }|r„|dd… }g }x>|D ]6\}}}}|jd|||f ƒ |r6|jd|jƒ  ƒ q6W |r„t jdj|ƒƒ dS )uk   
	Wrap logging.errors, adds the stack trace when the verbosity level :py:attr:`waflib.Logs.verbose` â‰¥ 2
	r-   Nr
   z  File %r, line %d, in %sz	%srH   éÿÿÿÿ)rc   Úerrorr9   Ú	tracebackÚextract_stackÚappendÚstripÚjoin)re   rT   ÚstÚbufÚfilenameÚlinenor   Úliner   r   r   rg     s    rg   c              O   s   t j| |Ž dS )z
	Wraps logging.warning
	N)rc   Zwarning)re   rT   r   r   r   Úwarn  s    rr   c              O   s   t j| |Ž dS )z
	Wraps logging.info
	N)rc   Úinfo)re   rT   r   r   r   rs     s    rs   c              C   sN   t jdƒag t_g t_tƒ } | jtƒ ƒ tj| ƒ tj	t
ƒ ƒ tjt jƒ dS )z5
	Initializes the logger :py:attr:`waflib.Logs.log`
	ÚwaflibN)r*   Ú	getLoggerrc   ÚhandlersÚfiltersr=   ÚsetFormatterrK   Ú
addHandlerZ	addFilterr)   ÚsetLevelÚDEBUG)Úhdlrr   r   r   Úinit_log$  s    

r}   c             C   s`   t j|ƒ}tjdkrtjj}nd}t j| d|d�}t jdƒ}|j|ƒ |j	|ƒ |j
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)rI   z%(message)s)r*   ru   r   Ú
hexversionr   rI   ZFileHandlerrU   rx   ry   rz   r{   )Úpathr   ÚloggerrI   r|   rK   r   r   r   Úmake_logger2  s    





r‚   é    c             C   sV   ddl m} tj| ƒ}|||d�}tjdƒ}|j|ƒ |j|ƒ ||_|jtj	ƒ |S )zL
	Creates a memory logger to avoid writing concurrently to the main logger
	r   )ÚMemoryHandler)Útargetz%(message)s)
Zlogging.handlersr„   r*   ru   rU   rx   ry   Z
memhandlerrz   r{   )r   Úto_logÚsizer„   r�   r|   rK   r   r   r   Úmake_mem_loggerS  s    



rˆ   c             C   s@   y&x | j D ]}|jƒ  | j|ƒ q
W W n tk
r:   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)rv   ÚcloseZremoveHandlerrZ   )r�   Úxr   r   r   Úfree_loggera  s    r‹   r   rH   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 %srG   )ZextraN)rs   r[   r	   )Úcolr5   ÚlabelÚsepr   r   r   Úpprintm  s    r�   )rƒ   )r   rH   )4r(   r   r\   rh   r   rt   r   r   r   r   r   r   ÚidÚ
__stdout__ZAnsiTermr   Ú
__stderr__r*   rV   rW   r7   r9   r   Z	indicatorrJ   Ú	NameErrorr   r   r?   r   Úobjectr    r[   ÚcompileÚMr3   r+   r)   rS   r=   rU   rK   rc   rd   rg   rr   rs   r}   r‚   rˆ   r‹   r�   r   r   r   r   Ú<module>   sh    

$.0	!
