Use py_resource module
[python/dscho.git] / Modules / syslogmodule.c
blob06c4f342186c68eddd6aab409d36ecf8ad09e741
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 95/06/29 (Steve Clift)
30 - Changed arg parsing to use PyArg_ParseTuple.
31 - Added PyErr_Clear() call(s) where needed.
32 - Fix core dumps if user message contains format specifiers.
33 - Change openlog arg defaults to match normal syslog behaviour.
34 - Plug memory leak in openlog().
35 - Fix setlogmask() to return previous mask value.
37 ******************************************************************/
39 /* syslog module */
41 #include "Python.h"
43 #include <syslog.h>
45 static PyObject *
46 syslog_openlog(self, args)
47 PyObject * self;
48 PyObject * args;
50 long logopt = 0;
51 long facility = LOG_USER;
53 static PyObject *ident_o = NULL;
55 Py_XDECREF(ident_o);
56 if (!PyArg_ParseTuple(args, "S|ll;ident string [, logoption [, facility]]",
57 &ident_o, &logopt, &facility)) {
58 return NULL;
60 Py_INCREF(ident_o); /* This is needed because openlog() does NOT make a copy
61 and syslog() later uses it.. cannot trash it. */
63 openlog(PyString_AsString(ident_o), logopt, facility);
65 Py_INCREF(Py_None);
66 return Py_None;
69 static PyObject *
70 syslog_syslog(self, args)
71 PyObject * self;
72 PyObject * args;
74 char *message, *s;
75 int priority = LOG_INFO | LOG_USER;
77 if (!PyArg_ParseTuple(args, "is;[priority,] message string",
78 &priority, &message)) {
79 PyErr_Clear();
80 if (!PyArg_ParseTuple(args, "s;[priority,] message string", &message)) {
81 return NULL;
84 syslog(priority, "%s", message);
85 Py_INCREF(Py_None);
86 return Py_None;
89 static PyObject *
90 syslog_closelog(self, args)
91 PyObject * self;
92 PyObject * args;
94 if (!PyArg_ParseTuple(args, ""))
95 return NULL;
96 closelog();
97 Py_INCREF(Py_None);
98 return Py_None;
101 static PyObject *
102 syslog_setlogmask(self, args)
103 PyObject * self;
104 PyObject * args;
106 long maskpri, omaskpri;
108 if (!PyArg_ParseTuple(args,"l;mask for priority",&maskpri))
109 return NULL;
110 omaskpri = setlogmask(maskpri);
111 return PyInt_FromLong(omaskpri);
114 static PyObject *
115 syslog_log_mask(self, args)
116 PyObject * self;
117 PyObject * args;
119 long mask;
120 long pri;
121 if (!PyArg_ParseTuple(args,"l",&pri))
122 return NULL;
123 mask = LOG_MASK(pri);
124 return PyInt_FromLong(mask);
127 static PyObject *
128 syslog_log_upto(self, args)
129 PyObject * self;
130 PyObject * args;
132 long mask;
133 long pri;
134 if (!PyArg_ParseTuple(args,"l",&pri))
135 return NULL;
136 mask = LOG_UPTO(pri);
137 return PyInt_FromLong(mask);
140 /* List of functions defined in the module */
142 static PyMethodDef syslog_methods[] = {
143 {"openlog", syslog_openlog, METH_VARARGS},
144 {"closelog", syslog_closelog, METH_VARARGS},
145 {"syslog", syslog_syslog, METH_VARARGS},
146 {"setlogmask", syslog_setlogmask, METH_VARARGS},
147 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
148 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
149 {NULL, NULL, 0}
152 /* Initialization function for the module */
154 #define DICT_SET_INT(d, s, x) \
155 PyDict_SetItemString(d, s, PyInt_FromLong((long) (x)))
157 void
158 initsyslog()
160 PyObject *m, *d;
162 /* Create the module and add the functions */
163 m = Py_InitModule("syslog", syslog_methods);
165 /* Add some symbolic constants to the module */
166 d = PyModule_GetDict(m);
168 /* Priorities */
169 DICT_SET_INT(d, "LOG_EMERG", LOG_EMERG);
170 DICT_SET_INT(d, "LOG_ALERT", LOG_ALERT);
171 DICT_SET_INT(d, "LOG_CRIT", LOG_CRIT);
172 DICT_SET_INT(d, "LOG_ERR", LOG_ERR);
173 DICT_SET_INT(d, "LOG_WARNING", LOG_WARNING);
174 DICT_SET_INT(d, "LOG_NOTICE", LOG_NOTICE);
175 DICT_SET_INT(d, "LOG_INFO", LOG_INFO);
176 DICT_SET_INT(d, "LOG_DEBUG", LOG_DEBUG);
178 /* openlog() option flags */
179 DICT_SET_INT(d, "LOG_PID", LOG_PID);
180 DICT_SET_INT(d, "LOG_CONS", LOG_CONS);
181 DICT_SET_INT(d, "LOG_NDELAY", LOG_NDELAY);
182 DICT_SET_INT(d, "LOG_NOWAIT", LOG_NOWAIT);
183 #ifdef LOG_PERROR
184 DICT_SET_INT(d, "LOG_PERROR", LOG_PERROR);
185 #endif
187 /* Facilities */
188 DICT_SET_INT(d, "LOG_KERN", LOG_KERN);
189 DICT_SET_INT(d, "LOG_USER", LOG_USER);
190 DICT_SET_INT(d, "LOG_MAIL", LOG_MAIL);
191 DICT_SET_INT(d, "LOG_DAEMON", LOG_DAEMON);
192 DICT_SET_INT(d, "LOG_AUTH", LOG_AUTH);
193 DICT_SET_INT(d, "LOG_LPR", LOG_LPR);
194 DICT_SET_INT(d, "LOG_NEWS", LOG_NEWS);
195 DICT_SET_INT(d, "LOG_UUCP", LOG_UUCP);
196 DICT_SET_INT(d, "LOG_CRON", LOG_CRON);
197 DICT_SET_INT(d, "LOG_LOCAL0", LOG_LOCAL0);
198 DICT_SET_INT(d, "LOG_LOCAL1", LOG_LOCAL1);
199 DICT_SET_INT(d, "LOG_LOCAL2", LOG_LOCAL2);
200 DICT_SET_INT(d, "LOG_LOCAL3", LOG_LOCAL3);
201 DICT_SET_INT(d, "LOG_LOCAL4", LOG_LOCAL4);
202 DICT_SET_INT(d, "LOG_LOCAL5", LOG_LOCAL5);
203 DICT_SET_INT(d, "LOG_LOCAL6", LOG_LOCAL6);
204 DICT_SET_INT(d, "LOG_LOCAL7", LOG_LOCAL7);
206 /* Check for errors */
207 if (PyErr_Occurred())
208 Py_FatalError("can't initialize module syslog");