source: vendor/python/2.5/Modules/syslogmodule.c

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 6.5 KB
Line 
1/***********************************************************
2Copyright 1994 by Lance Ellinghouse,
3Cathedral City, California Republic, United States of America.
4
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
11supporting 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.
14
15LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, 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.
22
23******************************************************************/
24
25/******************************************************************
26
27Revision history:
28
291998/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
3595/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.
42
43******************************************************************/
44
45/* syslog module */
46
47#include "Python.h"
48
49#include <syslog.h>
50
51/* only one instance, only one syslog, so globals should be ok */
52static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
53
54
55static PyObject *
56syslog_openlog(PyObject * self, PyObject * args)
57{
58 long logopt = 0;
59 long facility = LOG_USER;
60 PyObject *new_S_ident_o;
61
62 if (!PyArg_ParseTuple(args,
63 "S|ll;ident string [, logoption [, facility]]",
64 &new_S_ident_o, &logopt, &facility))
65 return NULL;
66
67 /* This is needed because openlog() does NOT make a copy
68 * and syslog() later uses it.. cannot trash it.
69 */
70 Py_XDECREF(S_ident_o);
71 S_ident_o = new_S_ident_o;
72 Py_INCREF(S_ident_o);
73
74 openlog(PyString_AsString(S_ident_o), logopt, facility);
75
76 Py_INCREF(Py_None);
77 return Py_None;
78}
79
80
81static PyObject *
82syslog_syslog(PyObject * self, PyObject * args)
83{
84 char *message;
85 int priority = LOG_INFO;
86
87 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 }
94
95 syslog(priority, "%s", message);
96 Py_INCREF(Py_None);
97 return Py_None;
98}
99
100static PyObject *
101syslog_closelog(PyObject *self, PyObject *unused)
102{
103 closelog();
104 Py_XDECREF(S_ident_o);
105 S_ident_o = NULL;
106 Py_INCREF(Py_None);
107 return Py_None;
108}
109
110static PyObject *
111syslog_setlogmask(PyObject *self, PyObject *args)
112{
113 long maskpri, omaskpri;
114
115 if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
116 return NULL;
117 omaskpri = setlogmask(maskpri);
118 return PyInt_FromLong(omaskpri);
119}
120
121static PyObject *
122syslog_log_mask(PyObject *self, PyObject *args)
123{
124 long mask;
125 long pri;
126 if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
127 return NULL;
128 mask = LOG_MASK(pri);
129 return PyInt_FromLong(mask);
130}
131
132static PyObject *
133syslog_log_upto(PyObject *self, PyObject *args)
134{
135 long mask;
136 long pri;
137 if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
138 return NULL;
139 mask = LOG_UPTO(pri);
140 return PyInt_FromLong(mask);
141}
142
143/* List of functions defined in the module */
144
145static PyMethodDef syslog_methods[] = {
146 {"openlog", syslog_openlog, METH_VARARGS},
147 {"closelog", syslog_closelog, METH_NOARGS},
148 {"syslog", syslog_syslog, METH_VARARGS},
149 {"setlogmask", syslog_setlogmask, METH_VARARGS},
150 {"LOG_MASK", syslog_log_mask, METH_VARARGS},
151 {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
152 {NULL, NULL, 0}
153};
154
155/* Initialization function for the module */
156
157PyMODINIT_FUNC
158initsyslog(void)
159{
160 PyObject *m;
161
162 /* Create the module and add the functions */
163 m = Py_InitModule("syslog", syslog_methods);
164 if (m == NULL)
165 return;
166
167 /* Add some symbolic constants to the module */
168
169 /* Priorities */
170 PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
171 PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
172 PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
173 PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
174 PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
175 PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
176 PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
177 PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
178
179 /* openlog() option flags */
180 PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
181 PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
182 PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
183#ifdef LOG_NOWAIT
184 PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
185#endif
186#ifdef LOG_PERROR
187 PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
188#endif
189
190 /* Facilities */
191 PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
192 PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
193 PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
194 PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
195 PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
196 PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
197 PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
198 PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
199 PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
200 PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
201 PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
202 PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
203 PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
204 PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
205
206#ifndef LOG_SYSLOG
207#define LOG_SYSLOG LOG_DAEMON
208#endif
209#ifndef LOG_NEWS
210#define LOG_NEWS LOG_MAIL
211#endif
212#ifndef LOG_UUCP
213#define LOG_UUCP LOG_MAIL
214#endif
215#ifndef LOG_CRON
216#define LOG_CRON LOG_DAEMON
217#endif
218
219 PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
220 PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
221 PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
222 PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
223}
Note: See TracBrowser for help on using the repository browser.