o
    ϴf                     @   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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   )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   <module>   s     .