DEFINITIONS
This source file includes following functions.
- syslog_write
- mSyslog_close
- mSyslog_open
- mSyslog_reopen
- mSyslog_isopen
- mSyslog_ident
- mSyslog_options
- mSyslog_facility
- mSyslog_get_mask
- mSyslog_set_mask
- mSyslog_log
- mSyslog_inspect
- mSyslog_instance
- define_syslog_shortcut_method
- mSyslogConstants_LOG_UPTO
- Init_syslog
1
2
3
4
5
6
7
8
9
10 #include "ruby.h"
11 #include <syslog.h>
12
13
14 static VALUE mSyslog, mSyslogConstants;
15 static VALUE syslog_ident = Qnil, syslog_options = INT2FIX(-1),
16 syslog_facility = INT2FIX(-1), syslog_mask = INT2FIX(-1);
17 static int syslog_opened = 0;
18
19
20 static void syslog_write(int pri, int argc, VALUE *argv)
21 {
22 VALUE str;
23
24 if (argc < 1) {
25 rb_raise(rb_eArgError, "no log message supplied");
26 }
27
28 if (!syslog_opened) {
29 rb_raise(rb_eRuntimeError, "must open syslog before write");
30 }
31
32 str = rb_f_sprintf(argc, argv);
33
34 syslog(pri, "%s", RSTRING(str)->ptr);
35 }
36
37
38 static VALUE mSyslog_close(VALUE self)
39 {
40 closelog();
41 syslog_opened = 0;
42
43 return Qnil;
44 }
45
46 static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
47 {
48 VALUE ident, opt, fac;
49 int mask;
50
51 if (syslog_opened) {
52 rb_raise(rb_eRuntimeError, "syslog already open");
53 }
54 rb_scan_args(argc, argv, "03", &ident, &opt, &fac);
55 if (NIL_P(ident)) {
56 ident = rb_gv_get("$0");
57 }
58 if (NIL_P(opt)) {
59 opt = INT2NUM(LOG_PID | LOG_CONS);
60 }
61 if (NIL_P(fac)) {
62 fac = INT2NUM(LOG_USER);
63 }
64
65 #ifdef SafeStringValue
66 SafeStringValue(ident);
67 #else
68 Check_SafeStr(ident);
69 #endif
70 syslog_ident = ident;
71 syslog_options = opt;
72 syslog_facility = fac;
73 openlog(RSTRING(ident)->ptr, NUM2INT(opt), NUM2INT(fac));
74 syslog_opened = 1;
75
76 setlogmask(mask = setlogmask(0));
77 syslog_mask = INT2NUM(mask);
78
79
80 if (rb_block_given_p()) {
81 rb_ensure(rb_yield, self, mSyslog_close, self);
82 }
83
84 return self;
85 }
86
87 static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
88 {
89 mSyslog_close(self);
90
91 return mSyslog_open(argc, argv, self);
92 }
93
94 static VALUE mSyslog_isopen(VALUE self)
95 {
96 return syslog_opened ? Qtrue : Qfalse;
97 }
98
99 static VALUE mSyslog_ident(VALUE self)
100 {
101 return syslog_ident;
102 }
103
104 static VALUE mSyslog_options(VALUE self)
105 {
106 return syslog_options;
107 }
108
109 static VALUE mSyslog_facility(VALUE self)
110 {
111 return syslog_facility;
112 }
113
114 static VALUE mSyslog_get_mask(VALUE self)
115 {
116 return syslog_mask;
117 }
118
119 static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
120 {
121 if (!syslog_opened) {
122 rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
123 }
124
125 setlogmask(NUM2INT(mask));
126 syslog_mask = mask;
127
128 return mask;
129 }
130
131 static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
132 {
133 VALUE pri;
134
135 if (argc < 2) {
136 rb_raise(rb_eArgError, "wrong # of arguments(%d for 2+)", argc);
137 }
138
139 argc--;
140 pri = *argv++;
141
142 if (!FIXNUM_P(pri)) {
143 rb_raise(rb_eTypeError, "type mismatch: %s given", rb_class2name(CLASS_OF(pri)));
144 }
145
146 syslog_write(FIX2INT(pri), argc, argv);
147
148 return self;
149 }
150
151 static VALUE mSyslog_inspect(VALUE self)
152 {
153 #define N 7
154 int argc = N;
155 VALUE argv[N];
156 const char fmt[] =
157 "<#%s: opened=%s, ident=\"%s\", options=%d, facility=%d, mask=%d>";
158
159 argv[0] = rb_str_new(fmt, sizeof(fmt) - 1);
160 argv[1] = mSyslog;
161 argv[2] = syslog_opened ? Qtrue : Qfalse;
162 argv[3] = syslog_ident;
163 argv[4] = syslog_options;
164 argv[5] = syslog_facility;
165 argv[6] = syslog_mask;
166
167 return rb_f_sprintf(argc, argv);
168 #undef N
169 }
170
171 static VALUE mSyslog_instance(VALUE self)
172 {
173 return self;
174 }
175
176 #define define_syslog_shortcut_method(pri, name) \
177 static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
178 { \
179 syslog_write(pri, argc, argv); \
180 \
181 return self; \
182 }
183
184 #ifdef LOG_EMERG
185 define_syslog_shortcut_method(LOG_EMERG, emerg)
186 #endif
187 #ifdef LOG_ALERT
188 define_syslog_shortcut_method(LOG_ALERT, alert)
189 #endif
190 #ifdef LOG_CRIT
191 define_syslog_shortcut_method(LOG_CRIT, crit)
192 #endif
193 #ifdef LOG_ERR
194 define_syslog_shortcut_method(LOG_ERR, err)
195 #endif
196 #ifdef LOG_WARNING
197 define_syslog_shortcut_method(LOG_WARNING, warning)
198 #endif
199 #ifdef LOG_NOTICE
200 define_syslog_shortcut_method(LOG_NOTICE, notice)
201 #endif
202 #ifdef LOG_INFO
203 define_syslog_shortcut_method(LOG_INFO, info)
204 #endif
205 #ifdef LOG_DEBUG
206 define_syslog_shortcut_method(LOG_DEBUG, debug)
207 #endif
208
209 static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE pri)
210 {
211 return INT2FIX(LOG_MASK(FIX2INT(pri)));
212 }
213
214 static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri)
215 {
216 return INT2FIX(LOG_UPTO(FIX2INT(pri)));
217 }
218
219
220 void Init_syslog()
221 {
222 mSyslog = rb_define_module("Syslog");
223
224 mSyslogConstants = rb_define_module_under(mSyslog, "Constants");
225
226 rb_include_module(mSyslog, mSyslogConstants);
227
228 rb_define_module_function(mSyslog, "open", mSyslog_open, -1);
229 rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1);
230 rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1);
231 rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0);
232
233 rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0);
234 rb_define_module_function(mSyslog, "options", mSyslog_options, 0);
235 rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0);
236
237 rb_define_module_function(mSyslog, "log", mSyslog_log, -1);
238 rb_define_module_function(mSyslog, "close", mSyslog_close, 0);
239 rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0);
240 rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1);
241
242 rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
243 rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
244
245 rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0);
246 rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0);
247
248 rb_define_module_function(mSyslogConstants, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
249 rb_define_module_function(mSyslogConstants, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
250
251 #define rb_define_syslog_const(id) \
252 rb_define_const(mSyslogConstants, #id, INT2NUM(id))
253
254
255 #ifdef LOG_PID
256 rb_define_syslog_const(LOG_PID);
257 #endif
258 #ifdef LOG_CONS
259 rb_define_syslog_const(LOG_CONS);
260 #endif
261 #ifdef LOG_ODELAY
262 rb_define_syslog_const(LOG_ODELAY);
263 #endif
264 #ifdef LOG_NDELAY
265 rb_define_syslog_const(LOG_NDELAY);
266 #endif
267 #ifdef LOG_NOWAIT
268 rb_define_syslog_const(LOG_NOWAIT);
269 #endif
270 #ifdef LOG_PERROR
271 rb_define_syslog_const(LOG_PERROR);
272 #endif
273
274
275 #ifdef LOG_AUTH
276 rb_define_syslog_const(LOG_AUTH);
277 #endif
278 #ifdef LOG_AUTHPRIV
279 rb_define_syslog_const(LOG_AUTHPRIV);
280 #endif
281 #ifdef LOG_CONSOLE
282 rb_define_syslog_const(LOG_CONSOLE);
283 #endif
284 #ifdef LOG_CRON
285 rb_define_syslog_const(LOG_CRON);
286 #endif
287 #ifdef LOG_DAEMON
288 rb_define_syslog_const(LOG_DAEMON);
289 #endif
290 #ifdef LOG_FTP
291 rb_define_syslog_const(LOG_FTP);
292 #endif
293 #ifdef LOG_KERN
294 rb_define_syslog_const(LOG_KERN);
295 #endif
296 #ifdef LOG_LPR
297 rb_define_syslog_const(LOG_LPR);
298 #endif
299 #ifdef LOG_MAIL
300 rb_define_syslog_const(LOG_MAIL);
301 #endif
302 #ifdef LOG_NEWS
303 rb_define_syslog_const(LOG_NEWS);
304 #endif
305 #ifdef LOG_NTP
306 rb_define_syslog_const(LOG_NTP);
307 #endif
308 #ifdef LOG_SECURITY
309 rb_define_syslog_const(LOG_SECURITY);
310 #endif
311 #ifdef LOG_SYSLOG
312 rb_define_syslog_const(LOG_SYSLOG);
313 #endif
314 #ifdef LOG_USER
315 rb_define_syslog_const(LOG_USER);
316 #endif
317 #ifdef LOG_UUCP
318 rb_define_syslog_const(LOG_UUCP);
319 #endif
320 #ifdef LOG_LOCAL0
321 rb_define_syslog_const(LOG_LOCAL0);
322 #endif
323 #ifdef LOG_LOCAL1
324 rb_define_syslog_const(LOG_LOCAL1);
325 #endif
326 #ifdef LOG_LOCAL2
327 rb_define_syslog_const(LOG_LOCAL2);
328 #endif
329 #ifdef LOG_LOCAL3
330 rb_define_syslog_const(LOG_LOCAL3);
331 #endif
332 #ifdef LOG_LOCAL4
333 rb_define_syslog_const(LOG_LOCAL4);
334 #endif
335 #ifdef LOG_LOCAL5
336 rb_define_syslog_const(LOG_LOCAL5);
337 #endif
338 #ifdef LOG_LOCAL6
339 rb_define_syslog_const(LOG_LOCAL6);
340 #endif
341 #ifdef LOG_LOCAL7
342 rb_define_syslog_const(LOG_LOCAL7);
343 #endif
344
345 #define rb_define_syslog_shortcut(name) \
346 rb_define_module_function(mSyslog, #name, mSyslog_##name, -1)
347
348
349 #ifdef LOG_EMERG
350 rb_define_syslog_const(LOG_EMERG);
351 rb_define_syslog_shortcut(emerg);
352 #endif
353 #ifdef LOG_ALERT
354 rb_define_syslog_const(LOG_ALERT);
355 rb_define_syslog_shortcut(alert);
356 #endif
357 #ifdef LOG_CRIT
358 rb_define_syslog_const(LOG_CRIT);
359 rb_define_syslog_shortcut(crit);
360 #endif
361 #ifdef LOG_ERR
362 rb_define_syslog_const(LOG_ERR);
363 rb_define_syslog_shortcut(err);
364 #endif
365 #ifdef LOG_WARNING
366 rb_define_syslog_const(LOG_WARNING);
367 rb_define_syslog_shortcut(warning);
368 #endif
369 #ifdef LOG_NOTICE
370 rb_define_syslog_const(LOG_NOTICE);
371 rb_define_syslog_shortcut(notice);
372 #endif
373 #ifdef LOG_INFO
374 rb_define_syslog_const(LOG_INFO);
375 rb_define_syslog_shortcut(info);
376 #endif
377 #ifdef LOG_DEBUG
378 rb_define_syslog_const(LOG_DEBUG);
379 rb_define_syslog_shortcut(debug);
380 #endif
381 }