o
    x[h  ã                   @   sü   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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ZdZejd ZG dd	„ d	ejƒZejdfd
d„Zdd„ Zddd„Zddd„ZG dd„ dejƒZdd„ Zdd„ ZG dd„ dejƒZdd„ ZdS )é    N)Údefaultdict)Úsuppress)ÚDefaultDictz6%(asctime)s - %(filename)s[%(levelname)s]: %(message)sé#   é   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCustomLoggerTypezÿA hack to get mypy to stop complaining about custom logging methods.

    When using deprecated or trace logging, rather than:
        LOG = logging.getLogger(__name__)
    Instead do:
        LOG = cast(CustomLoggerType, logging.getLogger(__name__))
    c                 O   ó   d S ©N© ©ÚselfÚargsÚkwargsr
   r
   ú7/usr/lib/python3/dist-packages/cloudinit/log/loggers.pyÚtrace&   ó   zCustomLoggerType.tracec                 O   r   r	   r
   r   r
   r
   r   Ú
deprecated)   r   zCustomLoggerType.deprecatedN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r
   r
   r
   r   r      s    r   c                 C   sN   |pt  t¡}t  ¡ }t  tj¡}| |¡ | | ¡ | 	|¡ | | ¡ d S r	   )
ÚloggingÚ	FormatterÚDEFAULT_LOG_FORMATÚ	getLoggerÚStreamHandlerÚsysÚstderrÚsetFormatterÚsetLevelÚ
addHandler)ÚlevelÚ	formatterÚrootÚconsoler
   r
   r   Úsetup_basic_logging-   s   


r%   c              	   C   s^   | sd S | j D ] }t|tjƒr'ttƒ� | ¡  W d   ƒ n1 s"w   Y  qt| jƒ d S r	   )	ÚhandlersÚ
isinstancer   r   r   ÚIOErrorÚflushÚflush_loggersÚparent)r#   Úhr
   r
   r   r*   7   s   


ÿ€r*   Úreturnc                  C   sH   dd„ } t  td¡ t  td¡ tt jd| tƒƒ tt jd| tƒƒ dS )z:Add DEPRECATED and TRACE log levels to the logging module.c                    s   ‡ fdd„}|S )Nc                    s(   |   ˆ ¡r| jˆ ||fi |¤Ž d S d S r	   )ÚisEnabledForÚ_log)r   Úmessager   r   ©r!   r
   r   Úlog_at_levelE   s   
ÿz>define_extra_loggers.<locals>.new_logger.<locals>.log_at_levelr
   )r!   r2   r
   r1   r   Ú
new_loggerD   s   z(define_extra_loggers.<locals>.new_loggerÚ
DEPRECATEDÚTRACEr   r   N)r   ÚaddLevelNamer4   r5   ÚsetattrÚLogger)r3   r
   r
   r   Údefine_extra_loggersA   s
   r9   c           	   	   C   st  | si } t  ¡ }tƒ }| t j¡ g }|  d¡}|r't|tƒr'| t|ƒ¡ n4d| v r[| d D ]+}t|tƒr<| |¡ q/t|t	j
jƒrSdd„ |D ƒ}| d |¡¡ q/| t|ƒ¡ q/d}|D ]8}ttƒ�* |d7 }| d¡rutj |¡szt |¡}t j |¡ | |¡ 	 W d   ƒ  d S 1 s’w   Y  q_| |¡ |  d	d
¡}tj d| ¡ |r¸tj d¡ tƒ  d S d S )NÚlogcfgÚlog_cfgsc                 S   s   g | ]}t |ƒ‘qS r
   )Ústr)Ú.0Úcr
   r
   r   Ú
<listcomp>e   s    z!setup_logging.<locals>.<listcomp>Ú
r   é   ú/Ú	log_basicTz0WARN: no logging configured! (tried %s configs)
zSetting up basic logging...
)r   r   ÚLogExporterr   ÚWARNÚgetr'   r<   ÚappendÚcollectionsÚabcÚIterableÚjoinr   ÚFileNotFoundErrorÚ
startswithÚosÚpathÚisfileÚioÚStringIOÚconfigÚ
fileConfigr    r   r   Úwriter%   )	ÚcfgÚroot_loggerÚexporterr;   Úlog_cfgÚa_cfgÚcfg_strÚam_triedÚbasic_enabledr
   r
   r   Úsetup_loggingQ   sH   




