o
    &Õj×  ã                   @   s®   d dl mZ d dlmZ d dlmZ d dlZd dlZd dlZd dl	Z	d dl
Z
e
 d¡Zeeeƒr4eeƒZG dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZG dd„ de	jƒZdS )é    )Úbfile)Úmasked_crc32c)Ú
record_pb2NZVDL_QUEUE_TIMEOUTc                   @   sD   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	dd„ ƒZ
dS )ÚRecordWriterz$Package data with crc32 or not.
    c                 C   s
   || _ d S ©N)Ú_writer)ÚselfÚwriter© r
   ú^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/writer/record_writer.pyÚ__init__"   s   
zRecordWriter.__init__c                 C   s$   t  dt|ƒ¡}| j || ¡ dS )zrPackage and write data to disk.

        Args:
            data (string or bytes): Data to write to disk.
        ú<QN)ÚstructÚpackÚlenr   Úwrite)r   ÚdataÚheaderr
   r
   r   r   %   s   zRecordWriter.writec                 C   sL   t  dt|ƒ¡}t  dt|ƒ¡}t  dt|ƒ¡}| j || | | ¡ dS )a5  Package data with crc32 and write to disk.

        Format of a single record: (little-endian)
        uint64    length
        uint32    masked crc of length
        byte      data[length]
        uint32    masked crc of data

        Args:
            data (string or bytes): Data to write to disk.
        r   z<IN)r   r   r   r   r   r   )r   r   r   Z
header_crcZ
footer_crcr
   r
   r   Ú	write_crc.   s   zRecordWriter.write_crcc                 C   ó   | j  ¡  d S r   )r   Úflush©r   r
   r
   r   r   ?   ó   zRecordWriter.flushc                 C   r   r   )r   Úcloser   r
   r
   r   r   B   r   zRecordWriter.closec                 C   s   | j jS r   )r   Úclosedr   r
   r
   r   r   E   s   zRecordWriter.closedN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   Úpropertyr   r
   r
   r
   r   r      s    	r   c                   @   sJ   e Zd ZdZ				ddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dS )ÚRecordFileWriterzä Writers `Record` protocol buffers to an records file one by one.

    The `RecordFileWriter` class create an records file in the specified
    directory and asynchronously writes `Record` protocol buffers to this
    file.
    é
   éx   Ú c                 C   sÌ   || _ t |¡st |¡ |rAd|v r)t ||¡| _t | j¡r(td | j¡ƒ n%dt ¡ |f }t ||¡| _td ||¡ƒ nt |dt ¡ |f ¡| _t 	| jd¡| _
tt| j
ƒ||ƒ| _|  ¡  d S )NZ
vdlrecordsz-`{}` is exists, VisualDL will add logs to it.zvdlrecords.%010d.log%szfSince the log filename should contain `vdlrecords`, the filename is invalid and `{}` will replace `{}`Úwb)Ú_logdirr   ÚexistsÚmakedirsÚjoinÚ
_file_nameÚprintÚformatÚtimeZBFileZ_general_file_writerÚ_AsyncWriterr   Ú_async_writerr   )r   ZlogdirÚmax_queue_sizeÚ
flush_secsÚfilename_suffixÚfilenameÚfnr
   r
   r   r   R   s<   

ÿÿ€ÿþþ
þzRecordFileWriter.__init__c                 C   ó   | j S r   )r%   r   r
   r
   r   Ú
get_logdirx   ó   zRecordFileWriter.get_logdirc                 C   r4   r   )r)   r   r
   r
   r   Úget_filename{   r6   zRecordFileWriter.get_filenamec                 C   s4   t |tjƒstdt|ƒ ƒ‚| ¡ }| j |¡ d S )Nz0Expected an record_pb2.Record proto,  but got %s)Ú
isinstancer   ZRecordÚ	TypeErrorÚtypeZSerializeToStringr.   r   )r   ÚrecordÚar
   r
   r   Ú
add_record~   s   ÿzRecordFileWriter.add_recordc                 C   r   r   )r.   r   r   r
   r
   r   r   …   r   zRecordFileWriter.flushc                 C   r   r   )r.   r   r   r
   r
   r   r   ˆ   r   zRecordFileWriter.closeN)r!   r"   r#   r#   )
