
    &`ib-                        d dl Zd dlZd dlmZ d dlmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ  G d	 d
ej                  Z G d dej                  Z G d d          Zdee         fdZdee         fdZdS )    N)Enum)OptionalUnion)CoreContextFilter)JSONFormatter)PlainRayHandler)TrainContextTrainRunContext)get_module_namec                   |    e Zd ZdZ G d dee          Z G d dee          Zdee	e
f         fdZd Zd	 Zd
 ZdS )TrainContextFilterzyAdd Ray Train metadata to the log records.

    This filter is applied to Ray Train controller and worker processes.
    c                   "    e Zd ZdZdZdZdZdZdS )TrainContextFilter.LogKeyrun_name	component
world_rank
local_rank	node_rankN)__name__
__module____qualname__RUN_NAME	COMPONENT
WORLD_RANK
LOCAL_RANK	NODE_RANK     z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/train/v2/_internal/logging/logging.pyLogKeyr      s'        	!
!
			r   r    c                       e Zd ZdZdZdS )!TrainContextFilter.TrainComponent
controllerworkerN)r   r   r   
CONTROLLERWORKERr   r   r   TrainComponentr"      s        !
r   r'   contextc                    t          |t                    | _        | j        r|j                                        j        | _        |                                | _        |	                                | _
        |                                | _        t          j        j        | _        d S |                                j        | _        t          j        j        | _        d S N)
isinstancer	   
_is_workertrain_run_contextget_run_configname	_run_nameget_world_rank_world_rankget_local_rank_local_rankget_node_rank
_node_rankr   r'   r&   
_componentr%   )selfr(   s     r   __init__zTrainContextFilter.__init__!   s     *7L A A? 	P")";"J"J"L"L"QDN$+$:$:$<$<D$+$:$:$<$<D#*#8#8#:#:DO#5#D#KDOOO")"8"8":":"?DN#5#D#ODOOOr   c                     t          |t          j        j        | j                   t          |t          j        j        | j                   dS NT)setattrr   r    r   r0   r   r7   r8   records     r   controller_filterz$TrainContextFilter.controller_filter-   s=    *1:DNKKK*1;T_MMMtr   c                 x   t          |t          j        j        | j                   t          |t          j        j        | j                   t          |t          j        j        | j                   t          |t          j        j	        | j
                   t          |t          j        j        | j                   dS r;   )r<   r   r    r   r0   r   r7   r   r2   r   r4   r   r6   r=   s     r   worker_filterz TrainContextFilter.worker_filter3   s    *1:DNKKK*1;T_MMM*1<d>NOOO*1<d>NOOO*1;T_MMMtr   c                 d    | j         r|                     |          S |                     |          S r*   )r,   rA   r?   r=   s     r   filterzTrainContextFilter.filter=   s4    ? 	2%%f---))&111r   N)r   r   r   __doc__strr   r    r'   r   r
   r	   r9   r?   rA   rC   r   r   r   r   r      s                  d          d   
Po|&C D 
P 
P 
P 
P    2 2 2 2 2r   r   c                   f     e Zd ZdZdef fdZd Zdej        ddfdZ	de
e         fd	Zd
 Z xZS )SessionFileHandlera  A handler that writes to a log file in the Ray session directory.

    The Ray session directory isn't available until Ray is initialized, so any logs
    emitted before Ray is initialized will be lost.
    This handler will not create the file handler until you emit a log record.

    Args:
        filename: The name of the log file. The file is created in the 'logs/train'
            directory of the Ray session directory.
    filenamec                     t                                                       || _        d | _        d | _        d | _        d S r*   )superr9   	_filename_handler
_formatter_path)r8   rH   	__class__s     r   r9   zSessionFileHandler.__init__S   s9    !


r   c                     | j         |                                  | j         | j                             |           d S d S r*   )rL   _try_create_handleremitr=   s     r   rR   zSessionFileHandler.emitZ   sH    = $$&&&=$Mv&&&&& %$r   fmtreturnNc                 V    | j         | j                             |           || _        d S r*   )rL   setFormatterrM   )r8   rS   s     r   rV   zSessionFileHandler.setFormatter`   s+    =$M&&s+++r   c                 F    | j         |                                  | j        S r*   )rL   rQ   rN   )r8   s    r   get_log_file_pathz$SessionFileHandler.get_log_file_pathe   s$    = $$&&&zr   c                 ^   | j         J t                                          }|d S t          j        |d           t          j                            || j                  | _        t          j
        | j                  | _         | j        !| j                             | j                   d S d S )NT)exist_ok)rL   LoggingManagerget_log_directoryosmakedirspathjoinrK   rN   loggingFileHandlerrM   rV   )r8   log_directorys     r   rQ   z&SessionFileHandler._try_create_handlerj   s    }$$$ '88:: F
