Blocked revisions 73580-73582 via svnmerge
[python/dscho.git] / Modules / syslogmodule.c
blobc6a3b36e269a01ff31538de890c2f861f1c969af
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;
60 PyObject *new_S_ident_o;
61 const char *ident;
63 if (!PyArg_ParseTuple(args,
64 "U|ll;ident string [, logoption [, facility]]",
65 &new_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_XDECREF(S_ident_o);
72 S_ident_o = new_S_ident_o;
73 Py_INCREF(S_ident_o);
75 ident = _PyUnicode_AsString(S_ident_o);
76 if (ident == NULL)
77 return NULL;
78 openlog(ident, logopt, facility);
80 Py_INCREF(Py_None);
81 return Py_None;
85 static PyObject *
86 syslog_syslog(PyObject * self, PyObject * args)
88 PyObject *message_object;
89 const char *message;
90 int priority = LOG_INFO;
92 if (!PyArg_ParseTuple(args, "iU;[priority,] message string",
93 &priority, &message_object)) {
94 PyErr_Clear();
95 if (!PyArg_ParseTuple(args, "U;[priority,] message string",
96 &message_object))
97 return NULL;
100 message = _PyUnicode_AsString(message_object);
101 if (message == NULL)
102 return NULL;
103 Py_BEGIN_ALLOW_THREADS;
104 syslog(priority, "%s", message);
105 Py_END_ALLOW_THREADS;
106 Py_RETURN_NONE;
109 static PyObject *
110 syslog_closelog(PyObject *self, PyObject *unused)
112 closelog();
113 Py_XDECREF(S_ident_o);
114 S_ident_o = NULL;
115 Py_INCREF(Py_None);
116 return Py_None;
119 static PyObject *
120 syslog_setlogmask(PyObject *self, PyObject *args)
122 long maskpri, omaskpri;
124 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
125 return NULL;
126 omaskpri = setlogmask(maskpri);
127 return PyLong_FromLong(omaskpri);
130 static PyObject *
131 syslog_log_mask(PyObject *self, PyObject *args)
133 long mask;
134 long pri;
135 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
136 return NULL;
137 mask = LOG_MASK(pri);
138 return PyLong_FromLong(mask);
141 static PyObject *
142 syslog_log_upto(PyObject *self, PyObject *args)
144 long mask;
145 long pri;
146 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
147 return NULL;
148 mask = LOG_UPTO(pri);
149 return PyLong_FromLong(mask);
152 /* List of functions defined in the module */
154 static PyMethodDef syslog_methods[] = {
155 {"openlog", syslog_openlog, METH_VARARGS},
156 {"closelog", syslog_closelog, METH_NOARGS},
157 {"syslog", syslog_syslog, METH_VARARGS},
158 {"setlogmask", syslog_setlogmask, METH_VARARGS},
159 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
160 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
161 {NULL, NULL, 0}
164 /* Initialization function for the module */
167 static struct PyModuleDef syslogmodule = {
168 PyModuleDef_HEAD_INIT,
169 "syslog",
170 NULL,
172 syslog_methods,
173 NULL,
174 NULL,
175 NULL,
176 NULL
179 PyMODINIT_FUNC
180 PyInit_syslog(void)
182 PyObject *m;
184 /* Create the module and add the functions */
185 m = PyModule_Create(&syslogmodule);
186 if (m == NULL)
187 return NULL;
189 /* Add some symbolic constants to the module */
191 /* Priorities */
192 PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
193 PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
194 PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
195 PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
196 PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
197 PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
198 PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
199 PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
201 /* openlog() option flags */
202 PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
203 PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
204 PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
205 #ifdef LOG_NOWAIT
206 PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
207 #endif
208 #ifdef LOG_PERROR
209 PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
210 #endif
212 /* Facilities */
213 PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
214 PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
215 PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
216 PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
217 PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
218 PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
219 PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
220 PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
221 PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
222 PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
223 PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
224 PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
225 PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
226 PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
228 #ifndef LOG_SYSLOG
229 #define LOG_SYSLOG LOG_DAEMON
230 #endif
231 #ifndef LOG_NEWS
232 #define LOG_NEWS LOG_MAIL
233 #endif
234 #ifndef LOG_UUCP
235 #define LOG_UUCP LOG_MAIL
236 #endif
237 #ifndef LOG_CRON
238 #define LOG_CRON LOG_DAEMON
239 #endif
241 PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
242 PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
243 PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
244 PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
245 return m;