B
    Ù‹d{L  ã               @   sL  d 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 ddlm	Z
 ddlmZ ddlmZ ddlmZmZ d	d
dddgZdddddddgZxeD ]Zeeeƒeƒ e< q’W ee7 ZdaG dd„ deƒZG dd„ deƒZG dd	„ d	e
jƒZeƒ Zdd„ Zdd„ Ze ¡ Z G dd„ de ƒZ!G dd„ dej"ƒZ"G d d!„ d!ƒZ#G d"d#„ d#ej$ƒZ%dS )$zâThis module defines a logging class based on the built-in logging module.

.. note::

    This module is meant for internal ``astropy`` usage. For use in other
    packages, we recommend implementing your own logger instead.

é    N)Úcontextmanageré   )Úconfig)Úconf)Úfind_current_module)ÚAstropyWarningÚAstropyUserWarningÚConfr   ÚlogÚAstropyLoggerÚLoggingErrorÚNOTSETÚDEBUGÚINFOÚWARNINGÚERRORÚCRITICALÚFATALc               @   s   e Zd ZdZdS )r   z—
    This exception is for various errors that occur in the astropy logger,
    typically when activating or deactivating logger-related features.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú[/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/logger.pyr   &   s   c               @   s   e Zd ZdZdS )Ú_AstLogIPYExczé
    An exception that is used only as a placeholder to indicate to the
    IPython exception-catching mechanism that the astropy
    exception-capturing is activated. It should not actually be used as
    an exception anywhere.
    N)r   r   r   r   r   r   r   r   r   -   s   r   c               @   sp   e Zd ZdZe dd¡Ze dd¡Ze dd¡Ze dd¡Z	e d	d
¡Z
e dd¡Ze dd¡Ze d	d¡ZdS )r	   z8
    Configuration parameters for `astropy.logger`.
    r   z®Threshold for the logging messages. Logging messages that are less severe than this level will be ignored. The levels are ``'DEBUG'``, ``'INFO'``, ``'WARNING'``, ``'ERROR'``.Tz%Whether to log `warnings.warn` calls.Fz.Whether to log exceptions before raising them.z-Whether to always log messages to a log file.Ú z€The file to log messages to.  If empty string is given, it defaults to a file ``'astropy.log'`` in the astropy config directory.z2Threshold for logging messages to `log_file_path`.z3%(asctime)r, %(origin)r, %(levelname)r, %(message)rzFormat for log file entries.z~The encoding (e.g., UTF-8) to use for the log file.  If empty string is given, it defaults to the platform-preferred encoding.N)r   r   r   r   Ú_configZ
ConfigItemÚ	log_levelÚlog_warningsÚlog_exceptionsÚlog_to_fileÚlog_file_pathÚlog_file_levelÚlog_file_formatÚlog_file_encodingr   r   r   r   r	   6   s2   c           	   C   s:   t  ¡ } t  t¡ zt  d¡at ¡  W dt  | ¡ X tS )zpInitializes the Astropy log--in most circumstances this is called
    automatically when importing astropy.
    ÚastropyN)ÚloggingÚgetLoggerClassÚsetLoggerClassr   Ú	getLoggerr
   Ú_set_defaults)Zorig_logger_clsr   r   r   Ú	_init_loga   s    

r+   c              C   sŒ   t  ¡ rt  ¡  t  ¡ r t  ¡  b yPt ¡  z8tjjj	} x(|  
¡ D ]}|dksX| d¡rB| |= qBW W dt ¡  X W n tk
r†   Y nX dS )a  Shut down exception and warning logging (if enabled) and clear all
    Astropy loggers from the logging module's cache.

    This involves poking some logging module internals, so much if it is 'at
    your own risk' and is allowed to pass silently if any exceptions occur.
    r%   zastropy.N)r
   Úexception_logging_enabledÚdisable_exception_loggingÚwarnings_logging_enabledÚdisable_warnings_loggingr&   Ú_acquireLockÚLoggerÚmanagerÚ
loggerDictÚkeysÚ
startswithÚ_releaseLockÚ	Exception)r3   Úkeyr   r   r   Ú_teardown_logs   s    

