U
    ŒÌJh™  ã                   @   sø   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mZm	Z	m
Z
 d dlmZmZmZmZ d dlmZ G dd„ dejƒZG dd	„ d	ejƒZG d
d„ dejƒZedœdd„Zedœdd„Ze	e dœdd„Zdd„ Ze
eef edœdd„Zdd„ ZdS )é    N)ÚOrderedDict)ÚAnyÚDictÚListÚUnion)ÚdefaultsÚsecret_managerÚsystemÚutil)ÚUAConfigc                   @   s    e Zd ZdZejdœdd„ZdS )ÚRegexRedactionFilterú,A logging filter to redact confidential info©Úrecordc                 C   s   t  t|jƒ¡|_dS ©NT)r
   Zredact_sensitive_logsÚstrÚmsg©Úselfr   © r   ú./usr/lib/python3/dist-packages/uaclient/log.pyÚfilter   s    zRegexRedactionFilter.filterN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚloggingÚ	LogRecordr   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZejdœdd„ZdS )ÚKnownSecretRedactionFilterr   r   c                 C   s   t j t|jƒ¡|_dS r   )r   ZsecretsZredact_secretsr   r   r   r   r   r   r      s    z!KnownSecretRedactionFilter.filterNr   r   r   r   r   r      s   r   c                   @   s.   e Zd ZdZdZdZdZeje	dœdd„Z
dS )	ÚJsonArrayFormatterzYJson Array Formatter for our logging mechanism
    Custom made for Pro logging needs
    z%Y-%m-%dT%H:%M:%Sz%s.%03d)ÚasctimeZ	levelnameÚnameZfuncNameÚlinenoÚmessage)r   Úreturnc                 C   sÄ   |  ¡ |_|  |¡|_i }|jr0|  |j¡|d< | d¡sJ|jrJ|j|d< |jr`|  	|j¡|d< |j
 d¡}|r„t|tƒr„| |¡ tƒ }| jD ]}|j
 |¡}|||< q||d< t t| ¡ ƒ¡S )NÚexc_infoÚ
stack_infoÚextra)Z
getMessager$   Z
formatTimer!   r&   ZformatExceptionÚgetZexc_textr'   ZformatStackÚ__dict__Ú
isinstanceÚdictÚupdater   Úrequired_fieldsÚjsonÚdumpsÚlistÚvalues)r   r   Zextra_message_dictr(   Zlocal_log_recordZfieldÚvaluer   r   r   Úformat,   s,    
ÿ
ÿ


zJsonArrayFormatter.formatN)r   r   r   r   Zdefault_time_formatZdefault_msec_formatr.   r   r   r   r4   r   r   r   r   r       s
   	r    )r%   c                   C   s   t  ¡ rtƒ jS tƒ S dS )z\
    Gets the correct log_file path,
    adjusting for whether the user is root or not.
    N)r
   Úwe_are_currently_rootr   Úlog_fileÚget_user_log_filer   r   r   r   Úget_user_or_root_log_file_pathJ   s    r8   c                   C   s   t j t ¡ d¡S )z/Gets the correct user log_file storage locationúubuntu-pro.log)ÚosÚpathÚjoinr	   Zget_user_cache_dirr   r   r   r   r7   U   s    r7   c                  C   sH   t  d¡} g }| D ]0}t j d|dtjd¡}t j |¡r| |¡ q|S )z|Gets all the log files for the users in the system

    Returns a list of all user log files in their home directories.
    z/homez.cacher9   )r:   Úlistdirr;   r<   r   ZUSER_CACHE_SUBDIRÚisfileÚappend)Zuser_directoriesZ	log_filesZuser_directoryZ	user_pathr   r   r   Úget_all_user_log_filesZ   s    
ûr@   c                  C   s\   t  d¡} |  t j¡ t  ¡ }| tƒ ¡ | t j¡ | tƒ ¡ | t	ƒ ¡ |  
|¡ d S )NÚ	ubuntupro)r   Ú	getLoggerÚsetLevelÚINFOZStreamHandlerÚsetFormatterr    Ú	addFilterr   r   Ú
addHandler)ÚloggerZconsole_handlerr   r   r   Úsetup_journald_loggingn   s    
rI   )Ú	log_levelr6   c                 C   s®   t | tƒr|  ¡ } t ¡ s tƒ }t d¡}| | ¡ g |_	t
 |¡}| ¡ sh|jjddd |jdd t |¡}| tƒ ¡ | | ¡ | tƒ ¡ | tƒ ¡ | |¡ dS )zoSetup logging to log_file

    If run as non-root then log_file is replaced with a user-specific log file.
    rA   T)ÚparentsÚexist_oki   )ÚmodeN)r+   r   Úupperr
   r5   r7   r   rB   rC   ZhandlersÚpathlibÚPathÚexistsÚparentÚmkdirZtouchZFileHandlerrE   r    rF   r   r   rG   )rJ   r6   rH   Zlog_file_pathZfile_handlerr   r   r   Úsetup_cli_loggingy   s"    





rT   c                  K   s   d| iS )z“
    A helper for passing extra fields to log statements.
    Usage:
    LOG.info("message string", extra=log.extra(field=something_relevant))
    r(   r   )Úkwargsr   r   r   r(   š   s    r(   )r/   r   r:   rO   Úcollectionsr   Útypingr   r   r   r   Zuaclientr   r   r	   r
   Zuaclient.configr   ZFilterr   r   Z	Formatterr    r   r8   r7   r@   rI   ÚintrT   r(   r   r   r   r   Ú<module>   s    .!