Use standard line endings in sysloghandler.py

This file was accidentally using DOS file endings, unlike every other
file in this project.
This commit is contained in:
Pierre Ossman 2024-08-29 16:31:43 +02:00
parent 3f205f7cc6
commit 245fd08e52
1 changed files with 118 additions and 118 deletions

View File

@ -1,118 +1,118 @@
import logging.handlers as handlers, socket, os, time import logging.handlers as handlers, socket, os, time
class WebsockifySysLogHandler(handlers.SysLogHandler): class WebsockifySysLogHandler(handlers.SysLogHandler):
""" """
A handler class that sends proper Syslog-formatted messages, A handler class that sends proper Syslog-formatted messages,
as defined by RFC 5424. as defined by RFC 5424.
""" """
_legacy_head_fmt = '<{pri}>{ident}[{pid}]: ' _legacy_head_fmt = '<{pri}>{ident}[{pid}]: '
_rfc5424_head_fmt = '<{pri}>1 {timestamp} {hostname} {ident} {pid} - - ' _rfc5424_head_fmt = '<{pri}>1 {timestamp} {hostname} {ident} {pid} - - '
_head_fmt = _rfc5424_head_fmt _head_fmt = _rfc5424_head_fmt
_legacy = False _legacy = False
_timestamp_fmt = '%Y-%m-%dT%H:%M:%SZ' _timestamp_fmt = '%Y-%m-%dT%H:%M:%SZ'
_max_hostname = 255 _max_hostname = 255
_max_ident = 24 #safer for old daemons _max_ident = 24 #safer for old daemons
_send_length = False _send_length = False
_tail = '\n' _tail = '\n'
ident = None ident = None
def __init__(self, address=('localhost', handlers.SYSLOG_UDP_PORT), def __init__(self, address=('localhost', handlers.SYSLOG_UDP_PORT),
facility=handlers.SysLogHandler.LOG_USER, facility=handlers.SysLogHandler.LOG_USER,
socktype=None, ident=None, legacy=False): socktype=None, ident=None, legacy=False):
""" """
Initialize a handler. Initialize a handler.
If address is specified as a string, a UNIX socket is used. To log to a If address is specified as a string, a UNIX socket is used. To log to a
local syslogd, "WebsockifySysLogHandler(address="/dev/log")" can be local syslogd, "WebsockifySysLogHandler(address="/dev/log")" can be
used. If facility is not specified, LOG_USER is used. If socktype is used. If facility is not specified, LOG_USER is used. If socktype is
specified as socket.SOCK_DGRAM or socket.SOCK_STREAM, that specific specified as socket.SOCK_DGRAM or socket.SOCK_STREAM, that specific
socket type will be used. For Unix sockets, you can also specify a 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 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 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 used as the application name in all messages sent. Set legacy to True
to use the old version of the protocol. to use the old version of the protocol.
""" """
self.ident = ident self.ident = ident
if legacy: if legacy:
self._legacy = True self._legacy = True
self._head_fmt = self._legacy_head_fmt self._head_fmt = self._legacy_head_fmt
super().__init__(address, facility, socktype) super().__init__(address, facility, socktype)
def emit(self, record): def emit(self, record):
""" """
Emit a record. Emit a record.
The record is formatted, and then sent to the syslog server. If The record is formatted, and then sent to the syslog server. If
exception information is present, it is NOT sent to the server. exception information is present, it is NOT sent to the server.
""" """
try: try:
# Gather info. # Gather info.
text = self.format(record).replace(self._tail, ' ') text = self.format(record).replace(self._tail, ' ')
if not text: # nothing to log if not text: # nothing to log
return return
pri = self.encodePriority(self.facility, pri = self.encodePriority(self.facility,
self.mapPriority(record.levelname)) self.mapPriority(record.levelname))
timestamp = time.strftime(self._timestamp_fmt, time.gmtime()); timestamp = time.strftime(self._timestamp_fmt, time.gmtime());
hostname = socket.gethostname()[:self._max_hostname] hostname = socket.gethostname()[:self._max_hostname]
if self.ident: if self.ident:
ident = self.ident[:self._max_ident] ident = self.ident[:self._max_ident]
else: else:
ident = '' ident = ''
pid = os.getpid() # shouldn't need truncation pid = os.getpid() # shouldn't need truncation
# Format the header. # Format the header.
head = { head = {
'pri': pri, 'pri': pri,
'timestamp': timestamp, 'timestamp': timestamp,
'hostname': hostname, 'hostname': hostname,
'ident': ident, 'ident': ident,
'pid': pid, 'pid': pid,
} }
msg = self._head_fmt.format(**head).encode('ascii', 'ignore') msg = self._head_fmt.format(**head).encode('ascii', 'ignore')
# Encode text as plain ASCII if possible, else use UTF-8 with BOM. # Encode text as plain ASCII if possible, else use UTF-8 with BOM.
try: try:
msg += text.encode('ascii') msg += text.encode('ascii')
except UnicodeEncodeError: except UnicodeEncodeError:
msg += text.encode('utf-8-sig') msg += text.encode('utf-8-sig')
# Add length or tail character, if necessary. # Add length or tail character, if necessary.
if self.socktype != socket.SOCK_DGRAM: if self.socktype != socket.SOCK_DGRAM:
if self._send_length: if self._send_length:
msg = ('%d ' % len(msg)).encode('ascii') + msg msg = ('%d ' % len(msg)).encode('ascii') + msg
else: else:
msg += self._tail.encode('ascii') msg += self._tail.encode('ascii')
# Send the message. # Send the message.
if self.unixsocket: if self.unixsocket:
try: try:
self.socket.send(msg) self.socket.send(msg)
except OSError: except OSError:
self._connect_unixsocket(self.address) self._connect_unixsocket(self.address)
self.socket.send(msg) self.socket.send(msg)
else: else:
if self.socktype == socket.SOCK_DGRAM: if self.socktype == socket.SOCK_DGRAM:
self.socket.sendto(msg, self.address) self.socket.sendto(msg, self.address)
else: else:
self.socket.sendall(msg) self.socket.sendall(msg)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
raise raise
except: except:
self.handleError(record) self.handleError(record)