o
    ÙKÕi™  ã                   @   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defdd„Zdefdd„Zde	e fdd„Zdd„ Zde
eef defdd„Zdd„ ZdS )é    N)ÚOrderedDict)ÚAnyÚDictÚListÚUnion)ÚdefaultsÚsecret_managerÚsystemÚutil)ÚUAConfigc                   @   ó    e Zd ZdZdejfdd„ZdS )ÚRegexRedactionFilterú,A logging filter to redact confidential infoÚrecordc                 C   s   t  t|jƒ¡|_dS ©NT)r
   Ú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      ó    r   c                   @   r   )ÚKnownSecretRedactionFilterr   r   c                 C   s   t j t|jƒ¡|_dS r   )r   ÚsecretsÚredact_secretsr   r   r   r   r   r   r      s   z!KnownSecretRedactionFilter.filterNr   r   r   r   r   r!      r    r!   c                   @   s0   e Zd ZdZdZdZdZdejde	f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)ÚasctimeÚ	levelnameÚnameÚfuncNameÚlinenoÚmessager   Úreturnc                 C   sÄ   |  ¡ |_|  |¡|_i }|jr|  |j¡|d< | d¡s%|jr%|j|d< |jr0|  	|j¡|d< |j
 d¡}|rBt|tƒrB| |¡ tƒ }| jD ]}|j
 |¡}|||< qH||d< t t| ¡ ƒ¡S )NÚexc_infoÚ
stack_infoÚextra)Ú
getMessager*   Ú
formatTimer%   r,   ÚformatExceptionÚgetÚexc_textr-   ÚformatStackÚ__dict__Ú
isinstanceÚdictÚupdater   Úrequired_fieldsÚjsonÚdumpsÚlistÚvalues)r   r   Úextra_message_dictr.   Úlocal_log_recordÚfieldÚvaluer   r   r   Úformat,   s,   
ÿ
ÿ


zJsonArrayFormatter.formatN)r   r   r   r   Údefault_time_formatÚdefault_msec_formatr9   r   r   r   rB   r   r   r   r   r$      s    	r$   r+   c                   C   s   t  ¡ rtƒ jS tƒ S )z\
    Gets the correct log_file path,
    adjusting for whether the user is root or not.
    )r
   Úwe_are_currently_rootr   Úlog_fileÚget_user_log_filer   r   r   r   Úget_user_or_root_log_file_pathJ   s   rH   c                   C   s   t j t ¡ d¡S )z/Gets the correct user log_file storage locationúubuntu-pro.log)ÚosÚpathÚjoinr	   Úget_user_cache_dirr   r   r   r   rG   U   s   rG   c                  C   sH   t  d¡} g }| D ]}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.cacherI   )rJ   ÚlistdirrK   rL   r   ÚUSER_CACHE_SUBDIRÚisfileÚappend)Úuser_directoriesÚ	log_filesÚuser_directoryÚ	user_pathr   r   r   Úget_all_user_log_filesZ   s   
û
€rV   c                  C   s\   t  d¡} |  t j¡ t  ¡ }| tƒ ¡ | t j¡ | tƒ ¡ | t	ƒ ¡ |  
|¡ d S )NÚ	ubuntupro)r   Ú	getLoggerÚsetLevelÚINFOÚStreamHandlerÚsetFormatterr$   Ú	addFilterr   r!   Ú
addHandler)ÚloggerÚconsole_handlerr   r   r   Úsetup_journald_loggingn   s   
ra   Ú	log_levelrF   c                 C   s®   t | tƒr	|  ¡ } t ¡ stƒ }t d¡}| | ¡ g |_	t
 |¡}| ¡ s4|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.
    rW   T)ÚparentsÚexist_oki   )ÚmodeN)r6   r   Úupperr
   rE   rG   r   rX   rY   ÚhandlersÚpathlibÚPathÚexistsÚparentÚmkdirÚtouchÚFileHandlerr\   r$   r]   r   r!   r^   )rb   rF   r_   Úlog_file_pathÚfile_handlerr   r   r   Úsetup_cli_loggingy   s"   





rq   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   rJ   rh   Úcollectionsr   Útypingr   r   r   r   Úuaclientr   r   r	   r
   Úuaclient.configr   ÚFilterr   r!   Ú	Formatterr$   r   rH   rG   rV   ra   Úintrq   r.   r   r   r   r   Ú<module>   s"    .!