r9   c               @   s–   e Zd ZdZddd„ZdZdd„ Zdd„ Zd	d
„ Zdd„ Z	dZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zed dd„ƒZed!dd„ƒZdd„ ZdS )"r   a‹  
    This class is used to set up the Astropy logging.

    The main functionality added by this class over the built-in
    logging.Logger class is the ability to keep track of the origin of the
    messages, the ability to enable logging of warnings.warn calls and
    exceptions, and the addition of colorized output and context managers to
    easily capture messages to a file or list.
    Nc             C   s`   |	d kri }	d|	kr@t dddgd}|d k	r8|j|	d< nd|	d< tj| |||||||||	|
dS )NÚoriginr   Tr&   )ZfinddiffÚunknown)ÚfuncÚextraÚsinfo)r   r   r1   Ú
makeRecord)ÚselfÚnameÚlevelÚpathnameÚlinenoÚmsgÚargsÚexc_infor<   r=   r>   Zcurrent_moduler   r   r   r?   ¡   s    zAstropyLogger.makeRecordc          	   O   sü   t |d tƒs| j||ŽS |d }t|ƒttfkrJ|jj› d|d › }nt|d ƒ}|d }d }tj	 
|¡\}}x^ttj ¡ ƒD ]L\}}	ytj	 
t|	ddƒ¡d }
W n tk
r¼   w‚Y nX |
|kr‚|	j}P q‚W |d k	rî| j|d|id n
|  |¡ d S )Nr   z: é   Ú__file__r   r:   )r=   )Ú
isinstancer   Ú_showwarning_origÚtyper   Ú	__class__r   ÚstrÚosÚpathÚsplitextÚlistÚsysÚmodulesÚitemsÚgetattrr7   Úwarning)r@   rF   ÚkwargsrW   ÚmessageZmod_pathÚmod_nameÚextrA   ÚmodrP   r   r   r   Ú_showwarning±   s(    zAstropyLogger._showwarningc             C   s
   | j d k	S )N)rK   )r@   r   r   r   r.   ×   s    z&AstropyLogger.warnings_logging_enabledc             C   s$   |   ¡ rtdƒ‚tj| _| jt_dS )aF  
        Enable logging of warnings.warn() calls

        Once called, any subsequent calls to ``warnings.warn()`` are
        redirected to this logger and emitted with level ``WARN``. Note that
        this replaces the output from ``warnings.warn``.

        This can be disabled with ``disable_warnings_logging``.
        z)Warnings logging has already been enabledN)r.   r   ÚwarningsÚshowwarningrK   r]   )r@   r   r   r   Úenable_warnings_loggingÚ   s    
z%AstropyLogger.enable_warnings_loggingc             C   s6   |   ¡ stdƒ‚tj| jkr$tdƒ‚| jt_d| _dS )zï
        Disable logging of warnings.warn() calls

        Once called, any subsequent calls to ``warnings.warn()`` are no longer
        redirected to this logger.

        This can be re-enabled with ``enable_warnings_logging``.
        z%Warnings logging has not been enabledzhCannot disable warnings logging: warnings.showwarning was not set by this logger, or has been overriddenN)r.   r   r^   r_   r]   rK   )r@   r   r   r   r/   é   s    	z&AstropyLogger.disable_warnings_loggingc             C   sš   |d krd }n$|}x|j d k	r&|j }qW t |¡}t|jƒdkrV|j› dt|ƒ› }n
t|jƒ}|d k	r~| j|d|jid n
|  |¡ |  |||¡ d S )Nr   z: r:   )r=   )	Útb_nextÚinspectÚ	getmoduleÚlenrF   r   rN   ÚerrorÚ_excepthook_orig)r@   ÚetypeÚvalueÚ	tracebackr\   ÚtbrY   r   r   r   Ú_excepthooký   s    



zAstropyLogger._excepthookc             C   sD   y
t ƒ }W n tk
r"   d}Y nX |dkr6| jdk	S t|jkS dS )z¼
        Determine if the exception-logging mechanism is enabled.

        Returns
        -------
        exclog : bool
            True if exception logging is on, False if not.
        N)Úget_ipythonÚ	NameErrorrf   r   Zcustom_exceptions)r@   Úipr   r   r   r,     s    	


z'AstropyLogger.exception_logging_enabledc                sz   y
t ƒ }W n tk
r"   d}Y nX ˆ  ¡ r4tdƒ‚|dkrNtjˆ _ˆ jt_n(d‡ fdd„	}| t	t
f|¡ dd„ ˆ _dS )zð
        Enable logging of exceptions

        Once called, any uncaught exceptions will be emitted with level
        ``ERROR`` by this logger, before being raised.

        This can be disabled with ``disable_exception_logging``.
        Nz*Exception logging has already been enabledc                s&   ˆ   |||¡ | j|||f|d d S )N)Ú	tb_offset)rk   Úshowtraceback)Zipyshellrg   Úevaluerj   ro   )r@   r   r   Úipy_exc_handler@  s    z?AstropyLogger.enable_exception_logging.<locals>.ipy_exc_handlerc             S   s   d S )Nr   )rg   rq   rj   r   r   r   Ú<lambda>M  ó    z8AstropyLogger.enable_exception_logging.<locals>.<lambda>)N)rl   rm   r,   r   rS   Ú
excepthookrf   rk   Úset_custom_excÚBaseExceptionr   )r@   rn   rr   r   )r@   r   Úenable_exception_logging&  s    	



z&AstropyLogger.enable_exception_loggingc             C   sr   y
t ƒ }W n tk
r"   d}Y nX |  ¡ s4tdƒ‚|dkr`tj| jkrPtdƒ‚| jt_d| _n| t	ƒ d¡ dS )zÒ
        Disable logging of exceptions

        Once called, any uncaught exceptions will no longer be emitted by this
        logger.

        This can be re-enabled with ``enable_exception_logging``.
        Nz&Exception logging has not been enabledzcCannot disable exception logging: sys.excepthook was not set by this logger, or has been overridden)
