]> jfr.im git - irc/quakenet/qwebirc.git/blame - util/syslog.py
Add custom menu items.
[irc/quakenet/qwebirc.git] / util / syslog.py
CommitLineData
39cf79cc
CP
1"""
2Limited drop in replacement for the syslog module.
3To use this symlink it into your PYTHONPATH.
4"""
5
6from twisted.internet.protocol import DatagramProtocol
7import os
8
9from config import SYSLOG_ADDR as ADDR
10IDENT = "syslog"
11
12protocol, opened = None, False
13
14class __SyslogProtocol(DatagramProtocol):
15 def __init__(self):
16 self.pid = os.getpid() # FORK WARNING
17
18 def send(self, data):
19 if self.transport is None: # oh well, it's UDP =)
20 return
21 self.transport.write("<1> %s[%d]: %s\n" % (self.ident, self.pid, data), ADDR)
22
23 def close(self):
24 if self.transport is None:
25 return
26 self.transport.stopListening()
27
28def __open_protocol():
29 global opened
30
31 if opened:
32 return
33
34 opened = True
35 from twisted.internet import reactor
36 reactor.listenUDP(0, protocol)
37
38def __build_protocol(ident=IDENT):
39 global protocol
40
41 if protocol is not None:
42 return
43
44 protocol = __SyslogProtocol()
45 protocol.ident = ident
46
47def syslog(data):
48 __build_protocol()
49 __open_protocol()
50 protocol.send(data)
51
52def openlog(ident, logopt=None, facility=None):
53 __build_protocol(ident)
54
55def closelog():
56 global protocol, opened
57
58 opened = False
59 if protocol is None:
60 return
61
62 protocol.close()
63 protocol = None
64
65def setlogmask(maskpri):
66 pass
67
68if __name__ == "__main__":
69 from twisted.internet import reactor
70 openlog("wibble")
71 syslog("HI\n")
72 closelog()
73 reactor.run()
74
75LOG_ALERT = LOG_AUTH = LOG_CONS = LOG_CRIT = LOG_CRON = LOG_DAEMON = LOG_DEBUG = LOG_EMERG = LOG_ERR = LOG_INFO = \
76LOG_KERN = LOG_LOCAL0 = LOG_LOCAL1 = LOG_LOCAL2 = LOG_LOCAL3 = LOG_LOCAL4 = LOG_LOCAL5 = LOG_LOCAL6 = LOG_LOCAL7 = \
77LOG_LPR = LOG_MAIL = LOG_NDELAY = LOG_NEWS = LOG_NOTICE = LOG_NOWAIT = LOG_PERROR = LOG_PID = LOG_SYSLOG = \
78LOG_UPTO = LOG_USER = LOG_UUCP = LOG_WARNING = 0
79
80LOG_MASK = LOG_UPTO = lambda *args, **kwargs: 0