a
    ”$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y   dZY n0 dd„ Zz
ejZW n ey8   dd„ ZY n0 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BLUEÚCYANZGREYÚNORMALÚ	cursor_onÚ
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v 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   ÚjavaÚTERMÚ Údumb)r   ZemacsZvt100r   N)ÚsysÚstderrÚisattyÚstdoutr   Úis_win32ÚosÚnameÚenvironÚgetÚ
colors_lst)ÚuseZterm© r   úf/home/jack/SDK/ZBT-AX3000-OpenWrt-21.02/build_dir/hostpkg/samba-4.14.12/third_party/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   r2   …   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	v pZdt	v 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)Úmoduler4   Úlevelnor0   Ú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.   r2   rB   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’   z`z|j | _ W n< tyJ   |jtjkr8tj |_ | _ ntj |_ | _ Y n0 |  |¡ |  	¡  W n, t
tfyx   ‚ Y n   |  |¡ Y n0 dS )zT
		Delegates the functionality to :py:meth:`waflib.Log.log_handler.emit_override`
		N)ÚstreamÚAttributeErrorr7   r0   Ú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 }zrt|tƒrt |ddƒr| |j¡}z| 	|| ¡ W qž t
yŒ   | 	||  |j¡¡ Y qž0 n| 	|| ¡ W qØ tyÆ   | 	||  d¡¡ Y qØ0 ntj | |¡ dS )zA
		Writes the log record to the desired stream (stderr/stdout)
		Ú
terminatorÚ
z%sÚencodingNúutf-8)r>   rM   rD   ÚunicodeÚ	formatterÚformatÚ
isinstanceÚdecoderO   ÚwriteÚUnicodeEncodeErrorÚencodeÚUnicodeErrorr0   ÚStreamHandlerrL   )r'   rK   ÚkwrD   r;   Úfsr   r   r   rG   ³   s     
zlog_handler.emit_overrideN)r+   r,   r-   r.   rL   rG   r   r   r   r   rC      s   rC   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )rR   z)Simple log formatter which handles colorsc                 C   s   t j | tt¡ d S r$   )r0   Ú	Formatterr2   Ú
LOG_FORMATÚHOUR_FORMAT)r'   r   r   r   r2   Í   s    zformatter.__init__c                 C   s:  z|j  d¡}W n ty(   |j }Y n0 td }|dkrD|j ¡ sL|dkr¼t|ddƒ}|du ržd}|jtj	krxt
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y   | d¡|j  Y S 0 |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*.
		rP   r   r   r3   Úc1Nr   Úc2z%s%s%sz\r(?!\n)|\x1B\[(K|.*?(m|h|l)))r;   rU   Ú	Exceptionr   rD   r   r>   r7   r0   ZERRORÚcolorsr   rF   r   r8   r   r
   ÚreÚsubÚargsÚUnicodeDecodeErrorrX   r   r`   ra   r]   rS   )r'   r@   r;   r   r`   ra   r   r   r   rS   Ð   s:    zformatter.formatN)r+   r,   r-   r.   r2   rS   r   r   r   r   rR   Ë   s   rR   c                  O   s4   t r0t| ƒ} | d  dd¡| d< tj| i |¤Ž dS )ui   
	Wraps logging.debug and discards messages if the verbosity level :py:attr:`waflib.Logs.verbose` â‰¤ 0
	r   rN   ú N)r?   ÚlistÚreplaceÚlogÚdebug©Úkr[   r   r   r   rl   þ   s    rl   c                  O   sˆ   t j| i |¤Ž tdkr„t ¡ }|r„|dd… }g }|D ]6\}}}}| d|||f ¡ |r8| d| ¡  ¡ q8|r„t  d |¡¡ dS )uk   
	Wrap logging.errors, adds the stack trace when the verbosity level :py:attr:`waflib.Logs.verbose` â‰¥ 2
	r3   Néÿÿÿÿz  File %r, line %d, in %sz	%srN   )rk   Úerrorr?   Ú	tracebackÚextract_stackÚappendÚstripÚjoin)rn   r[   ÚstÚbufÚfilenameÚlinenor   Úliner   r   r   rp     s    rp   c                  O   s   t j| i |¤Ž dS )z
	Wraps logging.warning
	N)rk   Zwarningrm   r   r   r   Úwarn  s    r{   c                  O   s   t j| i |¤Ž dS )z
	Wraps logging.info
	N)rk   Úinform   r   r   r   r|     s    r|   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)r0   Ú	getLoggerrk   ÚhandlersÚfiltersrC   ÚsetFormatterrR   Ú
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)rO   ú%(message)s)r0   r~   r   Ú
hexversionr   rO   ZFileHandlerr]   r   r‚   rƒ   r„   )Úpathr   ÚloggerrO   r…   rR   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Ž   r0   r~   r]   r   r‚   Z
memhandlerrƒ   r„   )r   Zto_logÚsizerŽ   r‹   r…   rR   r   r   r   Úmake_mem_loggerS  s    



r‘   c                 C   s:   z"| j D ]}| ¡  |  |¡ qW n ty4   Y n0 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)r   ÚcloseZremoveHandlerrb   )r‹   Úxr   r   r   Úfree_loggera  s    
r”   r   rN   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 %srM   )ÚextraN)r|   rc   r
   )Úcolr;   ÚlabelÚsepr   r   r   Úpprintm  s    r™   )r   )r   rN   )4r.   r   rd   rq   r   r}   r   r   r   r   r   r   ÚidÚ
__stdout__ZAnsiTermr   Ú
__stderr__r0   r^   r_   r=   r?   r   Z	indicatorrQ   Ú	NameErrorr   r!   rE   r"   Úobjectr#   rc   ÚcompileÚMr9   r1   r/   rZ   rC   r]   rR   rk   rl   rp   r{   r|   r†   rŒ   r‘   r”   r™   r   r   r   r   Ú<module>   sj    ô

$.0	!
