U
    {Ph[                     @  s   d dl m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
deje	d\ZZerjd dlmZ ne
deje	d	\ZZd
gZG dd
 d
ZdS )    )annotationsN)TYPE_CHECKING)
IgniteInfo)min_versionoptional_importzignite.engineEvents)Enginer   LogfileHandlerc                   @  sb   e Zd ZdZdejddfddddddd	d
ZdddddZdddddZdddddZ	dS )r	   a  
    Adds a `logging.FileHandler` to the attached engine's logger when the start event occurs and removes it again when
    then completed event occurs.

    A handler is needed to remove `FileHandler` object when the complete event occurs so that further runs of different
    engines write only to the log files they should, rather than previous files. Multiple handlers can write to the same
    file which allows output from train and evaluation engine objects to be condensed in one file. If the given output
    directory doesn't exist it will by default be created when the  start event occurs. This can be used in conjunction
    with `CheckpointSaver` to save a log file to the same destination as the saved checkpoints. Since the handler is
    added possibly after other logging events during initialisation, not all logging data will be retained.

    Args:
        output_dir: directory to save the log file to
        filename: name of the file to save log to
        loglevel: log level for the handler
        formatter: format string for the `logging.Formatter` set for the handler
        create_dir: if True, create `output_dir` if it doesn't exist
    zlog.txtz/%(asctime)s %(name)s %(levelname)s: %(message)sTstrintbool)
output_dirfilenameloglevel	formatter
create_dirc                 C  s.   || _ || _|| _|| _|| _d | _d | _d S N)r   r   r   r   r   loggerhandler)selfr   r   r   r   r    r   S/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/handlers/logfile_handler.py__init__2   s    zLogfileHandler.__init__r   None)enginereturnc                 C  s,   |j | _ |tj| j |tj| j d S r   )r   add_event_handlerr   STARTED_start	COMPLETED
_completedr   r   r   r   r   attachB   s    zLogfileHandler.attachc                 C  s   | j r$tj| js$tj| jdd ttj| j| j	| _
| j
| j | j
t| j | jd k	rz| j| j
 ntdd S )NT)exist_okz-`self.logger` must not be None in start event)r   ospathexistsr   makedirsloggingFileHandlerjoinr   r   setLevelr   setFormatter	Formatterr   r   
addHandlerAttributeErrorr!   r   r   r   r   G   s    
zLogfileHandler._startc                 C  s@   | j d k	r.| jd k	r.| j | j | j  ntdd | _d S )NzC`self.logger` and `self.handler` must not be None in complete event)r   r   removeHandlercloser/   r!   r   r   r   r    T   s
    zLogfileHandler._completedN)
__name__
__module____qualname____doc__r(   INFOr   r"   r   r    r   r   r   r   r	      s   )
__future__r   r(   r$   typingr   monai.configr   monai.utilsr   r   OPT_IMPORT_VERSIONr   _ignite.enginer   __all__r	   r   r   r   r   <module>   s   