o
    wOi                  
   @   sz   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	 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)DictTextIOheaderfiledstfinishedinterval_secc                 C   s   t j|s| rd S t| t j|rt|d%}	 | }|r.||  |  n
| r3nt| qW d    d S 1 sDw   Y  d S )Nr)	ospathexistsis_settimesleepopenreadlinewrite)r   r   r	   r
   r   fpline r   Y/home/ubuntu/.local/lib/python3.10/site-packages/torchelastic/multiprocessing/tail_log.pytail_logfile   s    

"r   c                	   @   sX   e Zd ZdZ	ddedeeef dedefddZ	dd
dZ
dddZd	efddZdS )TailLoga8  
    Tails 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.

    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.

    皙?name	log_filesr	   r   c                 C   sn   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>d   s    z$TailLog.__init__.<locals>.<dictcomp>F)len_threadpoolr   	__class____qualname___name_dst
_log_fileskeys_finished_events_futs_interval_sec_stopped)selfr   r   r	   r   nr   r   r   __init__R   s    
zTailLog.__init__returnc                 C   sZ   | j s| S | j D ] \}}| j| j jtd| j | d|| j| j	| | j
d q
| S )N[z]:)r   r   r	   r
   r   )r&   r+   itemsr.   appendsubmitr   r)   r*   r-   r/   )r1   r#   r   r   r   r   startk   s   
zTailLog.startNc                 C   s   | j  D ]}|  qt| jD ]/\}}z|  W q ty@ } ztd| j	 | d|j
j d|  W Y d }~qd }~ww | jrK| jjdd d| _d S )Nzerror in log tailor for z. z: T)wait)r-   valuesset	enumerater.   result	Exceptionlogerrorr)   r'   r(   r&   shutdownr0   )r1   r
   r#   fer   r   r   stop|   s$   

zTailLog.stopc                 C   s   | j S )N)r0   )r1   r   r   r   stopped   s   zTailLog.stopped)r   )r4   r   )r4   N)__name__
__module__r(   __doc__strr   intr   floatr3   r9   rE   boolrF   r   r   r   r   r   -   s    )



r   )loggingr   r   concurrent.futures.threadr   	threadingr   typingr   r   	getLoggerrG   r@   rJ   rL   r   r   r   r   r   r   <module>   s&   

