|
Ruby 1.9.3p194(2012-04-20revision35410)
|
00001 /* 00002 * UNIX Syslog extension for Ruby 00003 * Amos Gouaux, University of Texas at Dallas 00004 * <amos+ruby@utdallas.edu> 00005 * Documented by mathew <meta@pobox.com> 00006 * 00007 * $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $ 00008 * $Id: syslog.c 31234 2011-04-03 13:58:32Z nagachika $ 00009 */ 00010 00011 #include "ruby/ruby.h" 00012 #include "ruby/util.h" 00013 #include <syslog.h> 00014 00015 /* Syslog class */ 00016 static VALUE mSyslog, mSyslogConstants; 00017 static const char *syslog_ident = NULL; 00018 static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1; 00019 static int syslog_opened = 0; 00020 00021 /* Package helper routines */ 00022 static void syslog_write(int pri, int argc, VALUE *argv) 00023 { 00024 VALUE str; 00025 00026 rb_secure(4); 00027 if (argc < 1) { 00028 rb_raise(rb_eArgError, "no log message supplied"); 00029 } 00030 00031 if (!syslog_opened) { 00032 rb_raise(rb_eRuntimeError, "must open syslog before write"); 00033 } 00034 00035 str = rb_f_sprintf(argc, argv); 00036 00037 syslog(pri, "%s", RSTRING_PTR(str)); 00038 } 00039 00040 /* Closes the syslog facility. 00041 * Raises a runtime exception if it is not open. 00042 */ 00043 static VALUE mSyslog_close(VALUE self) 00044 { 00045 rb_secure(4); 00046 if (!syslog_opened) { 00047 rb_raise(rb_eRuntimeError, "syslog not opened"); 00048 } 00049 00050 closelog(); 00051 00052 free((void *)syslog_ident); 00053 syslog_ident = NULL; 00054 syslog_options = syslog_facility = syslog_mask = -1; 00055 syslog_opened = 0; 00056 00057 return Qnil; 00058 } 00059 00060 /* call-seq: 00061 * open(ident, options, facility) => syslog 00062 * 00063 * :yields: syslog 00064 * 00065 * Open the syslog facility. 00066 * Raises a runtime exception if it is already open. 00067 * 00068 * Can be called with or without a code block. If called with a block, the 00069 * Syslog object created is passed to the block. 00070 * 00071 * If the syslog is already open, raises a RuntimeError. 00072 * 00073 * +ident+ is a String which identifies the calling program. 00074 * 00075 * +options+ is the logical OR of any of the following: 00076 * 00077 * LOG_CONS:: If there is an error while sending to the system logger, 00078 * write directly to the console instead. 00079 * 00080 * LOG_NDELAY:: Open the connection now, rather than waiting for the first 00081 * message to be written. 00082 * 00083 * LOG_NOWAIT:: Don't wait for any child processes created while logging 00084 * messages. (Has no effect on Linux.) 00085 * 00086 * LOG_ODELAY:: Opposite of LOG_NDELAY; wait until a message is sent before 00087 * opening the connection. (This is the default.) 00088 * 00089 * LOG_PERROR:: Print the message to stderr as well as sending it to syslog. 00090 * (Not in POSIX.1-2001.) 00091 * 00092 * LOG_PID:: Include the current process ID with each message. 00093 * 00094 * +facility+ describes the type of program opening the syslog, and is 00095 * the logical OR of any of the following which are defined for the host OS: 00096 * 00097 * LOG_AUTH:: Security or authorization. Deprecated, use LOG_AUTHPRIV 00098 * instead. 00099 * 00100 * LOG_AUTHPRIV:: Security or authorization messages which should be kept 00101 * private. 00102 * 00103 * LOG_CONSOLE:: System console message. 00104 * 00105 * LOG_CRON:: System task scheduler (cron or at). 00106 * 00107 * LOG_DAEMON:: A system daemon which has no facility value of its own. 00108 * 00109 * LOG_FTP:: An FTP server. 00110 * 00111 * LOG_KERN:: A kernel message (not sendable by user processes, so not of 00112 * much use to Ruby, but listed here for completeness). 00113 * 00114 * LOG_LRP:: Line printer subsystem. 00115 * 00116 * LOG_MAIL:: Mail delivery or transport subsystem. 00117 * 00118 * LOG_NEWS:: Usenet news system. 00119 * 00120 * LOG_NTP:: Network Time Protocol server. 00121 * 00122 * LOG_SECURITY:: General security message. 00123 * 00124 * LOG_SYSLOG:: Messages generated internally by syslog. 00125 * 00126 * LOG_USER:: Generic user-level message. 00127 * 00128 * LOG_UUCP:: UUCP subsystem. 00129 * 00130 * LOG_LOCAL0 to LOG_LOCAL7:: Locally-defined facilities. 00131 * 00132 * Example: 00133 * 00134 * Syslog.open("webrick", Syslog::LOG_PID, 00135 * Syslog::LOG_DAEMON | Syslog::LOG_LOCAL3) 00136 * 00137 */ 00138 static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self) 00139 { 00140 VALUE ident, opt, fac; 00141 00142 if (syslog_opened) { 00143 rb_raise(rb_eRuntimeError, "syslog already open"); 00144 } 00145 00146 rb_scan_args(argc, argv, "03", &ident, &opt, &fac); 00147 00148 if (NIL_P(ident)) { 00149 ident = rb_gv_get("$0"); 00150 } 00151 SafeStringValue(ident); 00152 syslog_ident = strdup(RSTRING_PTR(ident)); 00153 00154 if (NIL_P(opt)) { 00155 syslog_options = LOG_PID | LOG_CONS; 00156 } else { 00157 syslog_options = NUM2INT(opt); 00158 } 00159 00160 if (NIL_P(fac)) { 00161 syslog_facility = LOG_USER; 00162 } else { 00163 syslog_facility = NUM2INT(fac); 00164 } 00165 00166 openlog(syslog_ident, syslog_options, syslog_facility); 00167 00168 syslog_opened = 1; 00169 00170 setlogmask(syslog_mask = setlogmask(0)); 00171 00172 /* be like File.new.open {...} */ 00173 if (rb_block_given_p()) { 00174 rb_ensure(rb_yield, self, mSyslog_close, self); 00175 } 00176 00177 return self; 00178 } 00179 00180 /* call-seq: 00181 * reopen(ident, options, facility) => syslog 00182 * 00183 * :yields: syslog 00184 * 00185 * Closes and then reopens the syslog. 00186 * 00187 * Arguments are the same as for open(). 00188 */ 00189 static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self) 00190 { 00191 mSyslog_close(self); 00192 00193 return mSyslog_open(argc, argv, self); 00194 } 00195 00196 /* call-seq: 00197 * opened? 00198 * 00199 * Returns true if the syslog is open. 00200 */ 00201 static VALUE mSyslog_isopen(VALUE self) 00202 { 00203 return syslog_opened ? Qtrue : Qfalse; 00204 } 00205 00206 /* Returns the identity string used in the last call to open() 00207 */ 00208 static VALUE mSyslog_ident(VALUE self) 00209 { 00210 return syslog_opened ? rb_str_new2(syslog_ident) : Qnil; 00211 } 00212 00213 /* Returns the options bitmask used in the last call to open() 00214 */ 00215 static VALUE mSyslog_options(VALUE self) 00216 { 00217 return syslog_opened ? INT2NUM(syslog_options) : Qnil; 00218 } 00219 00220 /* Returns the facility number used in the last call to open() 00221 */ 00222 static VALUE mSyslog_facility(VALUE self) 00223 { 00224 return syslog_opened ? INT2NUM(syslog_facility) : Qnil; 00225 } 00226 00227 /* Returns the log priority mask in effect. The mask is not reset by opening 00228 * or closing syslog. 00229 */ 00230 static VALUE mSyslog_get_mask(VALUE self) 00231 { 00232 return syslog_opened ? INT2NUM(syslog_mask) : Qnil; 00233 } 00234 00235 /* call-seq: 00236 * mask=(priority_mask) 00237 * 00238 * Sets the log priority mask. A method LOG_UPTO is defined to make it easier 00239 * to set mask values. Example: 00240 * 00241 * Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR) 00242 * 00243 * Alternatively, specific priorities can be selected and added together using 00244 * binary OR. Example: 00245 * 00246 * Syslog.mask = Syslog::LOG_MASK(Syslog::LOG_ERR) | Syslog::LOG_MASK(Syslog::LOG_CRIT) 00247 * 00248 * The priority mask persists through calls to open() and close(). 00249 */ 00250 static VALUE mSyslog_set_mask(VALUE self, VALUE mask) 00251 { 00252 rb_secure(4); 00253 if (!syslog_opened) { 00254 rb_raise(rb_eRuntimeError, "must open syslog before setting log mask"); 00255 } 00256 00257 setlogmask(syslog_mask = NUM2INT(mask)); 00258 00259 return mask; 00260 } 00261 00262 /* call-seq: 00263 * log(priority, format_string, *format_args) 00264 * 00265 * Log a message with the specified priority. Example: 00266 * 00267 * Syslog.log(Syslog::LOG_CRIT, "Out of disk space") 00268 * Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER']) 00269 * 00270 * The priority levels, in descending order, are: 00271 * 00272 * LOG_EMERG:: System is unusable 00273 * LOG_ALERT:: Action needs to be taken immediately 00274 * LOG_CRIT:: A critical condition has occurred 00275 * LOG_ERR:: An error occurred 00276 * LOG_WARNING:: Warning of a possible problem 00277 * LOG_NOTICE:: A normal but significant condition occurred 00278 * LOG_INFO:: Informational message 00279 * LOG_DEBUG:: Debugging information 00280 * 00281 * Each priority level also has a shortcut method that logs with it's named priority. 00282 * As an example, the two following statements would produce the same result: 00283 * 00284 * Syslog.log(Syslog::LOG_ALERT, "Out of memory") 00285 * Syslog.alert("Out of memory") 00286 * 00287 * Format strings are as for printf/sprintf, except that in addition %m is 00288 * replaced with the error message string that would be returned by 00289 * strerror(errno). 00290 * 00291 */ 00292 static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self) 00293 { 00294 VALUE pri; 00295 00296 if (argc < 2) { 00297 rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", argc); 00298 } 00299 00300 argc--; 00301 pri = *argv++; 00302 00303 if (!FIXNUM_P(pri)) { 00304 rb_raise(rb_eTypeError, "type mismatch: %s given", rb_class2name(CLASS_OF(pri))); 00305 } 00306 00307 syslog_write(FIX2INT(pri), argc, argv); 00308 00309 return self; 00310 } 00311 00312 /* Returns an inspect() string summarizing the object state. 00313 */ 00314 static VALUE mSyslog_inspect(VALUE self) 00315 { 00316 char buf[1024]; 00317 00318 if (syslog_opened) { 00319 snprintf(buf, sizeof(buf), 00320 "<#%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>", 00321 rb_class2name(self), 00322 syslog_ident, 00323 syslog_options, 00324 syslog_facility, 00325 syslog_mask); 00326 } else { 00327 snprintf(buf, sizeof(buf), 00328 "<#%s: opened=false>", rb_class2name(self)); 00329 } 00330 00331 return rb_str_new2(buf); 00332 } 00333 00334 /* Returns self, for backward compatibility. 00335 */ 00336 static VALUE mSyslog_instance(VALUE self) 00337 { 00338 return self; 00339 } 00340 00341 #define define_syslog_shortcut_method(pri, name) \ 00342 static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \ 00343 { \ 00344 syslog_write((pri), argc, argv); \ 00345 \ 00346 return self; \ 00347 } 00348 00349 #ifdef LOG_EMERG 00350 define_syslog_shortcut_method(LOG_EMERG, emerg) 00351 #endif 00352 #ifdef LOG_ALERT 00353 define_syslog_shortcut_method(LOG_ALERT, alert) 00354 #endif 00355 #ifdef LOG_CRIT 00356 define_syslog_shortcut_method(LOG_CRIT, crit) 00357 #endif 00358 #ifdef LOG_ERR 00359 define_syslog_shortcut_method(LOG_ERR, err) 00360 #endif 00361 #ifdef LOG_WARNING 00362 define_syslog_shortcut_method(LOG_WARNING, warning) 00363 #endif 00364 #ifdef LOG_NOTICE 00365 define_syslog_shortcut_method(LOG_NOTICE, notice) 00366 #endif 00367 #ifdef LOG_INFO 00368 define_syslog_shortcut_method(LOG_INFO, info) 00369 #endif 00370 #ifdef LOG_DEBUG 00371 define_syslog_shortcut_method(LOG_DEBUG, debug) 00372 #endif 00373 00374 /* call-seq: 00375 * LOG_MASK(priority_level) => priority_mask 00376 * 00377 * Generates a mask bit for a priority level. See #mask= 00378 */ 00379 static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE pri) 00380 { 00381 return INT2FIX(LOG_MASK(NUM2INT(pri))); 00382 } 00383 00384 /* call-seq: 00385 * LOG_UPTO(priority_level) => priority_mask 00386 * 00387 * Generates a mask value for priority levels at or below the level specified. 00388 * See #mask= 00389 */ 00390 static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri) 00391 { 00392 return INT2FIX(LOG_UPTO(NUM2INT(pri))); 00393 } 00394 00395 /* The syslog package provides a Ruby interface to the POSIX system logging 00396 * facility. 00397 * 00398 * Syslog messages are typically passed to a central logging daemon. 00399 * The daemon may filter them; route them into different files (usually 00400 * found under /var/log); place them in SQL databases; forward 00401 * them to centralized logging servers via TCP or UDP; or even alert the 00402 * system administrator via email, pager or text message. 00403 * 00404 * Unlike application-level logging via Logger or Log4r, syslog is designed 00405 * to allow secure tamper-proof logging. 00406 * 00407 * The syslog protocol is standardized in RFC 5424. 00408 */ 00409 void Init_syslog() 00410 { 00411 mSyslog = rb_define_module("Syslog"); 00412 00413 /* Document-module: Syslog::Constants 00414 * 00415 * Module holding Syslog constants. See Syslog::log and Syslog::open for 00416 * constant descriptions. 00417 */ 00418 mSyslogConstants = rb_define_module_under(mSyslog, "Constants"); 00419 00420 rb_include_module(mSyslog, mSyslogConstants); 00421 00422 rb_define_module_function(mSyslog, "open", mSyslog_open, -1); 00423 rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1); 00424 rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1); 00425 rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0); 00426 00427 rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0); 00428 rb_define_module_function(mSyslog, "options", mSyslog_options, 0); 00429 rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0); 00430 00431 rb_define_module_function(mSyslog, "log", mSyslog_log, -1); 00432 rb_define_module_function(mSyslog, "close", mSyslog_close, 0); 00433 rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0); 00434 rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1); 00435 00436 rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1); 00437 rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1); 00438 00439 rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0); 00440 rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0); 00441 00442 rb_define_module_function(mSyslogConstants, "LOG_MASK", mSyslogConstants_LOG_MASK, 1); 00443 rb_define_module_function(mSyslogConstants, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1); 00444 00445 #define rb_define_syslog_const(id) \ 00446 rb_define_const(mSyslogConstants, #id, INT2NUM(id)) 00447 00448 /* Various options when opening log */ 00449 #ifdef LOG_PID 00450 rb_define_syslog_const(LOG_PID); 00451 #endif 00452 #ifdef LOG_CONS 00453 rb_define_syslog_const(LOG_CONS); 00454 #endif 00455 #ifdef LOG_ODELAY 00456 rb_define_syslog_const(LOG_ODELAY); /* deprecated */ 00457 #endif 00458 #ifdef LOG_NDELAY 00459 rb_define_syslog_const(LOG_NDELAY); 00460 #endif 00461 #ifdef LOG_NOWAIT 00462 rb_define_syslog_const(LOG_NOWAIT); /* deprecated */ 00463 #endif 00464 #ifdef LOG_PERROR 00465 rb_define_syslog_const(LOG_PERROR); 00466 #endif 00467 00468 /* Various syslog facilities */ 00469 #ifdef LOG_AUTH 00470 rb_define_syslog_const(LOG_AUTH); 00471 #endif 00472 #ifdef LOG_AUTHPRIV 00473 rb_define_syslog_const(LOG_AUTHPRIV); 00474 #endif 00475 #ifdef LOG_CONSOLE 00476 rb_define_syslog_const(LOG_CONSOLE); 00477 #endif 00478 #ifdef LOG_CRON 00479 rb_define_syslog_const(LOG_CRON); 00480 #endif 00481 #ifdef LOG_DAEMON 00482 rb_define_syslog_const(LOG_DAEMON); 00483 #endif 00484 #ifdef LOG_FTP 00485 rb_define_syslog_const(LOG_FTP); 00486 #endif 00487 #ifdef LOG_KERN 00488 rb_define_syslog_const(LOG_KERN); 00489 #endif 00490 #ifdef LOG_LPR 00491 rb_define_syslog_const(LOG_LPR); 00492 #endif 00493 #ifdef LOG_MAIL 00494 rb_define_syslog_const(LOG_MAIL); 00495 #endif 00496 #ifdef LOG_NEWS 00497 rb_define_syslog_const(LOG_NEWS); 00498 #endif 00499 #ifdef LOG_NTP 00500 rb_define_syslog_const(LOG_NTP); 00501 #endif 00502 #ifdef LOG_SECURITY 00503 rb_define_syslog_const(LOG_SECURITY); 00504 #endif 00505 #ifdef LOG_SYSLOG 00506 rb_define_syslog_const(LOG_SYSLOG); 00507 #endif 00508 #ifdef LOG_USER 00509 rb_define_syslog_const(LOG_USER); 00510 #endif 00511 #ifdef LOG_UUCP 00512 rb_define_syslog_const(LOG_UUCP); 00513 #endif 00514 #ifdef LOG_LOCAL0 00515 rb_define_syslog_const(LOG_LOCAL0); 00516 #endif 00517 #ifdef LOG_LOCAL1 00518 rb_define_syslog_const(LOG_LOCAL1); 00519 #endif 00520 #ifdef LOG_LOCAL2 00521 rb_define_syslog_const(LOG_LOCAL2); 00522 #endif 00523 #ifdef LOG_LOCAL3 00524 rb_define_syslog_const(LOG_LOCAL3); 00525 #endif 00526 #ifdef LOG_LOCAL4 00527 rb_define_syslog_const(LOG_LOCAL4); 00528 #endif 00529 #ifdef LOG_LOCAL5 00530 rb_define_syslog_const(LOG_LOCAL5); 00531 #endif 00532 #ifdef LOG_LOCAL6 00533 rb_define_syslog_const(LOG_LOCAL6); 00534 #endif 00535 #ifdef LOG_LOCAL7 00536 rb_define_syslog_const(LOG_LOCAL7); 00537 #endif 00538 00539 #define rb_define_syslog_shortcut(name) \ 00540 rb_define_module_function(mSyslog, #name, mSyslog_##name, -1) 00541 00542 /* Various syslog priorities and the shortcut methods */ 00543 #ifdef LOG_EMERG 00544 rb_define_syslog_const(LOG_EMERG); 00545 rb_define_syslog_shortcut(emerg); 00546 #endif 00547 #ifdef LOG_ALERT 00548 rb_define_syslog_const(LOG_ALERT); 00549 rb_define_syslog_shortcut(alert); 00550 #endif 00551 #ifdef LOG_CRIT 00552 rb_define_syslog_const(LOG_CRIT); 00553 rb_define_syslog_shortcut(crit); 00554 #endif 00555 #ifdef LOG_ERR 00556 rb_define_syslog_const(LOG_ERR); 00557 rb_define_syslog_shortcut(err); 00558 #endif 00559 #ifdef LOG_WARNING 00560 rb_define_syslog_const(LOG_WARNING); 00561 rb_define_syslog_shortcut(warning); 00562 #endif 00563 #ifdef LOG_NOTICE 00564 rb_define_syslog_const(LOG_NOTICE); 00565 rb_define_syslog_shortcut(notice); 00566 #endif 00567 #ifdef LOG_INFO 00568 rb_define_syslog_const(LOG_INFO); 00569 rb_define_syslog_shortcut(info); 00570 #endif 00571 #ifdef LOG_DEBUG 00572 rb_define_syslog_const(LOG_DEBUG); 00573 rb_define_syslog_shortcut(debug); 00574 #endif 00575 } 00576
1.7.3