Guido van Rossum | e44e373 | 1994-07-14 13:56:50 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, The Netherlands. |
| 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 names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* syslog module */ |
| 26 | /* (By Lance Ellinghouse) */ |
| 27 | |
| 28 | #include "allobjects.h" |
| 29 | #include "modsupport.h" |
| 30 | |
| 31 | #include <syslog.h> |
| 32 | |
| 33 | static object * |
| 34 | syslog_openlog(self, args) |
| 35 | object *self; |
| 36 | object *args; |
| 37 | { |
| 38 | char *ident = ""; |
| 39 | object *ident_o; |
| 40 | long logopt = LOG_PID; |
| 41 | long facility = LOG_USER; |
| 42 | if (!getargs(args, "(Sll);ident string, logoption, facility", &ident_o, &logopt, &facility)) |
| 43 | if (!getargs(args, "(Sl);ident string, logoption", &ident_o, &logopt)) |
| 44 | if (!getargs(args, "S;ident string", &ident_o)) |
| 45 | return NULL; |
| 46 | INCREF(ident_o); /* This is needed because openlog() does NOT make a copy |
| 47 | and syslog() later uses it.. cannot trash it. */ |
| 48 | ident = getstringvalue(ident_o); |
| 49 | openlog(ident,logopt,facility); |
| 50 | INCREF(None); |
| 51 | return None; |
| 52 | } |
| 53 | |
| 54 | static object * |
| 55 | syslog_syslog(self, args) |
| 56 | object *self; |
| 57 | object *args; |
| 58 | { |
| 59 | int priority = LOG_INFO; |
| 60 | char *message; |
| 61 | |
| 62 | if (!getargs(args,"(is);priority, message string",&priority,&message)) |
| 63 | if (!getargs(args,"s;message string",&message)) |
| 64 | return NULL; |
| 65 | syslog(priority, message); |
| 66 | INCREF(None); |
| 67 | return None; |
| 68 | } |
| 69 | |
| 70 | static object * |
| 71 | syslog_closelog(self, args) |
| 72 | object *self; |
| 73 | object *args; |
| 74 | { |
| 75 | if (!getnoarg(args)) |
| 76 | return NULL; |
| 77 | closelog(); |
| 78 | INCREF(None); |
| 79 | return None; |
| 80 | } |
| 81 | |
| 82 | static object * |
| 83 | syslog_setlogmask(self, args) |
| 84 | object *self; |
| 85 | object *args; |
| 86 | { |
| 87 | long maskpri; |
| 88 | if (!getargs(args,"l;mask for priority",&maskpri)) |
| 89 | return NULL; |
| 90 | setlogmask(maskpri); |
| 91 | INCREF(None); |
| 92 | return None; |
| 93 | } |
| 94 | |
| 95 | static object * |
| 96 | syslog_log_mask(self, args) |
| 97 | object *self; |
| 98 | object *args; |
| 99 | { |
| 100 | long mask; |
| 101 | long pri; |
| 102 | if (!getargs(args,"l",&pri)) |
| 103 | return NULL; |
| 104 | mask = LOG_MASK(pri); |
| 105 | return newintobject(mask); |
| 106 | } |
| 107 | |
| 108 | static object * |
| 109 | syslog_log_upto(self, args) |
| 110 | object *self; |
| 111 | object *args; |
| 112 | { |
| 113 | long mask; |
| 114 | long pri; |
| 115 | if (!getargs(args,"l",&pri)) |
| 116 | return NULL; |
| 117 | mask = LOG_UPTO(pri); |
| 118 | return newintobject(mask); |
| 119 | } |
| 120 | |
| 121 | /* List of functions defined in the module */ |
| 122 | |
| 123 | static struct methodlist syslog_methods[] = { |
| 124 | {"openlog", syslog_openlog}, |
| 125 | {"closelog", syslog_closelog}, |
| 126 | {"syslog", syslog_syslog}, |
| 127 | {"setlogmask", syslog_setlogmask}, |
| 128 | {"LOG_MASK", syslog_log_mask}, |
| 129 | {"LOG_UPTO", syslog_log_upto}, |
| 130 | {NULL, NULL} /* sentinel */ |
| 131 | }; |
| 132 | |
| 133 | /* Initialization function for the module */ |
| 134 | |
| 135 | void |
| 136 | initsyslog() |
| 137 | { |
| 138 | object *m, *d, *x; |
| 139 | |
| 140 | /* Create the module and add the functions */ |
| 141 | m = initmodule("syslog", syslog_methods); |
| 142 | |
| 143 | /* Add some symbolic constants to the module */ |
| 144 | d = getmoduledict(m); |
| 145 | x = newintobject(LOG_EMERG); |
| 146 | dictinsert(d, "LOG_EMERG", x); |
| 147 | x = newintobject(LOG_ALERT); |
| 148 | dictinsert(d, "LOG_ALERT", x); |
| 149 | x = newintobject(LOG_CRIT); |
| 150 | dictinsert(d, "LOG_CRIT", x); |
| 151 | x = newintobject(LOG_ERR); |
| 152 | dictinsert(d, "LOG_ERR", x); |
| 153 | x = newintobject(LOG_WARNING); |
| 154 | dictinsert(d, "LOG_WARNING", x); |
| 155 | x = newintobject(LOG_NOTICE); |
| 156 | dictinsert(d, "LOG_NOTICE", x); |
| 157 | x = newintobject(LOG_INFO); |
| 158 | dictinsert(d, "LOG_INFO", x); |
| 159 | x = newintobject(LOG_DEBUG); |
| 160 | dictinsert(d, "LOG_DEBUG", x); |
| 161 | x = newintobject(LOG_PID); |
| 162 | dictinsert(d, "LOG_PID", x); |
| 163 | x = newintobject(LOG_CONS); |
| 164 | dictinsert(d, "LOG_CONS", x); |
| 165 | x = newintobject(LOG_NDELAY); |
| 166 | dictinsert(d, "LOG_NDELAY", x); |
| 167 | x = newintobject(LOG_NOWAIT); |
| 168 | dictinsert(d, "LOG_NOWAIT", x); |
| 169 | x = newintobject(LOG_KERN); |
| 170 | dictinsert(d, "LOG_KERN", x); |
| 171 | x = newintobject(LOG_USER); |
| 172 | dictinsert(d, "LOG_USER", x); |
| 173 | x = newintobject(LOG_MAIL); |
| 174 | dictinsert(d, "LOG_MAIL", x); |
| 175 | x = newintobject(LOG_DAEMON); |
| 176 | dictinsert(d, "LOG_DAEMON", x); |
| 177 | x = newintobject(LOG_LPR); |
| 178 | dictinsert(d, "LOG_LPR", x); |
| 179 | x = newintobject(LOG_LOCAL0); |
| 180 | dictinsert(d, "LOG_LOCAL0", x); |
| 181 | x = newintobject(LOG_LOCAL1); |
| 182 | dictinsert(d, "LOG_LOCAL1", x); |
| 183 | x = newintobject(LOG_LOCAL2); |
| 184 | dictinsert(d, "LOG_LOCAL2", x); |
| 185 | x = newintobject(LOG_LOCAL3); |
| 186 | dictinsert(d, "LOG_LOCAL3", x); |
| 187 | x = newintobject(LOG_LOCAL4); |
| 188 | dictinsert(d, "LOG_LOCAL4", x); |
| 189 | x = newintobject(LOG_LOCAL5); |
| 190 | dictinsert(d, "LOG_LOCAL5", x); |
| 191 | x = newintobject(LOG_LOCAL6); |
| 192 | dictinsert(d, "LOG_LOCAL6", x); |
| 193 | x = newintobject(LOG_LOCAL7); |
| 194 | dictinsert(d, "LOG_LOCAL7", x); |
| 195 | |
| 196 | /* Check for errors */ |
| 197 | if (err_occurred()) |
| 198 | fatal("can't initialize module syslog"); |
| 199 | } |