
    5i                     x    d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
mZ d dlmZ  G d de      Zd Zy)    )print_functionunicode_literalsdivisionabsolute_importN)SysLogHandler)DXErrorc                   <    e Zd ZdZ	 	 	 d	dZd Zd Zd Zd Zd Z	y)
DXLogHandlera  
    Logging handler for DNAnexus application level logging.
    Code adapted from logging.handlers.SysLogHandler.

    This handler is automatically enabled in the job template when running Python code in the execution environment.
    It forwards all log messages sent through the :mod:`logging` module to the DNAnexus log service,
    so that they can be examined through the log query API.
    To enable the handler in a Python subprocess in the execution environment, use:

        import logging
        logging.basicConfig(level=logging.DEBUG)
        from dxpy.dxlog import DXLogHandler
        logging.getLogger().addHandler(DXLogHandler())

    c                 h   t         j                  j                  |        || _        t	        j                  t        j
                  t        j                        | _        || _        t	        j                  t        j
                  t        j                        | _	        t        j                  j                  |      st        d|z        t        j                  j                  |      st        d|z        | j                  j                  |       | j                  j                  |       || _        y )NzCThe path %s does not exist, but is required for application logging)loggingHandler__init__priority_log_addresssocketAF_UNIX
SOCK_DGRAMpriority_log_socketbulk_log_addressbulk_log_socketospathexistsr   connectsource)selfr   r   r   s       l/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/dxlog.pyr   zDXLogHandler.__init__)   s     	  &$8!#)==ARAR#S  0%}}V^^V=N=NOww~~23_cwxyyww~~./_cstuu  (()=>$$%56    c                     | j                   j                          | j                  j                          t        j                  j                  |        y )N)r   closer   r   r   )r   s    r   r   zDXLogHandler.close>   s8      &&(""$d#r   c                 h    | j                   | j                  j                  |j                  d         S )Nwarning)priority_namespriority_mapget	levelname)r   records     r   encodePriorityzDXLogHandler.encodePriorityC   s,    ""4#4#4#8#89I9I9#UVVr   c                 H   |j                  d      }t        t        j                  |            dk  r|S |d d }t        t        j                  t	        |                  dkD  r0|d d }t        t        j                  t	        |                  dkD  r0t	        |      }|dz   S )Nutf-8iO  i@  z... [truncated])encodelenjsondumps_bytes2utf8)r   message	msg_bytess      r   truncate_messagezDXLogHandler.truncate_messageG   s    NN7+	tzz'"#t+Net$	$**[345<!#2I $**[345< i(***r   c                 $    |j                  d      S )NzCPU: )
startswith)r   r0   s     r   is_resource_logzDXLogHandler.is_resource_logT   s    !!'**r   c           	         | j                  |      }|j                         }| j                  |      }t        j                  | j
                  t        t        t        j                         dz              ||d      j                  dd      }t        |j                        }|t        j                  k\  s$|t        j                  k(  r*| j                  |      r| j                  }| j                   }n| j"                  }| j$                  }	 |j'                  |       y # t(        j*                  $ r% |j-                  |       |j'                  |       Y y t.        t0        f$ r   | j3                  |       Y y xY w)Ni  )r   	timestamplevelmsgr)   ignore)r'   
getMessager2   r-   r.   r   introundtimer+   levelnor   CRITICALINFOr5   r   r   r   r   sendr   errorr   KeyboardInterrupt
SystemExithandleError)r   r&   r8   r0   datar?   
cur_socketcur_socket_addresss           r   emitzDXLogHandler.emitW   s9   ##F+##%
 ''0zzT[[s5W[I[C\?]$)'; <<BF7H<U 	 fnn%g&&&7gll+BtG[G[\cGd11J!%!:!:--J!%!6!6	%OOD!|| 	"12OOD!!:. 		%V$s   D 5E8!E8N)z/opt/dnanexus/log/priorityz/opt/dnanexus/log/bulkDX_APP)
__name__
__module____qualname____doc__r   r   r'   r2   r5   rJ    r   r   r
   r
      s2     -I": *$
W++%r   r
   c                 &    | j                  dd      S )z
    Convert bytes to a UTF-8 string and ignore UnicodeDecodeError for chars that could have been messed up by truncating.
    r)   r:   )decode)bytess    r   r/   r/   v   s     <<**r   )
__future__r   r   r   r   r   r-   r>   r   r   logging.handlersr   dxpy.exceptionsr   r
   r/   rP   r   r   <module>rW      s1   " S R & & & * #[%= [%z+r   