Software /
code /
prosody
Comparison
util-src/pposix.c @ 722:63456c9d0522
mod_posix: Support for logging to syslog (log = 'syslog' in config)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 15 Jan 2009 20:06:41 +0000 |
parent | 588:e743cb742ca6 |
child | 723:c1e7d280c174 |
comparison
equal
deleted
inserted
replaced
721:51233a8ae3d4 | 722:63456c9d0522 |
---|---|
26 #include <libgen.h> | 26 #include <libgen.h> |
27 #include <sys/types.h> | 27 #include <sys/types.h> |
28 #include <sys/stat.h> | 28 #include <sys/stat.h> |
29 #include <fcntl.h> | 29 #include <fcntl.h> |
30 | 30 |
31 #include <syslog.h> | |
32 | |
33 #include <string.h> | |
34 | |
31 #include "lua.h" | 35 #include "lua.h" |
36 #include "lauxlib.h" | |
37 | |
38 /* Daemonization support */ | |
32 | 39 |
33 static int daemonize(lua_State *L) | 40 static int daemonize(lua_State *L) |
34 { | 41 { |
35 | 42 |
36 pid_t pid; | 43 pid_t pid; |
81 lua_pushboolean(L, 1); | 88 lua_pushboolean(L, 1); |
82 lua_pushnil(L); | 89 lua_pushnil(L); |
83 return 2; | 90 return 2; |
84 } | 91 } |
85 | 92 |
93 /* Syslog support */ | |
94 | |
95 char *facility_strings[] = { "auth", | |
96 "authpriv", | |
97 "cron", | |
98 "daemon", | |
99 "ftp", | |
100 "kern", | |
101 "local0", | |
102 "local1", | |
103 "local2", | |
104 "local3", | |
105 "local4", | |
106 "local5", | |
107 "local6", | |
108 "local7", | |
109 "lpr", | |
110 "mail", | |
111 "syslog", | |
112 "user", | |
113 "uucp", | |
114 NULL | |
115 }; | |
116 int facility_constants[] = { | |
117 LOG_AUTH, | |
118 LOG_AUTHPRIV, | |
119 LOG_CRON, | |
120 LOG_DAEMON, | |
121 LOG_FTP, | |
122 LOG_KERN, | |
123 LOG_LOCAL0, | |
124 LOG_LOCAL1, | |
125 LOG_LOCAL2, | |
126 LOG_LOCAL3, | |
127 LOG_LOCAL4, | |
128 LOG_LOCAL5, | |
129 LOG_LOCAL6, | |
130 LOG_LOCAL7, | |
131 LOG_LPR, | |
132 LOG_MAIL, | |
133 LOG_NEWS, | |
134 LOG_SYSLOG, | |
135 LOG_USER, | |
136 LOG_UUCP, | |
137 -1 | |
138 }; | |
139 | |
140 /* " | |
141 The parameter ident in the call of openlog() is probably stored as-is. | |
142 Thus, if the string it points to is changed, syslog() may start | |
143 prepending the changed string, and if the string it points to ceases to | |
144 exist, the results are undefined. Most portable is to use a string | |
145 constant. | |
146 " -- syslog manpage | |
147 */ | |
148 char* syslog_ident = NULL; | |
149 | |
150 int syslog_open(lua_State* L) | |
151 { | |
152 int facility = luaL_checkoption(L, 2, "daemon", &facility_strings); | |
153 facility = facility_constants[facility]; | |
154 | |
155 luaL_checkstring(L, 1); | |
156 | |
157 if(syslog_ident) | |
158 free(syslog_ident); | |
159 | |
160 syslog_ident = strdup(lua_tostring(L, 1)); | |
161 | |
162 openlog(syslog_ident, LOG_PID, facility); | |
163 return 0; | |
164 } | |
165 | |
166 char *level_strings[] = { | |
167 "debug", | |
168 "info", | |
169 "notice", | |
170 "warn", | |
171 "error", | |
172 NULL | |
173 }; | |
174 int level_constants[] = { | |
175 LOG_DEBUG, | |
176 LOG_INFO, | |
177 LOG_NOTICE, | |
178 LOG_WARNING, | |
179 LOG_EMERG, | |
180 -1 | |
181 }; | |
182 int syslog_log(lua_State* L) | |
183 { | |
184 int level = luaL_checkoption(L, 1, "notice", &level_strings); | |
185 level = level_constants[level]; | |
186 | |
187 luaL_checkstring(L, 2); | |
188 | |
189 syslog(level, "%s", lua_tostring(L, 2)); | |
190 return 0; | |
191 } | |
192 | |
193 int syslog_close(lua_State* L) | |
194 { | |
195 closelog(); | |
196 if(syslog_ident) | |
197 { | |
198 free(syslog_ident); | |
199 syslog_ident = NULL; | |
200 } | |
201 return 0; | |
202 } | |
203 | |
86 int luaopen_util_pposix(lua_State *L) | 204 int luaopen_util_pposix(lua_State *L) |
87 { | 205 { |
88 lua_newtable(L); | 206 lua_newtable(L); |
207 | |
89 lua_pushcfunction(L, daemonize); | 208 lua_pushcfunction(L, daemonize); |
90 lua_setfield(L, -2, "daemonize"); | 209 lua_setfield(L, -2, "daemonize"); |
210 | |
211 lua_pushcfunction(L, syslog_open); | |
212 lua_setfield(L, -2, "syslog_open"); | |
213 | |
214 lua_pushcfunction(L, syslog_close); | |
215 lua_setfield(L, -2, "syslog_close"); | |
216 | |
217 lua_pushcfunction(L, syslog_log); | |
218 lua_setfield(L, -2, "syslog_log"); | |
219 | |
91 return 1; | 220 return 1; |
92 }; | 221 }; |