o
    ZhS                  
   @   s   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m	Z	m
Z
 e
r*d dlmZ ddgZe eZded	ed
e	dedef
ddZG dd dZdS )    N)ThreadPoolExecutorEvent)OptionalTextIOTYPE_CHECKING)Futuretail_logfileTailLogheaderfiledstfinishedinterval_secc                 C   s   t j|s| rd S t| t j|rt|dd%}	 | }|r/||  |  n
| r4nt| qW d    d S 1 sEw   Y  d S )Nreplace)errors)	ospathexistsis_settimesleepopenreadlinewrite)r   r   r   r   r   fpline r   a/var/www/auris/lib/python3.10/site-packages/torch/distributed/elastic/multiprocessing/tail_log.pyr	      s    

"c                   @   sj   e Zd ZdZ		ddedeeef dedeeeef  de	f
d	d
Z
dddZdddZdefddZdS )r
   a  
    Tail the given log files.

    The log files do not have to exist when the ``start()`` method is called. The tail-er will gracefully wait until
    the log files are created by the producer and will tail the contents of the
    log files until the ``stop()`` method is called.

    .. warning:: ``TailLog`` will wait indefinitely for the log file to be created!

    Each log file's line will be suffixed with a header of the form: ``[{name}{idx}]:``,
    where the ``name`` is user-provided and ``idx`` is the index of the log file
    in the ``log_files`` mapping. ``log_line_prefixes`` can be used to override the
    header for each log file.

    Usage:

    ::

     log_files = {0: "/tmp/0_stdout.log", 1: "/tmp/1_stdout.log"}
     tailer = TailLog("trainer", log_files, sys.stdout).start()
     # actually run the trainers to produce 0_stdout.log and 1_stdout.log
     run_trainers()
     tailer.stop()

     # once run_trainers() start writing the ##_stdout.log files
     # the tailer will print to sys.stdout:
     # >>> [trainer0]:log_line1
     # >>> [trainer1]:log_line1
     # >>> [trainer0]:log_line2
     # >>> [trainer0]:log_line3
     # >>> [trainer1]:log_line2

    .. note:: Due to buffering log lines between files may not necessarily
              be printed out in order. You should configure your application's
              logger to suffix each log line with a proper timestamp.

    N皙?name	log_filesr   log_line_prefixesr   c                 C   st   t |}d | _|dkrt|| jj d| d| _|| _|| _|| _|| _dd |	 D | _
g | _|| _d| _d S )Nr   _)max_workersthread_name_prefixc                 S   s   i | ]}|t  qS r   r   ).0
local_rankr   r   r   
<dictcomp>m   s    z$TailLog.__init__.<locals>.<dictcomp>F)len_threadpoolr   	__class____qualname___name_dst
_log_files_log_line_prefixeskeys_finished_events_futs_interval_sec_stopped)selfr    r!   r   r"   r   nr   r   r   __init__Y   s"   
zTailLog.__init__returnc                 C   sx   | j s| S | j D ]/\}}d| j | d}| jr$|| jv r$| j| }| j| j jt||| j	| j
| | jd q
| S )N[z]:)r   r   r   r   r   )r*   r/   itemsr-   r0   r3   appendsubmitr	   r.   r2   r4   )r6   r'   r   r   r   r   r   startt   s"   

zTailLog.startc                 C   s   | j  D ]}|  qt| jD ](\}}z|  W q ty9 } ztd| j	||j
j| W Y d }~qd }~ww | jrD| jjdd d| _d S )Nz$error in log tailor for %s%s. %s: %sT)wait)r2   valuesset	enumerater3   result	Exceptionloggererrorr-   r+   r,   r*   shutdownr5   )r6   r   r'   fer   r   r   stop   s$   
	
zTailLog.stopc                 C   s   | j S )N)r5   )r6   r   r   r   stopped   s   zTailLog.stopped)Nr   )r9   r
   )r9   N)__name__
__module__r,   __doc__strdictintr   r   floatr8   r>   rJ   boolrK   r   r   r   r   r
   2   s$    +



)loggingr   r   Zconcurrent.futures.threadr   	threadingr   typingr   r   r   concurrent.futures._baser   __all__	getLoggerrL   rE   rO   rR   r	   r
   r   r   r   r   <module>   s,   	

