Ruby 1.9.2p136(2010-12-25revision30365)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

# ext/syslog/syslog.c

Go to the documentation of this file.

    00001 /*
    00002  * UNIX Syslog extension for Ruby
    00003  * Amos Gouaux, University of Texas at Dallas
    00004  * <amos+ruby@utdallas.edu>
    00005  *
    00006  * $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $
    00007  * $Id: syslog.c 27437 2010-04-22 08:04:13Z nobu $
    00008  */
    00009
    00010 #include "ruby/ruby.h"
    00011 #include "ruby/util.h"
    00012 #include <syslog.h>
    00013
    00014 /* Syslog class */
    00015 static VALUE mSyslog, mSyslogConstants;
    00016 static const char *syslog_ident = NULL;
    00017 static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1;
    00018 static int syslog_opened = 0;
    00019
    00020 /* Package helper routines */
    00021 static void syslog_write(int pri, int argc, VALUE *argv)
    00022 {
    00023     VALUE str;
    00024
    00025     rb_secure(4);
    00026     if (argc < 1) {
    00027         rb_raise(rb_eArgError, "no log message supplied");
    00028     }
    00029
    00030     if (!syslog_opened) {
    00031         rb_raise(rb_eRuntimeError, "must open syslog before write");
    00032     }
    00033
    00034     str = rb_f_sprintf(argc, argv);
    00035
    00036     syslog(pri, "%s", RSTRING_PTR(str));
    00037 }
    00038
    00039 /* Syslog module methods */
    00040 static VALUE mSyslog_close(VALUE self)
    00041 {
    00042     rb_secure(4);
    00043     if (!syslog_opened) {
    00044         rb_raise(rb_eRuntimeError, "syslog not opened");
    00045     }
    00046
    00047     closelog();
    00048
    00049     free((void *)syslog_ident);
    00050     syslog_ident = NULL;
    00051     syslog_options = syslog_facility = syslog_mask = -1;
    00052     syslog_opened = 0;
    00053
    00054     return Qnil;
    00055 }
    00056
    00057 static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
    00058 {
    00059     VALUE ident, opt, fac;
    00060
    00061     if (syslog_opened) {
    00062         rb_raise(rb_eRuntimeError, "syslog already open");
    00063     }
    00064
    00065     rb_scan_args(argc, argv, "03", &ident, &opt, &fac);
    00066
    00067     if (NIL_P(ident)) {
    00068         ident = rb_gv_get("$0");
    00069     }
    00070     SafeStringValue(ident);
    00071     syslog_ident = strdup(RSTRING_PTR(ident));
    00072
    00073     if (NIL_P(opt)) {
    00074         syslog_options = LOG_PID | LOG_CONS;
    00075     } else {
    00076         syslog_options = NUM2INT(opt);
    00077     }
    00078
    00079     if (NIL_P(fac)) {
    00080         syslog_facility = LOG_USER;
    00081     } else {
    00082         syslog_facility = NUM2INT(fac);
    00083     }
    00084
    00085     openlog(syslog_ident, syslog_options, syslog_facility);
    00086
    00087     syslog_opened = 1;
    00088
    00089     setlogmask(syslog_mask = setlogmask(0));
    00090
    00091     /* be like File.new.open {...} */
    00092     if (rb_block_given_p()) {
    00093         rb_ensure(rb_yield, self, mSyslog_close, self);
    00094     }
    00095
    00096     return self;
    00097 }
    00098
    00099 static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
    00100 {
    00101     mSyslog_close(self);
    00102
    00103     return mSyslog_open(argc, argv, self);
    00104 }
    00105
    00106 static VALUE mSyslog_isopen(VALUE self)
    00107 {
    00108     return syslog_opened ? Qtrue : Qfalse;
    00109 }
    00110
    00111 static VALUE mSyslog_ident(VALUE self)
    00112 {
    00113     return syslog_opened ? rb_str_new2(syslog_ident) : Qnil;
    00114 }
    00115
    00116 static VALUE mSyslog_options(VALUE self)
    00117 {
    00118     return syslog_opened ? INT2NUM(syslog_options) : Qnil;
    00119 }
    00120
    00121 static VALUE mSyslog_facility(VALUE self)
    00122 {
    00123     return syslog_opened ? INT2NUM(syslog_facility) : Qnil;
    00124 }
    00125
    00126 static VALUE mSyslog_get_mask(VALUE self)
    00127 {
    00128     return syslog_opened ? INT2NUM(syslog_mask) : Qnil;
    00129 }
    00130
    00131 static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
    00132 {
    00133     rb_secure(4);
    00134     if (!syslog_opened) {
    00135         rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
    00136     }
    00137
    00138     setlogmask(syslog_mask = NUM2INT(mask));
    00139
    00140     return mask;
    00141 }
    00142
    00143 static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
    00144 {
    00145     VALUE pri;
    00146
    00147     if (argc < 2) {
    00148         rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", argc);
    00149     }
    00150
    00151     argc--;
    00152     pri = *argv++;
    00153
    00154     if (!FIXNUM_P(pri)) {
    00155       rb_raise(rb_eTypeError, "type mismatch: %s given", rb_class2name(CLASS_OF(pri)));
    00156     }
    00157
    00158     syslog_write(FIX2INT(pri), argc, argv);
    00159
    00160     return self;
    00161 }
    00162
    00163 static VALUE mSyslog_inspect(VALUE self)
    00164 {
    00165     char buf[1024];
    00166
    00167     if (syslog_opened) {
    00168         snprintf(buf, sizeof(buf),
    00169           "<#%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>",
    00170           rb_class2name(self),
    00171           syslog_ident,
    00172           syslog_options,
    00173           syslog_facility,
    00174           syslog_mask);
    00175     } else {
    00176         snprintf(buf, sizeof(buf),
    00177           "<#%s: opened=false>", rb_class2name(self));
    00178     }
    00179
    00180     return rb_str_new2(buf);
    00181 }
    00182
    00183 static VALUE mSyslog_instance(VALUE self)
    00184 {
    00185     return self;
    00186 }
    00187
    00188 #define define_syslog_shortcut_method(pri, name) \
    00189 static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
    00190 { \
    00191     syslog_write(pri, argc, argv); \
    00192 \
    00193     return self; \
    00194 }
    00195
    00196 #ifdef LOG_EMERG
    00197 define_syslog_shortcut_method(LOG_EMERG, emerg)
    00198 #endif
    00199 #ifdef LOG_ALERT
    00200 define_syslog_shortcut_method(LOG_ALERT, alert)
    00201 #endif
    00202 #ifdef LOG_CRIT
    00203 define_syslog_shortcut_method(LOG_CRIT, crit)
    00204 #endif
    00205 #ifdef LOG_ERR
    00206 define_syslog_shortcut_method(LOG_ERR, err)
    00207 #endif
    00208 #ifdef LOG_WARNING
    00209 define_syslog_shortcut_method(LOG_WARNING, warning)
    00210 #endif
    00211 #ifdef LOG_NOTICE
    00212 define_syslog_shortcut_method(LOG_NOTICE, notice)
    00213 #endif
    00214 #ifdef LOG_INFO
    00215 define_syslog_shortcut_method(LOG_INFO, info)
    00216 #endif
    00217 #ifdef LOG_DEBUG
    00218 define_syslog_shortcut_method(LOG_DEBUG, debug)
    00219 #endif
    00220
    00221 static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE pri)
    00222 {
    00223     return INT2FIX(LOG_MASK(NUM2INT(pri)));
    00224 }
    00225
    00226 static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri)
    00227 {
    00228     return INT2FIX(LOG_UPTO(NUM2INT(pri)));
    00229 }
    00230
    00231 /* Init for package syslog */
    00232 void Init_syslog()
    00233 {
    00234     mSyslog = rb_define_module("Syslog");
    00235
    00236     mSyslogConstants = rb_define_module_under(mSyslog, "Constants");
    00237
    00238     rb_include_module(mSyslog, mSyslogConstants);
    00239
    00240     rb_define_module_function(mSyslog, "open", mSyslog_open, -1);
    00241     rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1);
    00242     rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1);
    00243     rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0);
    00244
    00245     rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0);
    00246     rb_define_module_function(mSyslog, "options", mSyslog_options, 0);
    00247     rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0);
    00248
    00249     rb_define_module_function(mSyslog, "log", mSyslog_log, -1);
    00250     rb_define_module_function(mSyslog, "close", mSyslog_close, 0);
    00251     rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0);
    00252     rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1);
    00253
    00254     rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
    00255     rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
    00256
    00257     rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0);
    00258     rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0);
    00259
    00260     rb_define_module_function(mSyslogConstants, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
    00261     rb_define_module_function(mSyslogConstants, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
    00262
    00263 #define rb_define_syslog_const(id) \
    00264     rb_define_const(mSyslogConstants, #id, INT2NUM(id))
    00265
    00266     /* Various options when opening log */
    00267 #ifdef LOG_PID
    00268     rb_define_syslog_const(LOG_PID);
    00269 #endif
    00270 #ifdef LOG_CONS
    00271     rb_define_syslog_const(LOG_CONS);
    00272 #endif
    00273 #ifdef LOG_ODELAY
    00274     rb_define_syslog_const(LOG_ODELAY); /* deprecated */
    00275 #endif
    00276 #ifdef LOG_NDELAY
    00277     rb_define_syslog_const(LOG_NDELAY);
    00278 #endif
    00279 #ifdef LOG_NOWAIT
    00280     rb_define_syslog_const(LOG_NOWAIT); /* deprecated */
    00281 #endif
    00282 #ifdef LOG_PERROR
    00283     rb_define_syslog_const(LOG_PERROR);
    00284 #endif
    00285
    00286     /* Various syslog facilities */
    00287 #ifdef LOG_AUTH
    00288     rb_define_syslog_const(LOG_AUTH);
    00289 #endif
    00290 #ifdef LOG_AUTHPRIV
    00291     rb_define_syslog_const(LOG_AUTHPRIV);
    00292 #endif
    00293 #ifdef LOG_CONSOLE
    00294     rb_define_syslog_const(LOG_CONSOLE);
    00295 #endif
    00296 #ifdef LOG_CRON
    00297     rb_define_syslog_const(LOG_CRON);
    00298 #endif
    00299 #ifdef LOG_DAEMON
    00300     rb_define_syslog_const(LOG_DAEMON);
    00301 #endif
    00302 #ifdef LOG_FTP
    00303     rb_define_syslog_const(LOG_FTP);
    00304 #endif
    00305 #ifdef LOG_KERN
    00306     rb_define_syslog_const(LOG_KERN);
    00307 #endif
    00308 #ifdef LOG_LPR
    00309     rb_define_syslog_const(LOG_LPR);
    00310 #endif
    00311 #ifdef LOG_MAIL
    00312     rb_define_syslog_const(LOG_MAIL);
    00313 #endif
    00314 #ifdef LOG_NEWS
    00315     rb_define_syslog_const(LOG_NEWS);
    00316 #endif
    00317 #ifdef LOG_NTP
    00318    rb_define_syslog_const(LOG_NTP);
    00319 #endif
    00320 #ifdef LOG_SECURITY
    00321     rb_define_syslog_const(LOG_SECURITY);
    00322 #endif
    00323 #ifdef LOG_SYSLOG
    00324     rb_define_syslog_const(LOG_SYSLOG);
    00325 #endif
    00326 #ifdef LOG_USER
    00327     rb_define_syslog_const(LOG_USER);
    00328 #endif
    00329 #ifdef LOG_UUCP
    00330     rb_define_syslog_const(LOG_UUCP);
    00331 #endif
    00332 #ifdef LOG_LOCAL0
    00333     rb_define_syslog_const(LOG_LOCAL0);
    00334 #endif
    00335 #ifdef LOG_LOCAL1
    00336     rb_define_syslog_const(LOG_LOCAL1);
    00337 #endif
    00338 #ifdef LOG_LOCAL2
    00339     rb_define_syslog_const(LOG_LOCAL2);
    00340 #endif
    00341 #ifdef LOG_LOCAL3
    00342     rb_define_syslog_const(LOG_LOCAL3);
    00343 #endif
    00344 #ifdef LOG_LOCAL4
    00345     rb_define_syslog_const(LOG_LOCAL4);
    00346 #endif
    00347 #ifdef LOG_LOCAL5
    00348     rb_define_syslog_const(LOG_LOCAL5);
    00349 #endif
    00350 #ifdef LOG_LOCAL6
    00351     rb_define_syslog_const(LOG_LOCAL6);
    00352 #endif
    00353 #ifdef LOG_LOCAL7
    00354     rb_define_syslog_const(LOG_LOCAL7);
    00355 #endif
    00356
    00357 #define rb_define_syslog_shortcut(name) \
    00358     rb_define_module_function(mSyslog, #name, mSyslog_##name, -1)
    00359
    00360     /* Various syslog priorities and the shortcut methods */
    00361 #ifdef LOG_EMERG
    00362     rb_define_syslog_const(LOG_EMERG);
    00363     rb_define_syslog_shortcut(emerg);
    00364 #endif
    00365 #ifdef LOG_ALERT
    00366     rb_define_syslog_const(LOG_ALERT);
    00367     rb_define_syslog_shortcut(alert);
    00368 #endif
    00369 #ifdef LOG_CRIT
    00370     rb_define_syslog_const(LOG_CRIT);
    00371     rb_define_syslog_shortcut(crit);
    00372 #endif
    00373 #ifdef LOG_ERR
    00374     rb_define_syslog_const(LOG_ERR);
    00375     rb_define_syslog_shortcut(err);
    00376 #endif
    00377 #ifdef LOG_WARNING
    00378     rb_define_syslog_const(LOG_WARNING);
    00379     rb_define_syslog_shortcut(warning);
    00380 #endif
    00381 #ifdef LOG_NOTICE
    00382     rb_define_syslog_const(LOG_NOTICE);
    00383     rb_define_syslog_shortcut(notice);
    00384 #endif
    00385 #ifdef LOG_INFO
    00386     rb_define_syslog_const(LOG_INFO);
    00387     rb_define_syslog_shortcut(info);
    00388 #endif
    00389 #ifdef LOG_DEBUG
    00390     rb_define_syslog_const(LOG_DEBUG);
    00391     rb_define_syslog_shortcut(debug);
    00392 #endif
    00393 }
    00394