Updated for 2.1a3
[python/dscho.git] / Modules / syslogmodule.c
blobeda5490c16f1314da39b153919273aa48a909c21
1 /***********************************************************
2 Copyright 1994 by Lance Ellinghouse,
3 Cathedral City, California Republic, United States of America.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of Lance Ellinghouse
12 not be used in advertising or publicity pertaining to distribution
13 of the software without specific, written prior permission.
15 LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
18 INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19 FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /******************************************************************
27 Revision history:
29 1998/04/28 (Sean Reifschneider)
30 - When facility not specified to syslog() method, use default from openlog()
31 (This is how it was claimed to work in the documentation)
32 - Potential resource leak of o_ident, now cleaned up in closelog()
33 - Minor comment accuracy fix.
35 95/06/29 (Steve Clift)
36 - Changed arg parsing to use PyArg_ParseTuple.
37 - Added PyErr_Clear() call(s) where needed.
38 - Fix core dumps if user message contains format specifiers.
39 - Change openlog arg defaults to match normal syslog behavior.
40 - Plug memory leak in openlog().
41 - Fix setlogmask() to return previous mask value.
43 ******************************************************************/
45 /* syslog module */
47 #include "Python.h"
49 #include <syslog.h>
51 /* only one instance, only one syslog, so globals should be ok */
52 static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
55 static PyObject *
56 syslog_openlog(PyObject * self, PyObject * args)
58 long logopt = 0;
59 long facility = LOG_USER;
62 Py_XDECREF(S_ident_o);
63 if (!PyArg_ParseTuple(args,
64 "S|ll;ident string [, logoption [, facility]]",
65 &S_ident_o, &logopt, &facility))
66 return NULL;
68 /* This is needed because openlog() does NOT make a copy
69 * and syslog() later uses it.. cannot trash it.
71 Py_INCREF(S_ident_o);
73 openlog(PyString_AsString(S_ident_o), logopt, facility);
75 Py_INCREF(Py_None);
76 return Py_None;
80 static PyObject *
81 syslog_syslog(PyObject * self, PyObject * args)
83 char *message;
84 int priority = LOG_INFO;
86 if (!PyArg_ParseTuple(args, "is;[priority,] message string",
87 &priority, &message)) {
88 PyErr_Clear();
89 if (!PyArg_ParseTuple(args, "s;[priority,] message string",
90 &message))
91 return NULL;
94 syslog(priority, "%s", message);
95 Py_INCREF(Py_None);
96 return Py_None;
99 static PyObject *
100 syslog_closelog(PyObject *self, PyObject *args)
102 if (!PyArg_ParseTuple(args, ":closelog"))
103 return NULL;
104 closelog();
105 Py_XDECREF(S_ident_o);
106 S_ident_o = NULL;
107 Py_INCREF(Py_None);
108 return Py_None;
111 static PyObject *
112 syslog_setlogmask(PyObject *self, PyObject *args)
114 long maskpri, omaskpri;
116 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
117 return NULL;
118 omaskpri = setlogmask(maskpri);
119 return PyInt_FromLong(omaskpri);
122 static PyObject *
123 syslog_log_mask(PyObject *self, PyObject *args)
125 long mask;
126 long pri;
127 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
128 return NULL;
129 mask = LOG_MASK(pri);
130 return PyInt_FromLong(mask);
133 static PyObject *
134 syslog_log_upto(PyObject *self, PyObject *args)
136 long mask;
137 long pri;
138 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
139 return NULL;
140 mask = LOG_UPTO(pri);
141 return PyInt_FromLong(mask);
144 /* List of functions defined in the module */
146 static PyMethodDef syslog_methods[] = {
147 {"openlog", syslog_openlog, METH_VARARGS},
148 {"closelog", syslog_closelog, METH_VARARGS},
149 {"syslog", syslog_syslog, METH_VARARGS},
150 {"setlogmask", syslog_setlogmask, METH_VARARGS},
151 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
152 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
153 {NULL, NULL, 0}
156 /* helper function for initialization function */
158 static void
159 ins(PyObject *d, char *s, long x)
161 PyObject *v = PyInt_FromLong(x);
162 if (v) {
163 PyDict_SetItemString(d, s, v);
164 Py_DECREF(v);
168 /* Initialization function for the module */
170 DL_EXPORT(void)
171 initsyslog(void)
173 PyObject *m, *d;
175 /* Create the module and add the functions */
176 m = Py_InitModule("syslog", syslog_methods);
178 /* Add some symbolic constants to the module */
179 d = PyModule_GetDict(m);
181 /* Priorities */
182 ins(d, "LOG_EMERG", LOG_EMERG);
183 ins(d, "LOG_ALERT", LOG_ALERT);
184 ins(d, "LOG_CRIT", LOG_CRIT);
185 ins(d, "LOG_ERR", LOG_ERR);
186 ins(d, "LOG_WARNING", LOG_WARNING);
187 ins(d, "LOG_NOTICE", LOG_NOTICE);
188 ins(d, "LOG_INFO", LOG_INFO);
189 ins(d, "LOG_DEBUG", LOG_DEBUG);
191 /* openlog() option flags */
192 ins(d, "LOG_PID", LOG_PID);
193 ins(d, "LOG_CONS", LOG_CONS);
194 ins(d, "LOG_NDELAY", LOG_NDELAY);
195 #ifdef LOG_NOWAIT
196 ins(d, "LOG_NOWAIT", LOG_NOWAIT);
197 #endif
198 #ifdef LOG_PERROR
199 ins(d, "LOG_PERROR", LOG_PERROR);
200 #endif
202 /* Facilities */
203 ins(d, "LOG_KERN", LOG_KERN);
204 ins(d, "LOG_USER", LOG_USER);
205 ins(d, "LOG_MAIL", LOG_MAIL);
206 ins(d, "LOG_DAEMON", LOG_DAEMON);
207 ins(d, "LOG_AUTH", LOG_AUTH);
208 ins(d, "LOG_LPR", LOG_LPR);
209 ins(d, "LOG_LOCAL0", LOG_LOCAL0);
210 ins(d, "LOG_LOCAL1", LOG_LOCAL1);
211 ins(d, "LOG_LOCAL2", LOG_LOCAL2);
212 ins(d, "LOG_LOCAL3", LOG_LOCAL3);
213 ins(d, "LOG_LOCAL4", LOG_LOCAL4);
214 ins(d, "LOG_LOCAL5", LOG_LOCAL5);
215 ins(d, "LOG_LOCAL6", LOG_LOCAL6);
216 ins(d, "LOG_LOCAL7", LOG_LOCAL7);
218 #ifndef LOG_SYSLOG
219 #define LOG_SYSLOG LOG_DAEMON
220 #endif
221 #ifndef LOG_NEWS
222 #define LOG_NEWS LOG_MAIL
223 #endif
224 #ifndef LOG_UUCP
225 #define LOG_UUCP LOG_MAIL
226 #endif
227 #ifndef LOG_CRON
228 #define LOG_CRON LOG_DAEMON
229 #endif
231 ins(d, "LOG_SYSLOG", LOG_SYSLOG);
232 ins(d, "LOG_CRON", LOG_CRON);
233 ins(d, "LOG_UUCP", LOG_UUCP);
234 ins(d, "LOG_NEWS", LOG_NEWS);