r   r   r   r   r   r5   r7   r=   r   r   r
   r
   r
   r   r    J   s    	
û&r    c                   @   s.   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ ZdS )r-   r"   é   c                 C   sB   || _ d| _t |¡| _t| j| j |ƒ| _t ¡ | _	| j 
¡  dS )zƒStart a sub-thread to handle data writing.

        Args:
            record_writer (visualdl.record_writer.RecordWriter):
        FN)Ú_record_writerÚ_closedÚqueueÚQueueÚ_bytes_queueÚ_AsyncWriterThreadÚ_workerÚ	threadingÚLockÚ_lockÚstart)r   Úrecord_writerr0   r/   r
   r
   r   r      s   ÿ
z_AsyncWriter.__init__c              	   C   s|   | j 1 | jrtdƒ‚z
| jj|td W n tjy#   tdƒ Y n	w W d   ƒ d S W d   ƒ d S 1 s7w   Y  d S )NúWriter is closed.)Útimeoutz4This data was not written to the log due to timeout.)	rH   r@   ÚIOErrorrC   ÚputÚQUEUE_TIMEOUTrA   ÚFullr*   )r   Ú
bytestringr
   r
   r   r   œ   s   ÿÿü"úz_AsyncWriter.writec                 C   sN   | j  | jrtdƒ‚| j ¡  | j ¡  W d   ƒ d S 1 s w   Y  d S )NrK   )rH   r@   rM   rC   r(   r?   r   r   r
   r
   r   r   ¥   s   
"ûz_AsyncWriter.flushc                 C   sp   | j s6| j& | j s$d| _ | j ¡  | j ¡  | j ¡  W d   ƒ d S W d   ƒ d S 1 s/w   Y  d S d S )NT)r@   rH   rE   Ústopr?   r   r   r   r
   r
   r   r   ­   s   

û"ÿÿz_AsyncWriter.closeN)r"   r>   )r   r   r   r   r   r   r   r
   r
   r
   r   r-   Œ   s
    
	r-   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )rD   c                 C   s<   t j | ¡ d| _|| _|| _|| _d| _d| _t	ƒ | _
d S )NTr   F)rF   ÚThreadr   ÚdaemonÚ_queuer?   Ú_flush_secsÚ_next_flush_timeÚ_has_pending_dataÚobjectÚ_shutdown_signal)r   Z
data_queuerJ   r0   r
   r
   r   r   ¸   s   z_AsyncWriterThread.__init__c                 C   s   | j  | j¡ |  ¡  d S r   )rU   rN   rZ   r(   r   r
   r
   r   rR   Â   s   z_AsyncWriterThread.stopc                 C   s>  d}	 t   ¡ }| j| }d }zlz/|dkr| j d|¡}n| j d¡}|| jkr4W W |r2| j ¡  d S d S | j |¡ d| _W n3 t	j
yH   Y n* tyq } z|setd |¡ƒ td t  dt  ¡ ¡¡ƒ d}W Y d }~nd }~ww W |rz| j ¡  n	|rƒ| j ¡  w w t   ¡ }|| jkrž| jr˜| j ¡  d| _|| j | _q)NFTr   z;Warning: Writing data Error, Due to unresolved Exception {}z4Warning: Writing data to FileSystem failed since {}.z%a, %d %b %Y %H:%M:%S +0000)r,   rW   rU   ÚgetrZ   Ú	task_doner?   r   rX   rA   ÚEmptyÚ	Exceptionr*   r+   ÚstrftimeÚgmtimer   rV   )r   Zhas_unresolved_bugÚnowZqueue_wait_durationr   Úer
   r
   r   ÚrunÆ   sT   

ÿó
ÿ€ù€	
€ÿÿ

Þz_AsyncWriterThread.runN)r   r   r   r   rR   rc   r
   r
   r
   r   rD   ·   s    
rD   )Zvisualdl.ior   Zvisualdl.utils.crc32r   Zvisualdl.protor   r   r,   rA   rF   ÚosÚgetenvrO   r8   ÚstrÚintrY   r   r    r-   rS   rD   r
   r
   r
   r   Ú<module>   s   

,B+