2010-04-22 12:04:13 +04:00
|
|
|
/*
|
2001-11-26 15:00:40 +03:00
|
|
|
* UNIX Syslog extension for Ruby
|
|
|
|
* Amos Gouaux, University of Texas at Dallas
|
|
|
|
* <amos+ruby@utdallas.edu>
|
2011-02-22 00:13:04 +03:00
|
|
|
* Documented by mathew <meta@pobox.com>
|
2001-11-26 15:00:40 +03:00
|
|
|
*
|
2002-02-25 15:23:47 +03:00
|
|
|
* $RoughId: syslog.c,v 1.21 2002/02/25 12:21:17 knu Exp $
|
2001-11-26 15:00:40 +03:00
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2007-06-10 07:06:15 +04:00
|
|
|
#include "ruby/ruby.h"
|
|
|
|
#include "ruby/util.h"
|
2001-11-26 15:00:40 +03:00
|
|
|
#include <syslog.h>
|
|
|
|
|
|
|
|
/* Syslog class */
|
2012-05-21 11:18:49 +04:00
|
|
|
static VALUE mSyslog;
|
|
|
|
/*
|
|
|
|
* Module holding all Syslog constants. See Syslog::log and
|
|
|
|
* Syslog::open for constant descriptions.
|
|
|
|
*/
|
|
|
|
static VALUE mSyslogConstants;
|
|
|
|
/* Module holding Syslog option constants */
|
|
|
|
static VALUE mSyslogOption;
|
|
|
|
/* Module holding Syslog facility constants */
|
|
|
|
static VALUE mSyslogFacility;
|
|
|
|
/* Module holding Syslog level constants */
|
|
|
|
static VALUE mSyslogLevel;
|
|
|
|
/* Module holding Syslog utility macros */
|
|
|
|
static VALUE mSyslogMacros;
|
|
|
|
|
2002-11-27 10:15:37 +03:00
|
|
|
static const char *syslog_ident = NULL;
|
|
|
|
static int syslog_options = -1, syslog_facility = -1, syslog_mask = -1;
|
2001-11-26 15:00:40 +03:00
|
|
|
static int syslog_opened = 0;
|
|
|
|
|
|
|
|
/* Package helper routines */
|
|
|
|
static void syslog_write(int pri, int argc, VALUE *argv)
|
|
|
|
{
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
rb_raise(rb_eArgError, "no log message supplied");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!syslog_opened) {
|
|
|
|
rb_raise(rb_eRuntimeError, "must open syslog before write");
|
|
|
|
}
|
|
|
|
|
|
|
|
str = rb_f_sprintf(argc, argv);
|
|
|
|
|
2006-08-31 14:30:33 +04:00
|
|
|
syslog(pri, "%s", RSTRING_PTR(str));
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Closes the syslog facility.
|
|
|
|
* Raises a runtime exception if it is not open.
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_close(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2002-11-27 11:44:04 +03:00
|
|
|
if (!syslog_opened) {
|
|
|
|
rb_raise(rb_eRuntimeError, "syslog not opened");
|
|
|
|
}
|
|
|
|
|
2001-11-26 15:00:40 +03:00
|
|
|
closelog();
|
2002-11-27 11:44:04 +03:00
|
|
|
|
2012-01-10 07:49:10 +04:00
|
|
|
xfree((void *)syslog_ident);
|
2002-11-27 11:44:04 +03:00
|
|
|
syslog_ident = NULL;
|
|
|
|
syslog_options = syslog_facility = syslog_mask = -1;
|
2001-11-26 15:00:40 +03:00
|
|
|
syslog_opened = 0;
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* call-seq:
|
|
|
|
* open(ident, options, facility) => syslog
|
|
|
|
*
|
|
|
|
* :yields: syslog
|
|
|
|
*
|
|
|
|
* Open the syslog facility.
|
|
|
|
* Raises a runtime exception if it is already open.
|
|
|
|
*
|
|
|
|
* Can be called with or without a code block. If called with a block, the
|
|
|
|
* Syslog object created is passed to the block.
|
|
|
|
*
|
|
|
|
* If the syslog is already open, raises a RuntimeError.
|
|
|
|
*
|
|
|
|
* +ident+ is a String which identifies the calling program.
|
|
|
|
*
|
|
|
|
* +options+ is the logical OR of any of the following:
|
|
|
|
*
|
|
|
|
* LOG_CONS:: If there is an error while sending to the system logger,
|
|
|
|
* write directly to the console instead.
|
|
|
|
*
|
|
|
|
* LOG_NDELAY:: Open the connection now, rather than waiting for the first
|
|
|
|
* message to be written.
|
|
|
|
*
|
|
|
|
* LOG_NOWAIT:: Don't wait for any child processes created while logging
|
|
|
|
* messages. (Has no effect on Linux.)
|
|
|
|
*
|
|
|
|
* LOG_ODELAY:: Opposite of LOG_NDELAY; wait until a message is sent before
|
|
|
|
* opening the connection. (This is the default.)
|
|
|
|
*
|
|
|
|
* LOG_PERROR:: Print the message to stderr as well as sending it to syslog.
|
|
|
|
* (Not in POSIX.1-2001.)
|
|
|
|
*
|
|
|
|
* LOG_PID:: Include the current process ID with each message.
|
|
|
|
*
|
|
|
|
* +facility+ describes the type of program opening the syslog, and is
|
|
|
|
* the logical OR of any of the following which are defined for the host OS:
|
|
|
|
*
|
|
|
|
* LOG_AUTH:: Security or authorization. Deprecated, use LOG_AUTHPRIV
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* LOG_AUTHPRIV:: Security or authorization messages which should be kept
|
|
|
|
* private.
|
|
|
|
*
|
|
|
|
* LOG_CONSOLE:: System console message.
|
|
|
|
*
|
|
|
|
* LOG_CRON:: System task scheduler (cron or at).
|
|
|
|
*
|
|
|
|
* LOG_DAEMON:: A system daemon which has no facility value of its own.
|
|
|
|
*
|
|
|
|
* LOG_FTP:: An FTP server.
|
|
|
|
*
|
|
|
|
* LOG_KERN:: A kernel message (not sendable by user processes, so not of
|
|
|
|
* much use to Ruby, but listed here for completeness).
|
|
|
|
*
|
2011-12-11 06:44:04 +04:00
|
|
|
* LOG_LPR:: Line printer subsystem.
|
2011-02-22 00:13:04 +03:00
|
|
|
*
|
|
|
|
* LOG_MAIL:: Mail delivery or transport subsystem.
|
|
|
|
*
|
|
|
|
* LOG_NEWS:: Usenet news system.
|
|
|
|
*
|
|
|
|
* LOG_NTP:: Network Time Protocol server.
|
|
|
|
*
|
|
|
|
* LOG_SECURITY:: General security message.
|
|
|
|
*
|
|
|
|
* LOG_SYSLOG:: Messages generated internally by syslog.
|
|
|
|
*
|
|
|
|
* LOG_USER:: Generic user-level message.
|
|
|
|
*
|
|
|
|
* LOG_UUCP:: UUCP subsystem.
|
|
|
|
*
|
|
|
|
* LOG_LOCAL0 to LOG_LOCAL7:: Locally-defined facilities.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* Syslog.open("webrick", Syslog::LOG_PID,
|
|
|
|
* Syslog::LOG_DAEMON | Syslog::LOG_LOCAL3)
|
|
|
|
*
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
|
|
|
VALUE ident, opt, fac;
|
2017-09-30 11:35:23 +03:00
|
|
|
const char *ident_ptr;
|
2001-11-26 15:00:40 +03:00
|
|
|
|
|
|
|
if (syslog_opened) {
|
|
|
|
rb_raise(rb_eRuntimeError, "syslog already open");
|
|
|
|
}
|
2002-11-27 11:44:04 +03:00
|
|
|
|
2001-11-26 15:00:40 +03:00
|
|
|
rb_scan_args(argc, argv, "03", &ident, &opt, &fac);
|
2002-11-27 11:44:04 +03:00
|
|
|
|
2001-11-26 15:00:40 +03:00
|
|
|
if (NIL_P(ident)) {
|
2010-04-22 12:04:13 +04:00
|
|
|
ident = rb_gv_get("$0");
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
2017-09-30 11:35:23 +03:00
|
|
|
ident_ptr = StringValueCStr(ident);
|
|
|
|
syslog_ident = strdup(ident_ptr);
|
2002-11-27 11:44:04 +03:00
|
|
|
|
|
|
|
if (NIL_P(opt)) {
|
|
|
|
syslog_options = LOG_PID | LOG_CONS;
|
|
|
|
} else {
|
|
|
|
syslog_options = NUM2INT(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NIL_P(fac)) {
|
|
|
|
syslog_facility = LOG_USER;
|
|
|
|
} else {
|
|
|
|
syslog_facility = NUM2INT(fac);
|
|
|
|
}
|
|
|
|
|
2002-11-27 10:15:37 +03:00
|
|
|
openlog(syslog_ident, syslog_options, syslog_facility);
|
2002-11-27 11:44:04 +03:00
|
|
|
|
2001-11-26 15:00:40 +03:00
|
|
|
syslog_opened = 1;
|
|
|
|
|
2002-11-27 10:15:37 +03:00
|
|
|
setlogmask(syslog_mask = setlogmask(0));
|
2001-11-26 15:00:40 +03:00
|
|
|
|
|
|
|
/* be like File.new.open {...} */
|
|
|
|
if (rb_block_given_p()) {
|
2002-02-25 15:13:30 +03:00
|
|
|
rb_ensure(rb_yield, self, mSyslog_close, self);
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* call-seq:
|
2011-04-03 17:58:32 +04:00
|
|
|
* reopen(ident, options, facility) => syslog
|
2011-02-22 00:13:04 +03:00
|
|
|
*
|
|
|
|
* :yields: syslog
|
|
|
|
*
|
|
|
|
* Closes and then reopens the syslog.
|
|
|
|
*
|
|
|
|
* Arguments are the same as for open().
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2002-02-25 15:13:30 +03:00
|
|
|
mSyslog_close(self);
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2002-02-25 15:13:30 +03:00
|
|
|
return mSyslog_open(argc, argv, self);
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-04-03 17:58:32 +04:00
|
|
|
/* call-seq:
|
|
|
|
* opened?
|
|
|
|
*
|
2011-02-22 00:13:04 +03:00
|
|
|
* Returns true if the syslog is open.
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_isopen(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
|
|
|
return syslog_opened ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Returns the identity string used in the last call to open()
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_ident(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2002-11-27 11:44:04 +03:00
|
|
|
return syslog_opened ? rb_str_new2(syslog_ident) : Qnil;
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Returns the options bitmask used in the last call to open()
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_options(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2002-11-27 11:44:04 +03:00
|
|
|
return syslog_opened ? INT2NUM(syslog_options) : Qnil;
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Returns the facility number used in the last call to open()
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_facility(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2002-11-27 11:44:04 +03:00
|
|
|
return syslog_opened ? INT2NUM(syslog_facility) : Qnil;
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Returns the log priority mask in effect. The mask is not reset by opening
|
|
|
|
* or closing syslog.
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_get_mask(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2002-11-27 11:44:04 +03:00
|
|
|
return syslog_opened ? INT2NUM(syslog_mask) : Qnil;
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* call-seq:
|
2011-04-03 17:58:32 +04:00
|
|
|
* mask=(priority_mask)
|
2011-02-22 00:13:04 +03:00
|
|
|
*
|
|
|
|
* Sets the log priority mask. A method LOG_UPTO is defined to make it easier
|
|
|
|
* to set mask values. Example:
|
|
|
|
*
|
|
|
|
* Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
|
|
|
|
*
|
|
|
|
* Alternatively, specific priorities can be selected and added together using
|
|
|
|
* binary OR. Example:
|
|
|
|
*
|
|
|
|
* Syslog.mask = Syslog::LOG_MASK(Syslog::LOG_ERR) | Syslog::LOG_MASK(Syslog::LOG_CRIT)
|
|
|
|
*
|
|
|
|
* The priority mask persists through calls to open() and close().
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
|
|
|
if (!syslog_opened) {
|
|
|
|
rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
|
|
|
|
}
|
|
|
|
|
2002-11-27 10:15:37 +03:00
|
|
|
setlogmask(syslog_mask = NUM2INT(mask));
|
2001-11-26 15:00:40 +03:00
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* call-seq:
|
2011-04-03 17:58:32 +04:00
|
|
|
* log(priority, format_string, *format_args)
|
2011-02-22 00:13:04 +03:00
|
|
|
*
|
|
|
|
* Log a message with the specified priority. Example:
|
|
|
|
*
|
2011-04-03 17:58:32 +04:00
|
|
|
* Syslog.log(Syslog::LOG_CRIT, "Out of disk space")
|
|
|
|
* Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER'])
|
2011-02-22 00:13:04 +03:00
|
|
|
*
|
|
|
|
* The priority levels, in descending order, are:
|
|
|
|
*
|
|
|
|
* LOG_EMERG:: System is unusable
|
|
|
|
* LOG_ALERT:: Action needs to be taken immediately
|
|
|
|
* LOG_CRIT:: A critical condition has occurred
|
|
|
|
* LOG_ERR:: An error occurred
|
|
|
|
* LOG_WARNING:: Warning of a possible problem
|
|
|
|
* LOG_NOTICE:: A normal but significant condition occurred
|
|
|
|
* LOG_INFO:: Informational message
|
|
|
|
* LOG_DEBUG:: Debugging information
|
|
|
|
*
|
2011-04-03 17:58:32 +04:00
|
|
|
* Each priority level also has a shortcut method that logs with it's named priority.
|
|
|
|
* As an example, the two following statements would produce the same result:
|
|
|
|
*
|
|
|
|
* Syslog.log(Syslog::LOG_ALERT, "Out of memory")
|
|
|
|
* Syslog.alert("Out of memory")
|
|
|
|
*
|
2011-02-22 00:13:04 +03:00
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
|
|
|
VALUE pri;
|
|
|
|
|
2015-10-25 03:43:06 +03:00
|
|
|
rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS);
|
2001-11-26 15:00:40 +03:00
|
|
|
|
|
|
|
argc--;
|
|
|
|
pri = *argv++;
|
|
|
|
|
|
|
|
if (!FIXNUM_P(pri)) {
|
2014-01-13 04:57:38 +04:00
|
|
|
rb_raise(rb_eTypeError, "type mismatch: %"PRIsVALUE" given", rb_obj_class(pri));
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
syslog_write(FIX2INT(pri), argc, argv);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Returns an inspect() string summarizing the object state.
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_inspect(VALUE self)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2012-05-21 11:18:43 +04:00
|
|
|
Check_Type(self, T_MODULE);
|
|
|
|
|
2012-05-21 11:18:47 +04:00
|
|
|
if (!syslog_opened)
|
2014-01-13 04:57:41 +04:00
|
|
|
return rb_sprintf("<#%"PRIsVALUE": opened=false>", self);
|
2002-11-27 10:15:37 +03:00
|
|
|
|
2014-01-13 04:57:41 +04:00
|
|
|
return rb_sprintf("<#%"PRIsVALUE": opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>",
|
|
|
|
self,
|
2012-05-21 11:18:47 +04:00
|
|
|
syslog_ident,
|
|
|
|
syslog_options,
|
|
|
|
syslog_facility,
|
|
|
|
syslog_mask);
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* Returns self, for backward compatibility.
|
|
|
|
*/
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_instance(VALUE self)
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2001-11-26 15:00:40 +03:00
|
|
|
#define define_syslog_shortcut_method(pri, name) \
|
2002-02-25 15:13:30 +03:00
|
|
|
static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
|
2001-11-26 15:00:40 +03:00
|
|
|
{ \
|
2011-04-01 17:26:26 +04:00
|
|
|
syslog_write((pri), argc, argv); \
|
2001-11-26 15:00:40 +03:00
|
|
|
\
|
|
|
|
return self; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LOG_EMERG
|
|
|
|
define_syslog_shortcut_method(LOG_EMERG, emerg)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_ALERT
|
|
|
|
define_syslog_shortcut_method(LOG_ALERT, alert)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_CRIT
|
|
|
|
define_syslog_shortcut_method(LOG_CRIT, crit)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_ERR
|
|
|
|
define_syslog_shortcut_method(LOG_ERR, err)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_WARNING
|
|
|
|
define_syslog_shortcut_method(LOG_WARNING, warning)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_NOTICE
|
|
|
|
define_syslog_shortcut_method(LOG_NOTICE, notice)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_INFO
|
|
|
|
define_syslog_shortcut_method(LOG_INFO, info)
|
|
|
|
#endif
|
|
|
|
#ifdef LOG_DEBUG
|
|
|
|
define_syslog_shortcut_method(LOG_DEBUG, debug)
|
|
|
|
#endif
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* call-seq:
|
|
|
|
* LOG_MASK(priority_level) => priority_mask
|
|
|
|
*
|
|
|
|
* Generates a mask bit for a priority level. See #mask=
|
|
|
|
*/
|
2012-05-21 11:18:49 +04:00
|
|
|
static VALUE mSyslogMacros_LOG_MASK(VALUE mod, VALUE pri)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2008-08-12 13:35:12 +04:00
|
|
|
return INT2FIX(LOG_MASK(NUM2INT(pri)));
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* call-seq:
|
|
|
|
* LOG_UPTO(priority_level) => priority_mask
|
|
|
|
*
|
|
|
|
* Generates a mask value for priority levels at or below the level specified.
|
|
|
|
* See #mask=
|
|
|
|
*/
|
2012-05-21 11:18:49 +04:00
|
|
|
static VALUE mSyslogMacros_LOG_UPTO(VALUE mod, VALUE pri)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2008-08-12 13:35:12 +04:00
|
|
|
return INT2FIX(LOG_UPTO(NUM2INT(pri)));
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
static VALUE mSyslogMacros_included(VALUE mod, VALUE target)
|
|
|
|
{
|
|
|
|
rb_extend_object(target, mSyslogMacros);
|
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
2011-02-22 00:13:04 +03:00
|
|
|
/* The syslog package provides a Ruby interface to the POSIX system logging
|
|
|
|
* facility.
|
|
|
|
*
|
|
|
|
* Syslog messages are typically passed to a central logging daemon.
|
|
|
|
* The daemon may filter them; route them into different files (usually
|
|
|
|
* found under /var/log); place them in SQL databases; forward
|
|
|
|
* them to centralized logging servers via TCP or UDP; or even alert the
|
|
|
|
* system administrator via email, pager or text message.
|
|
|
|
*
|
|
|
|
* Unlike application-level logging via Logger or Log4r, syslog is designed
|
|
|
|
* to allow secure tamper-proof logging.
|
|
|
|
*
|
|
|
|
* The syslog protocol is standardized in RFC 5424.
|
|
|
|
*/
|
2014-10-05 03:09:32 +04:00
|
|
|
void Init_syslog(void)
|
2001-11-26 15:00:40 +03:00
|
|
|
{
|
2018-02-16 11:39:48 +03:00
|
|
|
#undef rb_intern
|
2002-02-25 15:13:30 +03:00
|
|
|
mSyslog = rb_define_module("Syslog");
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
mSyslogConstants = rb_define_module_under(mSyslog, "Constants");
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
mSyslogOption = rb_define_module_under(mSyslog, "Option");
|
|
|
|
mSyslogFacility = rb_define_module_under(mSyslog, "Facility");
|
|
|
|
mSyslogLevel = rb_define_module_under(mSyslog, "Level");
|
|
|
|
mSyslogMacros = rb_define_module_under(mSyslog, "Macros");
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2002-02-25 15:13:30 +03:00
|
|
|
rb_define_module_function(mSyslog, "open", mSyslog_open, -1);
|
|
|
|
rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1);
|
|
|
|
rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1);
|
|
|
|
rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0);
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2002-02-25 15:13:30 +03:00
|
|
|
rb_define_module_function(mSyslog, "ident", mSyslog_ident, 0);
|
|
|
|
rb_define_module_function(mSyslog, "options", mSyslog_options, 0);
|
|
|
|
rb_define_module_function(mSyslog, "facility", mSyslog_facility, 0);
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2002-02-25 15:13:30 +03:00
|
|
|
rb_define_module_function(mSyslog, "log", mSyslog_log, -1);
|
|
|
|
rb_define_module_function(mSyslog, "close", mSyslog_close, 0);
|
|
|
|
rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0);
|
|
|
|
rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1);
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2013-02-19 08:13:57 +04:00
|
|
|
rb_define_singleton_method(mSyslog, "inspect", mSyslog_inspect, 0);
|
2002-02-25 15:13:30 +03:00
|
|
|
rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0);
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
/* Syslog options */
|
2001-11-26 15:00:40 +03:00
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
#define rb_define_syslog_option(c) \
|
|
|
|
rb_define_const(mSyslogOption, #c, INT2NUM(c))
|
2001-11-26 15:00:40 +03:00
|
|
|
|
|
|
|
#ifdef LOG_PID
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_option(LOG_PID);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_CONS
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_option(LOG_CONS);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_ODELAY
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_option(LOG_ODELAY); /* deprecated */
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_NDELAY
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_option(LOG_NDELAY);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_NOWAIT
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_option(LOG_NOWAIT); /* deprecated */
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_PERROR
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_option(LOG_PERROR);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
/* Syslog facilities */
|
|
|
|
|
|
|
|
#define rb_define_syslog_facility(c) \
|
|
|
|
rb_define_const(mSyslogFacility, #c, INT2NUM(c))
|
|
|
|
|
2001-11-26 15:00:40 +03:00
|
|
|
#ifdef LOG_AUTH
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_AUTH);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_AUTHPRIV
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_AUTHPRIV);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_CONSOLE
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_CONSOLE);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_CRON
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_CRON);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_DAEMON
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_DAEMON);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_FTP
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_FTP);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_KERN
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_KERN);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LPR
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LPR);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_MAIL
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_MAIL);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_NEWS
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_NEWS);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_NTP
|
2017-09-27 08:08:53 +03:00
|
|
|
rb_define_syslog_facility(LOG_NTP);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_SECURITY
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_SECURITY);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_SYSLOG
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_SYSLOG);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_USER
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_USER);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_UUCP
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_UUCP);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL0
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL0);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL1
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL1);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL2
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL2);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL3
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL3);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL4
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL4);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL5
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL5);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL6
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL6);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_LOCAL7
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_facility(LOG_LOCAL7);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
|
2012-05-21 11:18:49 +04:00
|
|
|
/* Syslog levels and the shortcut methods */
|
|
|
|
|
|
|
|
#define rb_define_syslog_level(c, m) \
|
|
|
|
rb_define_const(mSyslogLevel, #c, INT2NUM(c)); \
|
|
|
|
rb_define_module_function(mSyslog, #m, mSyslog_##m, -1)
|
2001-11-26 15:00:40 +03:00
|
|
|
|
|
|
|
#ifdef LOG_EMERG
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_EMERG, emerg);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_ALERT
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_ALERT, alert);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_CRIT
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_CRIT, crit);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_ERR
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_ERR, err);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_WARNING
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_WARNING, warning);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_NOTICE
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_NOTICE, notice);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_INFO
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_INFO, info);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
|
|
|
#ifdef LOG_DEBUG
|
2012-05-21 11:18:49 +04:00
|
|
|
rb_define_syslog_level(LOG_DEBUG, debug);
|
2001-11-26 15:00:40 +03:00
|
|
|
#endif
|
2012-05-21 11:18:49 +04:00
|
|
|
|
|
|
|
/* Syslog macros */
|
|
|
|
|
|
|
|
rb_define_method(mSyslogMacros, "LOG_MASK", mSyslogMacros_LOG_MASK, 1);
|
|
|
|
rb_define_method(mSyslogMacros, "LOG_UPTO", mSyslogMacros_LOG_UPTO, 1);
|
|
|
|
rb_define_singleton_method(mSyslogMacros, "included", mSyslogMacros_included, 1);
|
|
|
|
|
|
|
|
rb_include_module(mSyslogConstants, mSyslogOption);
|
|
|
|
rb_include_module(mSyslogConstants, mSyslogFacility);
|
|
|
|
rb_include_module(mSyslogConstants, mSyslogLevel);
|
|
|
|
rb_funcall(mSyslogConstants, rb_intern("include"), 1, mSyslogMacros);
|
|
|
|
|
|
|
|
rb_define_singleton_method(mSyslogConstants, "included", mSyslogMacros_included, 1);
|
|
|
|
rb_funcall(mSyslog, rb_intern("include"), 1, mSyslogConstants);
|
2001-11-26 15:00:40 +03:00
|
|
|
}
|