Module: Syslog

Includes:
Constants
Defined in:
syslog.c

Defined Under Namespace

Modules: Constants

Class Method Summary collapse

Class Method Details

.closeObject

Closes the syslog facility. Raises a runtime exception if it is not open.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'syslog.c', line 43

static VALUE mSyslog_close(VALUE self)
{
    rb_secure(4);
    if (!syslog_opened) {
        rb_raise(rb_eRuntimeError, "syslog not opened");
    }

    closelog();

    free((void *)syslog_ident);
    syslog_ident = NULL;
    syslog_options = syslog_facility = syslog_mask = -1;
    syslog_opened = 0;

    return Qnil;
}

.facilityObject

Returns the facility number used in the last call to open()



222
223
224
225
# File 'syslog.c', line 222

static VALUE mSyslog_facility(VALUE self)
{
    return syslog_opened ? INT2NUM(syslog_facility) : Qnil;
}

.identObject

Returns the identity string used in the last call to open()



208
209
210
211
# File 'syslog.c', line 208

static VALUE mSyslog_ident(VALUE self)
{
    return syslog_opened ? rb_str_new2(syslog_ident) : Qnil;
}

.inspectObject

Returns an inspect() string summarizing the object state.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'syslog.c', line 314

static VALUE mSyslog_inspect(VALUE self)
{
    char buf[1024];

    if (syslog_opened) {
	snprintf(buf, sizeof(buf),
	  "<#%s: opened=true, ident=\"%s\", options=%d, facility=%d, mask=%d>",
	  rb_class2name(self),
	  syslog_ident,
	  syslog_options,
	  syslog_facility,
	  syslog_mask);
    } else {
	snprintf(buf, sizeof(buf),
	  "<#%s: opened=false>", rb_class2name(self));
    }

    return rb_str_new2(buf);
}

.instanceObject

Returns self, for backward compatibility.



336
337
338
339
# File 'syslog.c', line 336

static VALUE mSyslog_instance(VALUE self)
{
    return self;
}

.log(priority, format_string, *format_args) ⇒ Object

Log a message with the specified priority. Example:

Syslog.log(Syslog::LOG_CRIT, "Out of disk space")
Syslog.log(Syslog::LOG_CRIT, "User %s logged in", ENV['USER'])

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

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")

Format strings are as for printf/sprintf, except that in addition %m is replaced with the error message string that would be returned by strerror(errno).



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'syslog.c', line 292

static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
{
    VALUE pri;

    if (argc < 2) {
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 2+)", argc);
    }

    argc--;
    pri = *argv++;

    if (!FIXNUM_P(pri)) {
      rb_raise(rb_eTypeError, "type mismatch: %s given", rb_class2name(CLASS_OF(pri)));
    }

    syslog_write(FIX2INT(pri), argc, argv);

    return self;
}

.LOG_MASKObject

.LOG_UPTO(priority_level) ⇒ Object

Generates a mask value for priority levels at or below the level specified. See #mask=



390
391
392
393
# File 'syslog.c', line 390

static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri)
{
    return INT2FIX(LOG_UPTO(NUM2INT(pri)));
}

.maskObject

Returns the log priority mask in effect. The mask is not reset by opening or closing syslog.



230
231
232
233
# File 'syslog.c', line 230

static VALUE mSyslog_get_mask(VALUE self)
{
    return syslog_opened ? INT2NUM(syslog_mask) : Qnil;
}

.mask=(priority_mask) ⇒ Object

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().



250
251
252
253
254
255
256
257
258
259
260
# File 'syslog.c', line 250

static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
{
    rb_secure(4);
    if (!syslog_opened) {
        rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
    }

    setlogmask(syslog_mask = NUM2INT(mask));

    return mask;
}

.open(ident, options, facility) ⇒ Object

: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).

LOG_LRP

Line printer subsystem.

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)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'syslog.c', line 138

static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
{
    VALUE ident, opt, fac;

    if (syslog_opened) {
        rb_raise(rb_eRuntimeError, "syslog already open");
    }

    rb_scan_args(argc, argv, "03", &ident, &opt, &fac);

    if (NIL_P(ident)) {
        ident = rb_gv_get("$0");
    }
    SafeStringValue(ident);
    syslog_ident = strdup(RSTRING_PTR(ident));

    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);
    }

    openlog(syslog_ident, syslog_options, syslog_facility);

    syslog_opened = 1;

    setlogmask(syslog_mask = setlogmask(0));

    /* be like File.new.open {...} */
    if (rb_block_given_p()) {
        rb_ensure(rb_yield, self, mSyslog_close, self);
    }

    return self;
}

.reopen(ident, options, facility) ⇒ Object

:yields: syslog

Closes and then reopens the syslog.

Arguments are the same as for open().



189
190
191
192
193
194
# File 'syslog.c', line 189

static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
{
    mSyslog_close(self);

    return mSyslog_open(argc, argv, self);
}

.opened?Boolean

Returns true if the syslog is open.

Returns:

  • (Boolean)


201
202
203
204
# File 'syslog.c', line 201

static VALUE mSyslog_isopen(VALUE self)
{
    return syslog_opened ? Qtrue : Qfalse;
}

.optionsObject

Returns the options bitmask used in the last call to open()



215
216
217
218
# File 'syslog.c', line 215

static VALUE mSyslog_options(VALUE self)
{
    return syslog_opened ? INT2NUM(syslog_options) : Qnil;
}

.reopen(ident, options, facility) ⇒ Object

:yields: syslog

Closes and then reopens the syslog.

Arguments are the same as for open().



189
190
191
192
193
194
# File 'syslog.c', line 189

static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
{
    mSyslog_close(self);

    return mSyslog_open(argc, argv, self);
}