rl   rm   r,   r   rS   ru   rk   rf   rv   Útuple)r@   rn   r   r   r   r-   O  s    	

z'AstropyLogger.disable_exception_loggingc             C   s
   dt _dS )z)
        Enable colorized output
        TN)Ú_confÚ	use_color)r@   r   r   r   Úenable_colorl  s    zAstropyLogger.enable_colorc             C   s
   dt _dS )z*
        Disable colorized output
        FN)rz   r{   )r@   r   r   r   Údisable_colorr  s    zAstropyLogger.disable_colorc             c   s‚   t jrt jnd}tj||d}|dk	r0| |¡ |dk	rF| t|ƒ¡ t t j¡}| 	|¡ |  
|¡ dV  | ¡  |  |¡ dS )aÐ  
        Context manager to temporarily log messages to a file.

        Parameters
        ----------
        filename : str
            The file to log messages to.
        filter_level : str
            If set, any log messages less important than ``filter_level`` will
            not be output to the file. Note that this is in addition to the
            top-level filtering for the logger, so if the logger has level
            'INFO', then setting ``filter_level`` to ``INFO`` or ``DEBUG``
            will have no effect, since these messages are already filtered
            out.
        filter_origin : str
            If set, only log messages with an origin starting with
            ``filter_origin`` will be output to the file.

        Notes
        -----

        By default, the logger already outputs log messages to a file set in
        the Astropy configuration file. Using this context manager does not
        stop log messages from being output to that file, nor does it stop log
        messages from being printed to standard output.

        Examples
        --------

        The context manager is used as::

            with logger.log_to_file('myfile.log'):
                # your code here
        N)Úencoding)r   r$   r&   ÚFileHandlerÚsetLevelÚ	addFilterÚFilterOriginÚ	Formatterr#   ÚsetFormatterÚ
addHandlerÚcloseÚremoveHandler)r@   ÚfilenameÚfilter_levelÚfilter_originr~   ÚfhÚfr   r   r   r    x  s    $


zAstropyLogger.log_to_filec             c   sN   t ƒ }|dk	r| |¡ |dk	r.| t|ƒ¡ |  |¡ |jV  |  |¡ dS )a  
        Context manager to temporarily log messages to a list.

        Parameters
        ----------
        filename : str
            The file to log messages to.
        filter_level : str
            If set, any log messages less important than ``filter_level`` will
            not be output to the file. Note that this is in addition to the
            top-level filtering for the logger, so if the logger has level
            'INFO', then setting ``filter_level`` to ``INFO`` or ``DEBUG``
            will have no effect, since these messages are already filtered
            out.
        filter_origin : str
            If set, only log messages with an origin starting with
            ``filter_origin`` will be output to the file.

        Notes
        -----

        Using this context manager does not stop log messages from being
        output to standard output.

        Examples
        --------

        The context manager is used as::

            with logger.log_to_list() as log_list:
                # your code here
        N)ÚListHandlerr€   r   r‚   r…   Úlog_listr‡   )r@   r‰   rŠ   Zlhr   r   r   Úlog_to_list©  s    "