$ò
ÿ
þr^   c                   @   sN   e Zd ZU eeƒZeeef ed< de	j
fdd„Zdd„ Zdd„ Zd	d
„ ZdS )rD   ÚholderÚrecordc                 C   s   | j |j  | ¡ ¡ d S r	   )r_   Ú	levelnamerG   Ú
getMessage©r   r`   r
   r
   r   Úemit•   s   zLogExporter.emitc                 C   s   t  | j¡S r	   )ÚcopyÚdeepcopyr_   ©r   r
   r
   r   Úexport_logs˜   s   zLogExporter.export_logsc                 C   s   t tƒ| _d S r	   )r   Úlistr_   rg   r
   r
   r   Ú
clean_logs›   s   zLogExporter.clean_logsc                 C   r   r	   r
   rg   r
   r
   r   r)   ž   r   zLogExporter.flushN)r   r   r   r   ri   r_   r   r<   Ú__annotations__r   Ú	LogRecordrd   rh   rj   r)   r
   r
   r
   r   rD   ’   s   
 rD   c                  C   sF   t  ¡ } t| jƒ}|D ]}| ¡  | ¡  |  |¡ q|  t j¡ dS )z0Remove all current handlers and unset log level.N)	r   r   ri   r&   r)   ÚcloseÚremoveHandlerr   ÚNOTSET)Úlogr&   r,   r
   r
   r   Úreset_logging¢   s   
rq   c                     sJ   t  tj¡‰ tˆ ddd„ ƒ ˆ  t  d¡¡ ‡ fdd„} tt jd| ƒ dS )z·In the event that internal logging exception occurs and logging is not
    possible for some reason, make a desperate final attempt to log to stderr
    which may ease debugging.
    ÚhandleErrorc                 S   r   r	   r
   )r`   r
   r
   r   Ú<lambda>³   s    z&setup_backup_logging.<locals>.<lambda>z@FALLBACK: %(asctime)s - %(filename)s[%(levelname)s]: %(message)sc                    s@   t tƒ� ˆ  |¡ ˆ  ¡  W d  ƒ dS 1 sw   Y  dS )z;A closure that emits logs on stderr when other methods failN)r   r(   Úhandler)   rc   ©Úfallback_handlerr
   r   rr   º   s   


"þz)setup_backup_logging.<locals>.handleErrorN)r   r   r   r   r7   r   r   ÚHandler)rr   r
   ru   r   Úsetup_backup_logging­   s   ÿÿrx   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚCloudInitLogRecordz’reporting the filename as __init__.py isn't very useful in logs

    if the filename is __init__.py, use the parent directory as the filename
    c                    s<   t ƒ j|i |¤Ž d| jkrtj tj | j¡¡| _d S d S )Nz__init__.py)ÚsuperÚ__init__ÚfilenamerN   rO   ÚbasenameÚdirnameÚpathnamer   ©Ú	__class__r
   r   r{   É   s   
ÿzCloudInitLogRecord.__init__)r   r   r   r   r{   Ú__classcell__r
   r
   r€   r   ry   Ã   s    ry   c                  C   sJ   t jtj_tƒ  tƒ  tƒ  tƒ } |  	tj
¡ t ¡  | ¡ t t¡ dS )z(Customize the root logger for cloud-initN)ÚtimeÚgmtimer   r   Ú	converterr9   rx   rq   rD   r   rE   r   r    ÚsetLogRecordFactoryry   )Úhandlerr
   r
   r   Úconfigure_root_loggerÏ   s   
rˆ   )r-   Nr	   ) Úcollections.abcrH   re   rQ   r   Úlogging.configÚlogging.handlersrN   r   rƒ   r   Ú
contextlibr   Útypingr   r   r4   ÚDEBUGr5   r8   r   r%   r*   r9   r^   r   rD   rq   rx   rl   ry   rˆ   r
   r
   r
   r   Ú<module>   s2   





A