o
    &æÑgX  ã                   @   s:   d dl mZ d dlZd dlZd dlZG dd„ dejƒZdS )é    Nc                       sf   e Zd ZdZdZdZeZdZdZdZ	dZ
dZdZd	Zd
ejfejjd	d	df‡ fdd„	Zdd„ Z‡  ZS )ÚWebsockifySysLogHandlerzb
    A handler class that sends proper Syslog-formatted messages,
    as defined by RFC 5424.
    z<{pri}>{ident}[{pid}]: z2<{pri}>1 {timestamp} {hostname} {ident} {pid} - - Fz%Y-%m-%dT%H:%M:%SZéÿ   é   Ú
NÚ	localhostc                    s,   || _ |rd| _| j| _tƒ  |||¡ dS )aÅ  
        Initialize a handler.

        If address is specified as a string, a UNIX socket is used. To log to a
        local syslogd, "WebsockifySysLogHandler(address="/dev/log")" can be
        used. If facility is not specified, LOG_USER is used. If socktype is
        specified as socket.SOCK_DGRAM or socket.SOCK_STREAM, that specific
        socket type will be used. For Unix sockets, you can also specify a
        socktype of None, in which case socket.SOCK_DGRAM will be used, falling
        back to socket.SOCK_STREAM. If ident is specified, this string will be
        used as the application name in all messages sent. Set legacy to True
        to use the old version of the protocol.
        TN)ÚidentÚ_legacyÚ_legacy_head_fmtÚ	_head_fmtÚsuperÚ__init__)ÚselfÚaddressÚfacilityÚsocktyper   Úlegacy©Ú	__class__© úW/root/parts/websockify/install/lib/python3.10/site-packages/websockify/sysloghandler.pyr      s
   z WebsockifySysLogHandler.__init__c           
   	   C   s¼  zÉ|   |¡ | jd¡}|sW dS |  | j|  |j¡¡}t | j	t 
¡ ¡}t ¡ d| j… }| jr9| jd| j… }nd}t ¡ }|||||dœ}| jj d	i |¤Ž dd¡}	z	|	| d¡7 }	W n tym   |	| d¡7 }	Y nw | jtjkr‹| jrƒdt|	ƒ  d¡|	 }	n|	| j d¡7 }	| jr°z
| j |	¡ W W dS  ty¯   |  | j¡ | j |	¡ Y W dS w | jtjkrÁ| j |	| j¡ W dS | j |	¡ W dS  t t!fyÓ   ‚    |  "|¡ Y dS )
z±
        Emit a record.

        The record is formatted, and then sent to the syslog server. If
        exception information is present, it is NOT sent to the server.
        ú NÚ )ÚpriÚ	timestampÚhostnamer   ÚpidÚasciiÚignorez	utf-8-sigz%d r   )#ÚformatÚreplaceÚ_tailÚencodePriorityr   ÚmapPriorityÚ	levelnameÚtimeÚstrftimeÚ_timestamp_fmtÚgmtimeÚsocketÚgethostnameÚ_max_hostnamer   Ú
_max_identÚosÚgetpidr
   ÚencodeÚUnicodeEncodeErrorr   Ú
SOCK_DGRAMÚ_send_lengthÚlenÚ
unixsocketÚsendÚOSErrorÚ_connect_unixsocketr   ÚsendtoÚsendallÚKeyboardInterruptÚ
SystemExitÚhandleError)
r   ÚrecordÚtextr   r   r   r   r   ÚheadÚmsgr   r   r   Úemit2   sV   
ÿûÿþzWebsockifySysLogHandler.emit)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   Ú_rfc5424_head_fmtr
   r   r&   r*   r+   r1   r    r   ÚhandlersÚSYSLOG_UDP_PORTÚSysLogHandlerÚLOG_USERr   r@   Ú__classcell__r   r   r   r   r      s"    þr   )Úlogging.handlersrF   r(   r,   r$   rH   r   r   r   r   r   Ú<module>   s   $ 