[Zope-Checkins] CVS: Zope/lib/python/ZLogger - syslog.py:1.5.40.1
Toby Dickenson
tdickenson@geminidataloggers.com
Tue, 4 Feb 2003 05:43:06 -0500
Update of /cvs-repository/Zope/lib/python/ZLogger
In directory cvs.zope.org:/tmp/cvs-serv17999
Modified Files:
Tag: toby-syslog-reconnect-branch
syslog.py
Log Message:
syslog can write to a unix domain socket, usually /dev/log. This change allows the application to reconnect to the socket, for example if the listener restarts.
=== Zope/lib/python/ZLogger/syslog.py 1.5 => 1.5.40.1 ===
--- Zope/lib/python/ZLogger/syslog.py:1.5 Wed Aug 14 18:04:59 2002
+++ Zope/lib/python/ZLogger/syslog.py Tue Feb 4 05:43:02 2003
@@ -137,20 +137,23 @@
"local7": LOG_LOCAL7,
}
-import socket
+import socket, errno
class syslog_client:
def __init__ (self, address='/dev/log'):
self.address = address
- if type (address) == type(''):
+ self._connect()
+
+ def _connect(self):
+ if type (self.address) == type(''):
try: # APUE 13.4.2 specifes /dev/log as datagram socket
self.socket = socket.socket( socket.AF_UNIX
, socket.SOCK_DGRAM)
- self.socket.connect (address)
+ self.socket.connect (self.address)
except: # older linux may create as stream socket
self.socket = socket.socket( socket.AF_UNIX
, socket.SOCK_STREAM)
- self.socket.connect (address)
+ self.socket.connect (self.address)
self.unix = 1
else:
self.socket = socket.socket( socket.AF_INET
@@ -169,10 +172,20 @@
self.encode_priority (facility, priority),
message
)
- if self.unix:
- self.socket.send (message)
- else:
- self.socket.sendto (message, self.address)
+ for retries in (0,1):
+ try:
+ if self.unix:
+ self.socket.send (message)
+ else:
+ self.socket.sendto (message, self.address)
+ except socket.error,e:
+ if e[0] in (errno.ENOTCONN,errno.ECONNREFUSED):
+ self._connect()
+ # and retry
+ else:
+ raise
+ else:
+ break
def encode_priority (self, facility, priority):
if type(facility) == type(''):