MD1111 W\\-@@
+DJ77?&M&&t77777 '&r   )r   r   r   rD   rE   r9   rR   ra   	FormatterrV   r   rX   rQ   __classcell__)rO   s   @r   rG   rG   D   s        	 	      ' ' ' 1 d    
8C=    
8 8 8 8 8 8 8r   rG   c                       e Zd ZdZedeeef         defd            Z	ededefd            Z
ededefd            Zededdfd            Zededdfd	            Zedee         fd
            ZdS )r[   zN
    A utility class for managing the logging configuration of Ray Train.
    r(   rT   c                    t          j                                                    }ddddt          t                    iidt
          it          | dddt          t                    it          t                    dd| d	d
dgdt          t                    dd| d	d
dgdt          t                    dd| d	d
dgdt          t                    dd| d	d
dgddi dS )z1Return the base logging configuration dictionary.   Fray_jsonclass())rk   r(   )core_context_filtertrain_context_filterzray-train-sys-controller-z.logrl   rm   )rj   	formatterrH   filterszray-train-app-controller-zray-train-sys-worker-zray-train-app-worker-)consolefile_train_sys_controllerfile_train_app_controllerfile_train_sys_workerfile_train_app_worker)versiondisable_existing_loggers
formattersro   handlersloggers)	rayget_runtime_contextget_worker_idr   r   r   r   r   rG   )r(   file_identifiers     r   _get_base_logger_config_dictz+LoggingManager._get_base_logger_config_dict   sT    133AACC (-Wom&D&DE )-.?'@/Ag(V(V 
 $__%E%EF,-?@@!+ QO Q Q Q 57MN	. . --?@@!+ QO Q Q Q 57MN	. . --?@@!+ M M M M 57MN	* * --?@@!+ M M M M 57MN	* *) 6 K&
 &
 &	
r   c                 \    t                               |           }dg ddd|d         d<   |S )a  Return the controller logger configuration dictionary.

        On the controller process, only the `ray.train` logger is configured.
        This logger emits logs to the following three locations:
            - `file_train_sys_controller`: Ray Train system logs.
            - `file_train_app_controller`: Ray Train application logs.
            - `console`: Logs to the console.
        INFO)rq   rr   rp   Flevelrx   	propagatery   	ray.trainr[   r~   r(   config_dicts     r   "_get_controller_logger_config_dictz1LoggingManager._get_controller_logger_config_dict   sL     %AA'JJ  
 /
 /
I{+ r   c                 p    t                               |           }dg ddd|d         d<   dddgd	|d
<   |S )a  Return the worker loggers configuration dictionary.

        On the worker process, there are two loggers being configured:

        First, the `ray.train` logger is configured and emits logs to the
        following three locations:
            - `file_train_sys_worker`: Ray Train system logs.
            - `file_train_app_worker`: Ray Train application logs.
            - `console`: Logs to the console.
        Second, the root logger is configured and emits logs to the following
        two locations:
            - `console`: Logs to the console.
            - `file_train_app_worker`: Ray Train application logs.
        The root logger will not emit Ray Train system logs and thus not writing to
        `file_train_sys_worker` file handler.
        r   )rs   rt   rp   Fr   ry   r   rt   rp   )r   rx   rootr   r   s     r   _get_worker_logger_config_dictz-LoggingManager._get_worker_logger_config_dict   sb    & %AA'JJUUU/
 /
I{+ 0)<
 
F r   Nc                 x    t                               |           }t          j                            |           dS )zb
        Configure the logger on the controller process, which is the `ray.train` logger.
        N)r[   r   ra   config
dictConfigr(   r   s     r   configure_controller_loggerz*LoggingManager.configure_controller_logger   s3    
  BB7KK!!&)))))r   c                 x    t                               |           }t          j                            |           dS )z
        Configure the loggers on the worker process, which contains the
        `ray.train` logger and the root logger.
        N)r[   r   ra   r   r   r   s     r   configure_worker_loggerz&LoggingManager.configure_worker_logger   s3      >>wGG!!&)))))r   c                      t           j        j        j        } | dS |                                 }t
          j                            |dd          S )zReturn the directory where Ray Train writes log files.

        If not in a Ray session, return None.

        This path looks like: "/tmp/ray/session_xxx/logs/train/"
        Nlogstrain)rz   _privater$   _global_nodeget_session_dir_pathr]   r_   r`   )global_noderoot_dirs     r   r\   z LoggingManager.get_log_directory   sE     l)643355w||Hfg666r   )r   r   r   rD   staticmethodr   r
   r	   dictr~   r   r   r   r   r   rE   r\   r   r   r   r[   r[   |   s<         -
45-
	-
 -
 -
 \-
^ O     \,      \< *_ * * * * \* * *$ * * * \* 7x} 7 7 7 \7 7 7r   r[   rT   c                      t          j        d          } | j        D ]6}t          |t                    rd|j        v r|                                c S 7dS )zL
    Return the path to the file train application controller log file.
    r   zray-train-app-controllerNra   	getLoggerrx   r+   rG   rK   rX   loggerhandlers     r   )get_train_application_controller_log_pathr     sd     {++F? / /w 233	/*g.???,,.....4r   c                      t          j        d          } | j        D ]6}t          |t                    rd|j        v r|                                c S 7dS )zH
    Return the path to the file train application worker log file.
    r   zray-train-app-workerNr   r   s     r   %get_train_application_worker_log_pathr     sd     {++F? / /w 233	/&'*;;;,,.....4r   )logging.configra   r]   enumr   typingr   r   rz   ray._common.filtersr   ray._common.formattersr   ray._private.logr   (ray.train.v2._internal.execution.contextr	   r
   ray.train.v2._internal.utilr   Filterr   HandlerrG   r[   rE   r   r   r   r   r   <module>r      s       				       " " " " " " " " 



 1 1 1 1 1 1 0 0 0 0 0 0 , , , , , , R R R R R R R R 7 7 7 7 7 732 32 32 32 32 32 32 32l58 58 58 58 58 58 58 58pL7 L7 L7 L7 L7 L7 L7 L7^8C=     x}      r   