zAstropyLogger.log_to_listc       	   
   C   st  |   ¡ r|  ¡  |  ¡ r |  ¡  x | jdd… D ]}|  |¡ q0W |  tj¡ t	ƒ }|  
|¡ tjrPtj}yt d}W n tk
r   d}Y nX yP|dks |r¶tj t d¡d¡}ntj |¡}tjrÎtjnd}tj||d}W n@ tk
r" } z t d|›d	t|ƒ› t¡ W dd}~X Y n.X t tj¡}| |¡ | tj ¡ |  
|¡ tj!r`|  "¡  tj#rp|  $¡  dS )
z3
        Reset logger to its initial state
        NTFr   r%   zastropy.log)r~   z	log file z" could not be opened for writing: )%r.   r/   r,   r-   Úhandlersr‡   r€   r   r   ÚStreamHandlerr…   r    r!   Z_ASTROPY_TEST_rm   rO   rP   Újoinr   Zget_config_dirÚ
expanduserr$   r&   r   ÚOSErrorr^   ÚwarnrN   ÚRuntimeWarningrƒ   r#   r„   r"   r   r`   r   rx   )	r@   ÚhandlerÚshr!   Ztesting_moder~   r‹   ÚeÚ	formatterr   r   r   r*   Ô  sF    



zAstropyLogger._set_defaults)NNN)NN)NN)r   r   r   r   r?   rK   r]   r.   r`   r/   rf   rk   r,   rx   r-   r|   r}   r   r    r   r*   r   r   r   r   r   –   s&   	
&)0*c               @   s   e Zd ZdZdd„ ZdS )r‘   zÄ
    A specialized StreamHandler that logs INFO and DEBUG messages to
    stdout, and all other messages to stderr.  Also provides coloring
    of the output, if enabled in the parent logger.
    c             C   sê   |j tjkrtj}ntj}|j tjk s,tjs>t	|j
d|d n~ddlm} |j tjk rj||j
dd|d nR|j tjk rŠ||j
dd|d n2|j tjk rª||j
dd|d n||j
dd|d |j› d	|jd
›d|_t	d|j |d dS )z*
        The formatter for stderr
        r   )ÚendÚfiler   )Úcolor_printÚmagentaÚgreenZbrownÚredz [Úsú]z: )rœ   N)Úlevelnor&   r   rS   ÚstdoutÚstderrr   rz   r{   ÚprintÚ	levelnameZutils.consoler   ÚWARNr   rE   r:   rY   )r@   ÚrecordÚstreamr   r   r   r   Úemit  s    zStreamHandler.emitN)r   r   r   r   r«   r   r   r   r   r‘     s   r‘   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r‚   zA filter for the record originc             C   s
   || _ d S )N)r:   )r@   r:   r   r   r   Ú__init__5  s    zFilterOrigin.__init__c             C   s   |j  | j ¡S )N)r:   r5   )r@   r©   r   r   r   Úfilter8  s    zFilterOrigin.filterN)r   r   r   r   r¬   r­   r   r   r   r   r‚   2  s   r‚   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )r   z;A handler that can be used to capture the records in a listNc             C   s   t j | ¡ g | _d S )N)r&   ÚHandlerr¬   rŽ   )r@   r‰   rŠ   r   r   r   r¬   ?  s    zListHandler.__init__c             C   s   | j  |¡ d S )N)rŽ   Úappend)r@   r©   r   r   r   r«   C  s    zListHandler.emit)NN)r   r   r   r   r¬   r«   r   r   r   r   r   <  s   
r   )&r   rb   rO   rS   r&   r^   Ú
contextlibr   r   r   r   r   rz   Úutilsr   Zutils.exceptionsr   r   Ú__all__Zlogging_levelsrB   rV   Úglobalsr
   r7   r   r   ZConfigNamespacer	   r+   r9   r'   r1   r   r‘   r‚   r®   r   r   r   r   r   Ú<module>	   s<   
	(   |"
