home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Modules / syslogmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  6.4 KB  |  255 lines

  1. /***********************************************************
  2. Copyright 1994 by Lance Ellinghouse,
  3. Cathedral City, California Republic, United States of America.
  4.  
  5.                         All Rights Reserved
  6.  
  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.
  14.  
  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.
  22.  
  23. ******************************************************************/
  24.  
  25. /******************************************************************
  26.  
  27. Revision history:
  28.  
  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.
  34.  
  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 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. #if defined(AMITCP) || defined(INET225)
  51. #include <proto/socket.h>
  52. #endif
  53.  
  54. /*  only one instance, only one syslog, so globals should be ok  */
  55. static PyObject *S_ident_o = NULL;            /*  identifier, held by openlog()  */
  56.  
  57.  
  58. #ifndef INET225
  59. static PyObject * 
  60. syslog_openlog(PyObject * self, PyObject * args)
  61. {
  62.     long logopt = 0;
  63.     long facility = LOG_USER;
  64.  
  65.  
  66.     Py_XDECREF(S_ident_o);
  67.     if (!PyArg_ParseTuple(args,
  68.                   "S|ll;ident string [, logoption [, facility]]",
  69.                   &S_ident_o, &logopt, &facility))
  70.         return NULL;
  71.  
  72.     /* This is needed because openlog() does NOT make a copy
  73.      * and syslog() later uses it.. cannot trash it.
  74.      */
  75.     Py_INCREF(S_ident_o);
  76.  
  77.     openlog(PyString_AsString(S_ident_o), logopt, facility);
  78.  
  79.     Py_INCREF(Py_None);
  80.     return Py_None;
  81. }
  82. #endif /* !INET225 */
  83.  
  84.  
  85. static PyObject * 
  86. syslog_syslog(PyObject * self, PyObject * args)
  87. {
  88.     char *message;
  89.     int   priority = LOG_INFO;
  90.  
  91.     if (!PyArg_ParseTuple(args, "is;[priority,] message string",
  92.                   &priority, &message)) {
  93.         PyErr_Clear();
  94.         if (!PyArg_ParseTuple(args, "s;[priority,] message string",
  95.                       &message))
  96.             return NULL;
  97.     }
  98.  
  99.     syslog(priority, "%s", message);
  100.     Py_INCREF(Py_None);
  101.     return Py_None;
  102. }
  103.  
  104. #ifndef INET225
  105. static PyObject * 
  106. syslog_closelog(PyObject *self, PyObject *args)
  107. {
  108.     if (!PyArg_ParseTuple(args, ":closelog"))
  109.         return NULL;
  110.     closelog();
  111.     Py_XDECREF(S_ident_o);
  112.     S_ident_o = NULL;
  113.     Py_INCREF(Py_None);
  114.     return Py_None;
  115. }
  116.  
  117. static PyObject * 
  118. syslog_setlogmask(PyObject *self, PyObject *args)
  119. {
  120.     long maskpri, omaskpri;
  121.  
  122.     if (!PyArg_ParseTuple(args, "l;mask for priority", &maskpri))
  123.         return NULL;
  124.     omaskpri = setlogmask(maskpri);
  125.     return PyInt_FromLong(omaskpri);
  126. }
  127.  
  128. static PyObject * 
  129. syslog_log_mask(PyObject *self, PyObject *args)
  130. {
  131.     long mask;
  132.     long pri;
  133.     if (!PyArg_ParseTuple(args, "l:LOG_MASK", &pri))
  134.         return NULL;
  135.     mask = LOG_MASK(pri);
  136.     return PyInt_FromLong(mask);
  137. }
  138.  
  139. static PyObject * 
  140. syslog_log_upto(PyObject *self, PyObject *args)
  141. {
  142.     long mask;
  143.     long pri;
  144.     if (!PyArg_ParseTuple(args, "l:LOG_UPTO", &pri))
  145.         return NULL;
  146.     mask = LOG_UPTO(pri);
  147.     return PyInt_FromLong(mask);
  148. }
  149.  
  150. /* List of functions defined in the module */
  151.  
  152. static PyMethodDef syslog_methods[] = {
  153.     {"openlog",    syslog_openlog,        METH_VARARGS},
  154.     {"closelog",    syslog_closelog,    METH_VARARGS},
  155.     {"syslog",    syslog_syslog,        METH_VARARGS},
  156.     {"setlogmask",    syslog_setlogmask,    METH_VARARGS},
  157.     {"LOG_MASK",    syslog_log_mask,    METH_VARARGS},
  158.     {"LOG_UPTO",    syslog_log_upto,    METH_VARARGS},
  159.     {NULL,        NULL,            0}
  160. };
  161.  
  162. #endif /* !INET225 */
  163.  
  164. #ifdef INET225
  165. /* List of functions defined in the module */
  166. /* I-Net 225 only has syslog */
  167.  
  168. static PyMethodDef syslog_methods[] = {
  169.     {"syslog",    syslog_syslog,        METH_VARARGS},
  170.     {NULL,        NULL,            0}
  171. };
  172. #endif /* INET225 */
  173.  
  174.  
  175. /* helper function for initialization function */
  176.  
  177. static void
  178. ins(PyObject *d, char *s, long x)
  179. {
  180.     PyObject *v = PyInt_FromLong(x);
  181.     if (v) {
  182.         PyDict_SetItemString(d, s, v);
  183.         Py_DECREF(v);
  184.     }
  185. }
  186.  
  187. /* Initialization function for the module */
  188.  
  189. DL_EXPORT(void)
  190. initsyslog(void)
  191. {
  192.     PyObject *m, *d;
  193.  
  194.     /* Create the module and add the functions */
  195.     m = Py_InitModule("syslog", syslog_methods);
  196.  
  197.     /* Add some symbolic constants to the module */
  198.     d = PyModule_GetDict(m);
  199.  
  200.     /* Priorities */
  201.     ins(d, "LOG_EMERG",    LOG_EMERG);
  202.     ins(d, "LOG_ALERT",    LOG_ALERT);
  203.     ins(d, "LOG_CRIT",    LOG_CRIT);
  204.     ins(d, "LOG_ERR",    LOG_ERR);
  205.     ins(d, "LOG_WARNING",    LOG_WARNING);
  206.     ins(d, "LOG_NOTICE",    LOG_NOTICE);
  207.     ins(d, "LOG_INFO",    LOG_INFO);
  208.     ins(d, "LOG_DEBUG",    LOG_DEBUG);
  209.  
  210.     /* openlog() option flags */
  211.     ins(d, "LOG_PID",    LOG_PID);
  212.     ins(d, "LOG_CONS",    LOG_CONS);
  213.     ins(d, "LOG_NDELAY",    LOG_NDELAY);
  214. #ifdef LOG_NOWAIT
  215.     ins(d, "LOG_NOWAIT",    LOG_NOWAIT);
  216. #endif
  217. #ifdef LOG_PERROR
  218.     ins(d, "LOG_PERROR",    LOG_PERROR);
  219. #endif
  220.  
  221.     /* Facilities */
  222.     ins(d, "LOG_KERN",    LOG_KERN);
  223.     ins(d, "LOG_USER",    LOG_USER);
  224.     ins(d, "LOG_MAIL",    LOG_MAIL);
  225.     ins(d, "LOG_DAEMON",    LOG_DAEMON);
  226.     ins(d, "LOG_AUTH",    LOG_AUTH);
  227.     ins(d, "LOG_LPR",    LOG_LPR);
  228.     ins(d, "LOG_LOCAL0",    LOG_LOCAL0);
  229.     ins(d, "LOG_LOCAL1",    LOG_LOCAL1);
  230.     ins(d, "LOG_LOCAL2",    LOG_LOCAL2);
  231.     ins(d, "LOG_LOCAL3",    LOG_LOCAL3);
  232.     ins(d, "LOG_LOCAL4",    LOG_LOCAL4);
  233.     ins(d, "LOG_LOCAL5",    LOG_LOCAL5);
  234.     ins(d, "LOG_LOCAL6",    LOG_LOCAL6);
  235.     ins(d, "LOG_LOCAL7",    LOG_LOCAL7);
  236.  
  237. #ifndef LOG_SYSLOG
  238. #define LOG_SYSLOG        LOG_DAEMON
  239. #endif
  240. #ifndef LOG_NEWS
  241. #define LOG_NEWS        LOG_MAIL
  242. #endif
  243. #ifndef LOG_UUCP
  244. #define LOG_UUCP        LOG_MAIL
  245. #endif
  246. #ifndef LOG_CRON
  247. #define LOG_CRON        LOG_DAEMON
  248. #endif
  249.  
  250.     ins(d, "LOG_SYSLOG",    LOG_SYSLOG);
  251.     ins(d, "LOG_CRON",    LOG_CRON);
  252.     ins(d, "LOG_UUCP",    LOG_UUCP);
  253.     ins(d, "LOG_NEWS",    LOG_NEWS);
  254. }
  255.