/Modules/syslogmodule.c

http://unladen-swallow.googlecode.com/ · C · 225 lines · 140 code · 31 blank · 54 comment · 8 complexity · 7682c318a305495ee5be6b1fc28e25f9 MD5 · raw file

  1. /***********************************************************
  2. Copyright 1994 by Lance Ellinghouse,
  3. Cathedral City, California Republic, United States of America.
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and its
  6. documentation for any purpose and without fee is hereby granted,
  7. provided that the above copyright notice appear in all copies and that
  8. both that copyright notice and this permission notice appear in
  9. supporting documentation, and that the name of Lance Ellinghouse
  10. not be used in advertising or publicity pertaining to distribution
  11. of the software without specific, written prior permission.
  12. LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
  13. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL,
  15. INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  16. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  17. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  18. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. ******************************************************************/
  20. /******************************************************************
  21. Revision history:
  22. 1998/04/28 (Sean Reifschneider)
  23. - When facility not specified to syslog() method, use default from openlog()
  24. (This is how it was claimed to work in the documentation)
  25. - Potential resource leak of o_ident, now cleaned up in closelog()
  26. - Minor comment accuracy fix.
  27. 95/06/29 (Steve Clift)
  28. - Changed arg parsing to use PyArg_ParseTuple.
  29. - Added PyErr_Clear() call(s) where needed.
  30. - Fix core dumps if user message contains format specifiers.
  31. - Change openlog arg defaults to match normal syslog behavior.
  32. - Plug memory leak in openlog().
  33. - Fix setlogmask() to return previous mask value.
  34. ******************************************************************/
  35. /* syslog module */
  36. #include "Python.h"
  37. #include <syslog.h>
  38. /* only one instance, only one syslog, so globals should be ok */
  39. static PyObject *S_ident_o = NULL; /* identifier, held by openlog() */
  40. static PyObject *
  41. syslog_openlog(PyObject * self, PyObject * args)
  42. {
  43. long logopt = 0;
  44. long facility = LOG_USER;
  45. PyObject *new_S_ident_o;
  46. if (!PyArg_ParseTuple(args,
  47. "S|ll;ident string [, logoption [, facility]]",
  48. &new_S_ident_o, &logopt, &facility))
  49. return NULL;
  50. /* This is needed because openlog() does NOT make a copy
  51. * and syslog() later uses it.. cannot trash it.
  52. */
  53. Py_XDECREF(S_ident_o);
  54. S_ident_o = new_S_ident_o;
  55. Py_INCREF(S_ident_o);
  56. openlog(PyString_AsString(S_ident_o), logopt, facility);
  57. Py_INCREF(Py_None);
  58. return Py_None;
  59. }
  60. static PyObject *
  61. syslog_syslog(PyObject * self, PyObject * args)
  62. {
  63. char *message;
  64. int priority = LOG_INFO;
  65. if (!PyArg_ParseTuple(args, "is;[priority,] message string",
  66. &priority, &message)) {
  67. PyErr_Clear();
  68. if (!PyArg_ParseTuple(args, "s;[priority,] message string",
  69. &message))
  70. return NULL;
  71. }
  72. Py_BEGIN_ALLOW_THREADS;
  73. syslog(priority, "%s", message);
  74. Py_END_ALLOW_THREADS;
  75. Py_INCREF(Py_None);
  76. return Py_None;
  77. }
  78. static PyObject *
  79. syslog_closelog(PyObject *self, PyObject *unused)
  80. {
  81. closelog();
  82. Py_XDECREF(S_ident_o);
  83. S_ident_o = NULL;
  84. Py_INCREF(Py_None);
  85. return Py_None;
  86. }
  87. static PyObject *
  88. syslog_setlogmask(PyObject *self, PyObject *args)
  89. {
  90. long maskpri, omaskpri;
  91. if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
  92. return NULL;
  93. omaskpri = setlogmask(maskpri);
  94. return PyInt_FromLong(omaskpri);
  95. }
  96. static PyObject *
  97. syslog_log_mask(PyObject *self, PyObject *args)
  98. {
  99. long mask;
  100. long pri;
  101. if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
  102. return NULL;
  103. mask = LOG_MASK(pri);
  104. return PyInt_FromLong(mask);
  105. }
  106. static PyObject *
  107. syslog_log_upto(PyObject *self, PyObject *args)
  108. {
  109. long mask;
  110. long pri;
  111. if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
  112. return NULL;
  113. mask = LOG_UPTO(pri);
  114. return PyInt_FromLong(mask);
  115. }
  116. /* List of functions defined in the module */
  117. static PyMethodDef syslog_methods[] = {
  118. {"openlog", syslog_openlog, METH_VARARGS},
  119. {"closelog", syslog_closelog, METH_NOARGS},
  120. {"syslog", syslog_syslog, METH_VARARGS},
  121. {"setlogmask", syslog_setlogmask, METH_VARARGS},
  122. {"LOG_MASK", syslog_log_mask, METH_VARARGS},
  123. {"LOG_UPTO", syslog_log_upto, METH_VARARGS},
  124. {NULL, NULL, 0}
  125. };
  126. /* Initialization function for the module */
  127. PyMODINIT_FUNC
  128. initsyslog(void)
  129. {
  130. PyObject *m;
  131. /* Create the module and add the functions */
  132. m = Py_InitModule("syslog", syslog_methods);
  133. if (m == NULL)
  134. return;
  135. /* Add some symbolic constants to the module */
  136. /* Priorities */
  137. PyModule_AddIntConstant(m, "LOG_EMERG", LOG_EMERG);
  138. PyModule_AddIntConstant(m, "LOG_ALERT", LOG_ALERT);
  139. PyModule_AddIntConstant(m, "LOG_CRIT", LOG_CRIT);
  140. PyModule_AddIntConstant(m, "LOG_ERR", LOG_ERR);
  141. PyModule_AddIntConstant(m, "LOG_WARNING", LOG_WARNING);
  142. PyModule_AddIntConstant(m, "LOG_NOTICE", LOG_NOTICE);
  143. PyModule_AddIntConstant(m, "LOG_INFO", LOG_INFO);
  144. PyModule_AddIntConstant(m, "LOG_DEBUG", LOG_DEBUG);
  145. /* openlog() option flags */
  146. PyModule_AddIntConstant(m, "LOG_PID", LOG_PID);
  147. PyModule_AddIntConstant(m, "LOG_CONS", LOG_CONS);
  148. PyModule_AddIntConstant(m, "LOG_NDELAY", LOG_NDELAY);
  149. #ifdef LOG_NOWAIT
  150. PyModule_AddIntConstant(m, "LOG_NOWAIT", LOG_NOWAIT);
  151. #endif
  152. #ifdef LOG_PERROR
  153. PyModule_AddIntConstant(m, "LOG_PERROR", LOG_PERROR);
  154. #endif
  155. /* Facilities */
  156. PyModule_AddIntConstant(m, "LOG_KERN", LOG_KERN);
  157. PyModule_AddIntConstant(m, "LOG_USER", LOG_USER);
  158. PyModule_AddIntConstant(m, "LOG_MAIL", LOG_MAIL);
  159. PyModule_AddIntConstant(m, "LOG_DAEMON", LOG_DAEMON);
  160. PyModule_AddIntConstant(m, "LOG_AUTH", LOG_AUTH);
  161. PyModule_AddIntConstant(m, "LOG_LPR", LOG_LPR);
  162. PyModule_AddIntConstant(m, "LOG_LOCAL0", LOG_LOCAL0);
  163. PyModule_AddIntConstant(m, "LOG_LOCAL1", LOG_LOCAL1);
  164. PyModule_AddIntConstant(m, "LOG_LOCAL2", LOG_LOCAL2);
  165. PyModule_AddIntConstant(m, "LOG_LOCAL3", LOG_LOCAL3);
  166. PyModule_AddIntConstant(m, "LOG_LOCAL4", LOG_LOCAL4);
  167. PyModule_AddIntConstant(m, "LOG_LOCAL5", LOG_LOCAL5);
  168. PyModule_AddIntConstant(m, "LOG_LOCAL6", LOG_LOCAL6);
  169. PyModule_AddIntConstant(m, "LOG_LOCAL7", LOG_LOCAL7);
  170. #ifndef LOG_SYSLOG
  171. #define LOG_SYSLOG LOG_DAEMON
  172. #endif
  173. #ifndef LOG_NEWS
  174. #define LOG_NEWS LOG_MAIL
  175. #endif
  176. #ifndef LOG_UUCP
  177. #define LOG_UUCP LOG_MAIL
  178. #endif
  179. #ifndef LOG_CRON
  180. #define LOG_CRON LOG_DAEMON
  181. #endif
  182. PyModule_AddIntConstant(m, "LOG_SYSLOG", LOG_SYSLOG);
  183. PyModule_AddIntConstant(m, "LOG_CRON", LOG_CRON);
  184. PyModule_AddIntConstant(m, "LOG_UUCP", LOG_UUCP);
  185. PyModule_AddIntConstant(m, "LOG_NEWS", LOG_NEWS);
  186. }