1 /***********************************************************
2 Copyright 1994 by Lance Ellinghouse,
3 Cathedral City, California Republic, United States of America.
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 /******************************************************************
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 behaviour.
40 - Plug memory leak in openlog().
41 - Fix setlogmask() to return previous mask value.
43 ******************************************************************/
51 /* only one instance, only one syslog, so globals should be ok */
52 static PyObject
*S_ident_o
= NULL
; /* identifier, held by openlog() */
56 syslog_openlog(self
, args
)
61 long facility
= LOG_USER
;
64 Py_XDECREF(S_ident_o
);
65 if (!PyArg_ParseTuple(args
,
66 "S|ll;ident string [, logoption [, facility]]",
67 &S_ident_o
, &logopt
, &facility
))
70 /* This is needed because openlog() does NOT make a copy
71 * and syslog() later uses it.. cannot trash it.
75 openlog(PyString_AsString(S_ident_o
), logopt
, facility
);
83 syslog_syslog(self
, args
)
88 int priority
= LOG_INFO
;
90 if (!PyArg_ParseTuple(args
, "is;[priority,] message string",
91 &priority
, &message
)) {
93 if (!PyArg_ParseTuple(args
, "s;[priority,] message string",
98 syslog(priority
, "%s", message
);
104 syslog_closelog(self
, args
)
108 if (!PyArg_ParseTuple(args
, ""))
111 Py_XDECREF(S_ident_o
);
118 syslog_setlogmask(self
, args
)
122 long maskpri
, omaskpri
;
124 if (!PyArg_ParseTuple(args
, "l;mask for priority", &maskpri
))
126 omaskpri
= setlogmask(maskpri
);
127 return PyInt_FromLong(omaskpri
);
131 syslog_log_mask(self
, args
)
137 if (!PyArg_ParseTuple(args
, "l", &pri
))
139 mask
= LOG_MASK(pri
);
140 return PyInt_FromLong(mask
);
144 syslog_log_upto(self
, args
)
150 if (!PyArg_ParseTuple(args
, "l", &pri
))
152 mask
= LOG_UPTO(pri
);
153 return PyInt_FromLong(mask
);
156 /* List of functions defined in the module */
158 static PyMethodDef syslog_methods
[] = {
159 {"openlog", syslog_openlog
, METH_VARARGS
},
160 {"closelog", syslog_closelog
, METH_VARARGS
},
161 {"syslog", syslog_syslog
, METH_VARARGS
},
162 {"setlogmask", syslog_setlogmask
, METH_VARARGS
},
163 {"LOG_MASK", syslog_log_mask
, METH_VARARGS
},
164 {"LOG_UPTO", syslog_log_upto
, METH_VARARGS
},
168 /* helper function for initialization function */
176 PyObject
*v
= PyInt_FromLong(x
);
178 PyDict_SetItemString(d
, s
, v
);
183 /* Initialization function for the module */
190 /* Create the module and add the functions */
191 m
= Py_InitModule("syslog", syslog_methods
);
193 /* Add some symbolic constants to the module */
194 d
= PyModule_GetDict(m
);
197 ins(d
, "LOG_EMERG", LOG_EMERG
);
198 ins(d
, "LOG_ALERT", LOG_ALERT
);
199 ins(d
, "LOG_CRIT", LOG_CRIT
);
200 ins(d
, "LOG_ERR", LOG_ERR
);
201 ins(d
, "LOG_WARNING", LOG_WARNING
);
202 ins(d
, "LOG_NOTICE", LOG_NOTICE
);
203 ins(d
, "LOG_INFO", LOG_INFO
);
204 ins(d
, "LOG_DEBUG", LOG_DEBUG
);
206 /* openlog() option flags */
207 ins(d
, "LOG_PID", LOG_PID
);
208 ins(d
, "LOG_CONS", LOG_CONS
);
209 ins(d
, "LOG_NDELAY", LOG_NDELAY
);
211 ins(d
, "LOG_NOWAIT", LOG_NOWAIT
);
214 ins(d
, "LOG_PERROR", LOG_PERROR
);
218 ins(d
, "LOG_KERN", LOG_KERN
);
219 ins(d
, "LOG_USER", LOG_USER
);
220 ins(d
, "LOG_MAIL", LOG_MAIL
);
221 ins(d
, "LOG_DAEMON", LOG_DAEMON
);
222 ins(d
, "LOG_AUTH", LOG_AUTH
);
223 ins(d
, "LOG_LPR", LOG_LPR
);
224 ins(d
, "LOG_LOCAL0", LOG_LOCAL0
);
225 ins(d
, "LOG_LOCAL1", LOG_LOCAL1
);
226 ins(d
, "LOG_LOCAL2", LOG_LOCAL2
);
227 ins(d
, "LOG_LOCAL3", LOG_LOCAL3
);
228 ins(d
, "LOG_LOCAL4", LOG_LOCAL4
);
229 ins(d
, "LOG_LOCAL5", LOG_LOCAL5
);
230 ins(d
, "LOG_LOCAL6", LOG_LOCAL6
);
231 ins(d
, "LOG_LOCAL7", LOG_LOCAL7
);
234 #define LOG_SYSLOG LOG_DAEMON
237 #define LOG_NEWS LOG_MAIL
240 #define LOG_UUCP LOG_MAIL
243 #define LOG_CRON LOG_DAEMON
246 ins(d
, "LOG_SYSLOG", LOG_SYSLOG
);
247 ins(d
, "LOG_CRON", LOG_CRON
);
248 ins(d
, "LOG_UUCP", LOG_UUCP
);
249 ins(d
, "LOG_NEWS", LOG_NEWS
);
251 /* Check for errors */
252 if (PyErr_Occurred())
253 Py_FatalError("can't initialize module syslog");