blob: 9e1ed0619f1ff7b3fd55c2ce04b937bc12a6d528 [file] [log] [blame]
Guido van Rossume44e3731994-07-14 13:56:50 +00001/***********************************************************
Guido van Rossumf6971e21994-08-30 12:25:20 +00002Copyright 1994 by Lance Ellinghouse,
3Cathedral City, California Republic, United States of America.
Guido van Rossume44e3731994-07-14 13:56:50 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
Guido van Rossumf6971e21994-08-30 12:25:20 +000011supporting documentation, and that the name of Lance Ellinghouse
12not be used in advertising or publicity pertaining to distribution
13of the software without specific, written prior permission.
Guido van Rossume44e3731994-07-14 13:56:50 +000014
Guido van Rossumf6971e21994-08-30 12:25:20 +000015LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
Guido van Rossume44e3731994-07-14 13:56:50 +000016THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
Guido van Rossumf6971e21994-08-30 12:25:20 +000017FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
18INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Guido van Rossume44e3731994-07-14 13:56:50 +000022
23******************************************************************/
24
Guido van Rossumc1822a41995-10-11 16:15:28 +000025/******************************************************************
26
27Revision history:
28
Guido van Rossumae94cf21998-05-08 21:52:55 +0000291998/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.
34
Guido van Rossumc1822a41995-10-11 16:15:28 +00003595/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.
Thomas Wouters7e474022000-07-16 12:04:32 +000039 - Change openlog arg defaults to match normal syslog behavior.
Guido van Rossumc1822a41995-10-11 16:15:28 +000040 - Plug memory leak in openlog().
41 - Fix setlogmask() to return previous mask value.
42
43******************************************************************/
44
Guido van Rossume44e3731994-07-14 13:56:50 +000045/* syslog module */
Guido van Rossume44e3731994-07-14 13:56:50 +000046
Guido van Rossuma597dde1995-01-10 20:56:29 +000047#include "Python.h"
Guido van Rossume44e3731994-07-14 13:56:50 +000048
49#include <syslog.h>
50
Guido van Rossumae94cf21998-05-08 21:52:55 +000051/* only one instance, only one syslog, so globals should be ok */
52static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
53
54
Guido van Rossumf6971e21994-08-30 12:25:20 +000055static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000056syslog_openlog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +000057{
Barry Warsaw43a476a1997-01-09 23:51:21 +000058 long logopt = 0;
59 long facility = LOG_USER;
Raymond Hettinger6f5b7412004-12-16 23:52:04 +000060 PyObject *new_S_ident_o;
Guido van Rossumc1822a41995-10-11 16:15:28 +000061
Barry Warsaw43a476a1997-01-09 23:51:21 +000062 if (!PyArg_ParseTuple(args,
63 "S|ll;ident string [, logoption [, facility]]",
Raymond Hettinger6f5b7412004-12-16 23:52:04 +000064 &new_S_ident_o, &logopt, &facility))
Barry Warsaw43a476a1997-01-09 23:51:21 +000065 return NULL;
Guido van Rossumc1822a41995-10-11 16:15:28 +000066
Barry Warsaw43a476a1997-01-09 23:51:21 +000067 /* This is needed because openlog() does NOT make a copy
68 * and syslog() later uses it.. cannot trash it.
69 */
Raymond Hettinger6f5b7412004-12-16 23:52:04 +000070 Py_XDECREF(S_ident_o);
71 S_ident_o = new_S_ident_o;
Guido van Rossumae94cf21998-05-08 21:52:55 +000072 Py_INCREF(S_ident_o);
Guido van Rossumc1822a41995-10-11 16:15:28 +000073
Gregory P. Smithdd96db62008-06-09 04:58:54 +000074 openlog(PyString_AsString(S_ident_o), logopt, facility);
Barry Warsaw43a476a1997-01-09 23:51:21 +000075
76 Py_INCREF(Py_None);
77 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +000078}
79
Barry Warsaw43a476a1997-01-09 23:51:21 +000080
Guido van Rossumf6971e21994-08-30 12:25:20 +000081static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +000082syslog_syslog(PyObject * self, PyObject * args)
Guido van Rossume44e3731994-07-14 13:56:50 +000083{
Barry Warsaw43a476a1997-01-09 23:51:21 +000084 char *message;
Guido van Rossumae94cf21998-05-08 21:52:55 +000085 int priority = LOG_INFO;
Guido van Rossume44e3731994-07-14 13:56:50 +000086
Barry Warsaw43a476a1997-01-09 23:51:21 +000087 if (!PyArg_ParseTuple(args, "is;[priority,] message string",
88 &priority, &message)) {
89 PyErr_Clear();
90 if (!PyArg_ParseTuple(args, "s;[priority,] message string",
91 &message))
92 return NULL;
93 }
Guido van Rossumae94cf21998-05-08 21:52:55 +000094
Christian Heimesf0476e82008-02-23 17:42:31 +000095 Py_BEGIN_ALLOW_THREADS;
Barry Warsaw43a476a1997-01-09 23:51:21 +000096 syslog(priority, "%s", message);
Christian Heimesf0476e82008-02-23 17:42:31 +000097 Py_END_ALLOW_THREADS;
Barry Warsaw43a476a1997-01-09 23:51:21 +000098 Py_INCREF(Py_None);
99 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000100}
101
Guido van Rossumf6971e21994-08-30 12:25:20 +0000102static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000103syslog_closelog(PyObject *self, PyObject *unused)
Guido van Rossume44e3731994-07-14 13:56:50 +0000104{
Guido van Rossume44e3731994-07-14 13:56:50 +0000105 closelog();
Guido van Rossumae94cf21998-05-08 21:52:55 +0000106 Py_XDECREF(S_ident_o);
107 S_ident_o = NULL;
Guido van Rossumf6971e21994-08-30 12:25:20 +0000108 Py_INCREF(Py_None);
109 return Py_None;
Guido van Rossume44e3731994-07-14 13:56:50 +0000110}
111
Guido van Rossumf6971e21994-08-30 12:25:20 +0000112static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000113syslog_setlogmask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000114{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000115 long maskpri, omaskpri;
Guido van Rossumc1822a41995-10-11 16:15:28 +0000116
Barry Warsaw43a476a1997-01-09 23:51:21 +0000117 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
118 return NULL;
119 omaskpri = setlogmask(maskpri);
120 return PyInt_FromLong(omaskpri);
Guido van Rossume44e3731994-07-14 13:56:50 +0000121}
122
Guido van Rossumf6971e21994-08-30 12:25:20 +0000123static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000124syslog_log_mask(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000125{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000126 long mask;
127 long pri;
Guido van Rossum43713e52000-02-29 13:59:29 +0000128 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
Barry Warsaw43a476a1997-01-09 23:51:21 +0000129 return NULL;
130 mask = LOG_MASK(pri);
131 return PyInt_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000132}
133
Guido van Rossumf6971e21994-08-30 12:25:20 +0000134static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000135syslog_log_upto(PyObject *self, PyObject *args)
Guido van Rossume44e3731994-07-14 13:56:50 +0000136{
Barry Warsaw43a476a1997-01-09 23:51:21 +0000137 long mask;
138 long pri;
Guido van Rossum43713e52000-02-29 13:59:29 +0000139 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
Barry Warsaw43a476a1997-01-09 23:51:21 +0000140 return NULL;
141 mask = LOG_UPTO(pri);
142 return PyInt_FromLong(mask);
Guido van Rossume44e3731994-07-14 13:56:50 +0000143}
144
145/* List of functions defined in the module */
146
Guido van Rossumf6971e21994-08-30 12:25:20 +0000147static PyMethodDef syslog_methods[] = {
Guido van Rossumc1822a41995-10-11 16:15:28 +0000148 {"openlog", syslog_openlog, METH_VARARGS},
Georg Brandl96a8c392006-05-29 21:04:52 +0000149 {"closelog", syslog_closelog, METH_NOARGS},
Guido van Rossumc1822a41995-10-11 16:15:28 +0000150 {"syslog", syslog_syslog, METH_VARARGS},
151 {"setlogmask", syslog_setlogmask, METH_VARARGS},
152 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
153 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
154 {NULL, NULL, 0}
Guido van Rossume44e3731994-07-14 13:56:50 +0000155};
156
Guido van Rossumae94cf21998-05-08 21:52:55 +0000157/* Initialization function for the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000158
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000159PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000160initsyslog(void)
Guido van Rossume44e3731994-07-14 13:56:50 +0000161{
Fred Drake4baedc12002-04-01 14:53:37 +0000162 PyObject *m;
Guido van Rossume44e3731994-07-14 13:56:50 +0000163
164 /* Create the module and add the functions */
Guido van Rossumf6971e21994-08-30 12:25:20 +0000165 m = Py_InitModule("syslog", syslog_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000166 if (m == NULL)
167 return;
Guido van Rossume44e3731994-07-14 13:56:50 +0000168
169 /* Add some symbolic constants to the module */
Guido van Rossumc1822a41995-10-11 16:15:28 +0000170
171 /* Priorities */
Fred Drake4baedc12002-04-01 14:53:37 +0000172 PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
173 PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
174 PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
175 PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
176 PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
177 PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
178 PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
179 PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000180
181 /* openlog() option flags */
Fred Drake4baedc12002-04-01 14:53:37 +0000182 PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
183 PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
184 PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000185#ifdef LOG_NOWAIT
Fred Drake4baedc12002-04-01 14:53:37 +0000186 PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
Guido van Rossumbcc20741998-08-04 22:53:56 +0000187#endif
Guido van Rossumc1822a41995-10-11 16:15:28 +0000188#ifdef LOG_PERROR
Fred Drake4baedc12002-04-01 14:53:37 +0000189 PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
Guido van Rossumc1822a41995-10-11 16:15:28 +0000190#endif
191
192 /* Facilities */
Fred Drake4baedc12002-04-01 14:53:37 +0000193 PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
194 PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
195 PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
196 PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
197 PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
198 PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
199 PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
200 PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
201 PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
202 PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
203 PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
204 PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
205 PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
206 PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
Guido van Rossume44e3731994-07-14 13:56:50 +0000207
Guido van Rossumae94cf21998-05-08 21:52:55 +0000208#ifndef LOG_SYSLOG
209#define LOG_SYSLOG LOG_DAEMON
210#endif
211#ifndef LOG_NEWS
212#define LOG_NEWS LOG_MAIL
213#endif
214#ifndef LOG_UUCP
215#define LOG_UUCP LOG_MAIL
216#endif
217#ifndef LOG_CRON
218#define LOG_CRON LOG_DAEMON
219#endif
220
Fred Drake4baedc12002-04-01 14:53:37 +0000221 PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
222 PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
223 PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
224 PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
Guido van Rossume44e3731994-07-14 13:56:50 +0000225}