* ext/syslog/syslog.c: Merge from rough. Turn Syslog into a

module keeping backward compatibility intact.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2134 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
knu 2002-02-25 12:13:30 +00:00
parent 959a99078e
commit b700e189bd
4 changed files with 136 additions and 166 deletions

View File

@ -1,3 +1,8 @@
Mon Feb 25 21:12:08 2002 Akinori MUSHA <knu@iDaemons.org>
* ext/syslog/syslog.c: Merge from rough. Turn Syslog into a
module keeping backward compatibility intact.
Mon Feb 25 19:35:48 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp> Mon Feb 25 19:35:48 2002 Nobuyoshi Nakada <nobu.nakada@nifty.ne.jp>
* sample/test.rb (system): test with scripts under the source * sample/test.rb (system): test with scripts under the source

View File

@ -3,7 +3,7 @@
* Amos Gouaux, University of Texas at Dallas * Amos Gouaux, University of Texas at Dallas
* <amos+ruby@utdallas.edu> * <amos+ruby@utdallas.edu>
* *
* $RoughId: syslog.c,v 1.17 2001/11/24 12:42:39 knu Exp $ * $RoughId: syslog.c,v 1.20 2002/02/25 08:20:14 knu Exp $
* $Id$ * $Id$
*/ */
@ -11,9 +11,9 @@
#include <syslog.h> #include <syslog.h>
/* Syslog class */ /* Syslog class */
static VALUE cSyslog, mSyslogConstants; static VALUE mSyslog, mSyslogConstants;
static VALUE syslog_instance = Qnil, syslog_ident, syslog_options, static VALUE syslog_ident = Qnil, syslog_options = INT2FIX(-1),
syslog_facility, syslog_mask; syslog_facility = INT2FIX(-1), syslog_mask = INT2FIX(-1);
static int syslog_opened = 0; static int syslog_opened = 0;
/* Package helper routines */ /* Package helper routines */
@ -34,8 +34,8 @@ static void syslog_write(int pri, int argc, VALUE *argv)
syslog(pri, "%s", RSTRING(str)->ptr); syslog(pri, "%s", RSTRING(str)->ptr);
} }
/* Syslog instance methods */ /* Syslog module methods */
static VALUE cSyslog_close(VALUE self) static VALUE mSyslog_close(VALUE self)
{ {
closelog(); closelog();
syslog_opened = 0; syslog_opened = 0;
@ -43,7 +43,7 @@ static VALUE cSyslog_close(VALUE self)
return Qnil; return Qnil;
} }
static VALUE cSyslog_open(int argc, VALUE *argv, VALUE self) static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self)
{ {
VALUE ident, opt, fac; VALUE ident, opt, fac;
int mask; int mask;
@ -62,7 +62,7 @@ static VALUE cSyslog_open(int argc, VALUE *argv, VALUE self)
fac = INT2NUM(LOG_USER); fac = INT2NUM(LOG_USER);
} }
SafeStringValue(ident); Check_SafeStr(ident);
syslog_ident = ident; syslog_ident = ident;
syslog_options = opt; syslog_options = opt;
syslog_facility = fac; syslog_facility = fac;
@ -74,55 +74,45 @@ static VALUE cSyslog_open(int argc, VALUE *argv, VALUE self)
/* be like File.new.open {...} */ /* be like File.new.open {...} */
if (rb_block_given_p()) { if (rb_block_given_p()) {
rb_ensure(rb_yield, self, cSyslog_close, self); rb_ensure(rb_yield, self, mSyslog_close, self);
} }
return self; return self;
} }
static VALUE cSyslog_init(VALUE self) static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self)
{ {
syslog_instance = self; mSyslog_close(self);
syslog_opened = 0;
syslog_ident = Qnil;
syslog_options = syslog_facility = syslog_mask = INT2FIX(-1);
return self; return mSyslog_open(argc, argv, self);
} }
static VALUE cSyslog_reopen(int argc, VALUE *argv, VALUE self) static VALUE mSyslog_isopen(VALUE self)
{
cSyslog_close(self);
return cSyslog_open(argc, argv, self);
}
static VALUE cSyslog_isopen(VALUE self)
{ {
return syslog_opened ? Qtrue : Qfalse; return syslog_opened ? Qtrue : Qfalse;
} }
static VALUE cSyslog_ident(VALUE self) static VALUE mSyslog_ident(VALUE self)
{ {
return syslog_ident; return syslog_ident;
} }
static VALUE cSyslog_options(VALUE self) static VALUE mSyslog_options(VALUE self)
{ {
return syslog_options; return syslog_options;
} }
static VALUE cSyslog_facility(VALUE self) static VALUE mSyslog_facility(VALUE self)
{ {
return syslog_facility; return syslog_facility;
} }
static VALUE cSyslog_get_mask(VALUE self) static VALUE mSyslog_get_mask(VALUE self)
{ {
return syslog_mask; return syslog_mask;
} }
static VALUE cSyslog_set_mask(VALUE self, VALUE mask) static VALUE mSyslog_set_mask(VALUE self, VALUE mask)
{ {
if (!syslog_opened) { if (!syslog_opened) {
rb_raise(rb_eRuntimeError, "must open syslog before setting log mask"); rb_raise(rb_eRuntimeError, "must open syslog before setting log mask");
@ -134,7 +124,7 @@ static VALUE cSyslog_set_mask(VALUE self, VALUE mask)
return mask; return mask;
} }
static VALUE cSyslog_log(int argc, VALUE *argv, VALUE self) static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self)
{ {
VALUE pri; VALUE pri;
@ -154,7 +144,7 @@ static VALUE cSyslog_log(int argc, VALUE *argv, VALUE self)
return self; return self;
} }
static VALUE cSyslog_inspect(VALUE self) static VALUE mSyslog_inspect(VALUE self)
{ {
#define N 7 #define N 7
int argc = N; int argc = N;
@ -163,7 +153,7 @@ static VALUE cSyslog_inspect(VALUE self)
"<#%s: opened=%s, ident=\"%s\", options=%d, facility=%d, mask=%d>"; "<#%s: opened=%s, ident=\"%s\", options=%d, facility=%d, mask=%d>";
argv[0] = rb_str_new(fmt, sizeof(fmt) - 1); argv[0] = rb_str_new(fmt, sizeof(fmt) - 1);
argv[1] = cSyslog; argv[1] = mSyslog;
argv[2] = syslog_opened ? Qtrue : Qfalse; argv[2] = syslog_opened ? Qtrue : Qfalse;
argv[3] = syslog_ident; argv[3] = syslog_ident;
argv[4] = syslog_options; argv[4] = syslog_options;
@ -174,8 +164,13 @@ static VALUE cSyslog_inspect(VALUE self)
#undef N #undef N
} }
static VALUE mSyslog_instance(VALUE self)
{
return self;
}
#define define_syslog_shortcut_method(pri, name) \ #define define_syslog_shortcut_method(pri, name) \
static VALUE cSyslog_##name(int argc, VALUE *argv, VALUE self) \ static VALUE mSyslog_##name(int argc, VALUE *argv, VALUE self) \
{ \ { \
syslog_write(pri, argc, argv); \ syslog_write(pri, argc, argv); \
\ \
@ -207,38 +202,12 @@ define_syslog_shortcut_method(LOG_INFO, info)
define_syslog_shortcut_method(LOG_DEBUG, debug) define_syslog_shortcut_method(LOG_DEBUG, debug)
#endif #endif
/* Syslog class methods */ static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE pri)
static VALUE cSyslog_s_instance(VALUE klass)
{
VALUE obj;
obj = syslog_instance;
if (NIL_P(obj)) {
obj = rb_obj_alloc(klass);
rb_obj_call_init(obj, 0, NULL);
}
return obj;
}
static VALUE cSyslog_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE obj;
obj = cSyslog_s_instance(klass);
cSyslog_open(argc, argv, obj);
return obj;
}
static VALUE cSyslog_s_LOG_MASK(VALUE klass, VALUE pri)
{ {
return INT2FIX(LOG_MASK(FIX2INT(pri))); return INT2FIX(LOG_MASK(FIX2INT(pri)));
} }
static VALUE cSyslog_s_LOG_UPTO(VALUE klass, VALUE pri) static VALUE mSyslogConstants_LOG_UPTO(VALUE klass, VALUE pri)
{ {
return INT2FIX(LOG_UPTO(FIX2INT(pri))); return INT2FIX(LOG_UPTO(FIX2INT(pri)));
} }
@ -246,35 +215,34 @@ static VALUE cSyslog_s_LOG_UPTO(VALUE klass, VALUE pri)
/* Init for package syslog */ /* Init for package syslog */
void Init_syslog() void Init_syslog()
{ {
cSyslog = rb_define_class("Syslog", rb_cObject); mSyslog = rb_define_module("Syslog");
mSyslogConstants = rb_define_module_under(cSyslog, "Constants"); mSyslogConstants = rb_define_module_under(mSyslog, "Constants");
rb_include_module(cSyslog, mSyslogConstants); rb_include_module(mSyslog, mSyslogConstants);
rb_define_module_function(cSyslog, "open", cSyslog_s_open, -1); rb_define_module_function(mSyslog, "open", mSyslog_open, -1);
rb_define_module_function(cSyslog, "instance", cSyslog_s_instance, 0); rb_define_module_function(mSyslog, "reopen", mSyslog_reopen, -1);
rb_define_module_function(cSyslog, "LOG_MASK", cSyslog_s_LOG_MASK, 1); rb_define_module_function(mSyslog, "open!", mSyslog_reopen, -1);
rb_define_module_function(cSyslog, "LOG_UPTO", cSyslog_s_LOG_UPTO, 1); rb_define_module_function(mSyslog, "opened?", mSyslog_isopen, 0);
rb_undef_method(CLASS_OF(cSyslog), "new"); 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);
rb_define_method(cSyslog, "initialize", cSyslog_init, 0); rb_define_module_function(mSyslog, "log", mSyslog_log, -1);
rb_define_method(cSyslog, "open", cSyslog_open, -1); rb_define_module_function(mSyslog, "close", mSyslog_close, 0);
rb_define_method(cSyslog, "reopen", cSyslog_reopen, -1); rb_define_module_function(mSyslog, "mask", mSyslog_get_mask, 0);
rb_define_method(cSyslog, "open!", cSyslog_reopen, -1); rb_define_module_function(mSyslog, "mask=", mSyslog_set_mask, 1);
rb_define_method(cSyslog, "opened?", cSyslog_isopen, 0);
rb_define_method(cSyslog, "ident", cSyslog_ident, 0); rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
rb_define_method(cSyslog, "options", cSyslog_options, 0); rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
rb_define_method(cSyslog, "facility", cSyslog_facility, 0);
rb_define_method(cSyslog, "log", cSyslog_log, -1); rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0);
rb_define_method(cSyslog, "close", cSyslog_close, 0); rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0);
rb_define_method(cSyslog, "mask", cSyslog_get_mask, 0);
rb_define_method(cSyslog, "mask=", cSyslog_set_mask, 1);
rb_define_method(cSyslog, "inspect", cSyslog_inspect, 0); rb_define_module_function(mSyslogConstants, "LOG_MASK", mSyslogConstants_LOG_MASK, 1);
rb_define_module_function(mSyslogConstants, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1);
#define rb_define_syslog_const(id) \ #define rb_define_syslog_const(id) \
rb_define_const(mSyslogConstants, #id, INT2NUM(id)) rb_define_const(mSyslogConstants, #id, INT2NUM(id))
@ -371,7 +339,7 @@ void Init_syslog()
#endif #endif
#define rb_define_syslog_shortcut(name) \ #define rb_define_syslog_shortcut(name) \
rb_define_method(cSyslog, #name, cSyslog_##name, -1) rb_define_module_function(mSyslog, #name, mSyslog_##name, -1)
/* Various syslog priorities and the shortcut methods */ /* Various syslog priorities and the shortcut methods */
#ifdef LOG_EMERG #ifdef LOG_EMERG

View File

@ -1,5 +1,5 @@
.\" syslog.txt - -*- Indented-Text -*- .\" syslog.txt - -*- Indented-Text -*-
$RoughId: syslog.txt,v 1.15 2001/11/25 21:21:23 knu Exp $ $RoughId: syslog.txt,v 1.18 2002/02/25 08:20:14 knu Exp $
$Id$ $Id$
UNIX Syslog extension for Ruby UNIX Syslog extension for Ruby
@ -9,11 +9,9 @@ Amos Gouaux, University of Texas at Dallas
Akinori MUSHA Akinori MUSHA
<knu@ruby-lang.org> <knu@ruby-lang.org>
** Syslog(Class) ** Syslog(Module)
Superclass: Object Included Modules: Syslog::Constants
Mix-ins: Syslog::Constants
require 'syslog' require 'syslog'
@ -22,40 +20,19 @@ if you're writing a server in Ruby. For the details of the syslog(8)
architecture and constants, see the syslog(3) manual page of your architecture and constants, see the syslog(3) manual page of your
platform. platform.
Class Methods: Module Methods:
open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
facility = Syslog::LOG_USER) [{ |syslog| ... }] facility = Syslog::LOG_USER) [{ |syslog| ... }]
Opens syslog with the given options and returns the singleton Opens syslog with the given options and returns the module
object of the Syslog class. If a block is given, calls it itself. If a block is given, calls it with an argument of
with an argument of the object. If syslog is already opened, itself. If syslog is already opened, raises RuntimeError.
raises RuntimeError.
Example: Example:
sl = Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY,
Syslog::LOG_FTP) Syslog::LOG_FTP)
instance
Returns the singleton object.
LOG_MASK(pri)
Creates a mask for one priority.
LOG_UPTO(pri)
Creates a mask for all priorities up to pri.
Methods:
open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
facility = Syslog::LOG_USER)
Opens syslog with the given options. If syslog is already
opened, raises RuntimeError.
open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
facility = Syslog::LOG_USER) facility = Syslog::LOG_USER)
reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, reopen(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS,
@ -72,14 +49,14 @@ Methods:
facility facility
Returns the parameters given in the last open, respectively. Returns the parameters given in the last open, respectively.
Every call of Syslog::open/Syslog#open resets those values. Every call of Syslog::open resets these values.
log(pri, message, ...) log(pri, message, ...)
Writes message to syslog. Writes message to syslog.
Example: Example:
sl.log(Syslog::LOG_CRIT, "the sky is falling in %d seconds!", 10) Syslog.log(Syslog::LOG_CRIT, "the sky is falling in %d seconds!", 10)
crit(message, ...) crit(message, ...)
emerg(message, ...) emerg(message, ...)
@ -90,21 +67,21 @@ Methods:
info(message, ...) info(message, ...)
debug(message, ...) debug(message, ...)
These are shortcut methods of Syslog#log(). The Lineup may These are shortcut methods of Syslog::log(). The lineup may
vary depending on what priorities are defined on your system. vary depending on what priorities are defined on your system.
Example: Example:
sl.crit("the sky is falling in %d seconds!",5) Syslog.crit("the sky is falling in %d seconds!", 5)
mask mask
mask=(mask) mask=(mask)
Returns or sets the log priority mask. The value of the mask Returns or sets the log priority mask. The value of the mask
is persistent and Syslog::open/Syslog#open/Syslog#close don't is persistent and will not be reset by Syslog::open or
reset it. Syslog::close.
Example: Example:
sl.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR) Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR)
close close
@ -112,13 +89,33 @@ Methods:
inspect inspect
Returns the "inspect" string of the object. Returns the "inspect" string of the Syslog module.
instance
Returns the module itself. (Just for backward compatibility)
LOG_MASK(pri)
Creates a mask for one priority.
LOG_UPTO(pri)
Creates a mask for all priorities up to pri.
** Syslog::Constants(Module) ** Syslog::Constants(Module)
Superclass: Object
require 'syslog' require 'syslog'
include Syslog::Constants include Syslog::Constants
This module includes the LOG_* constants available on the system. This module includes the LOG_* constants available on the system.
Module Methods:
LOG_MASK(pri)
Creates a mask for one priority.
LOG_UPTO(pri)
Creates a mask for all priorities up to pri.

View File

@ -1,5 +1,5 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# $RoughId: test.rb,v 1.8 2001/11/24 18:11:32 knu Exp $ # $RoughId: test.rb,v 1.9 2002/02/25 08:20:14 knu Exp $
# $Id$ # $Id$
# Please only run this test on machines reasonable for testing. # Please only run this test on machines reasonable for testing.
@ -14,94 +14,94 @@ $:.unshift('.')
require 'syslog' require 'syslog'
class TestSyslog < RUNIT::TestCase class TestSyslog < RUNIT::TestCase
def test_s_new def test_new
assert_exception(NameError) { assert_exception(NameError) {
Syslog.new Syslog.new
} }
end end
def test_s_instance def test_instance
sl1 = Syslog.instance sl1 = Syslog.instance
sl2 = Syslog.open sl2 = Syslog.open
sl3 = Syslog.instance sl3 = Syslog.instance
assert_equal(sl1, sl2) assert_equal(Syslog, sl1)
assert_equal(sl1, sl3) assert_equal(Syslog, sl2)
assert_equal(Syslog, sl3)
ensure ensure
sl1.close Syslog.close
end end
def test_s_open def test_open
# default parameters # default parameters
sl = Syslog.open Syslog.open
assert_equal($0, sl.ident) assert_equal($0, Syslog.ident)
assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, sl.options) assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options)
assert_equal(Syslog::LOG_USER, sl.facility) assert_equal(Syslog::LOG_USER, Syslog.facility)
# open without close # open without close
assert_exception(RuntimeError) { assert_exception(RuntimeError) {
sl.open Syslog.open
} }
sl.close Syslog.close
# given parameters # given parameters
sl = Syslog.open("foo", Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog::LOG_DAEMON) Syslog.open("foo", Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog::LOG_DAEMON)
assert_equal('foo', sl.ident) assert_equal('foo', Syslog.ident)
assert_equal(Syslog::LOG_NDELAY | Syslog::LOG_PERROR, sl.options) assert_equal(Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog.options)
assert_equal(Syslog::LOG_DAEMON, sl.facility) assert_equal(Syslog::LOG_DAEMON, Syslog.facility)
sl.close Syslog.close
# default parameters again (after close) # default parameters again (after close)
sl = Syslog.open Syslog.open
sl.close Syslog.close
assert_equal($0, sl.ident) assert_equal($0, Syslog.ident)
assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, sl.options) assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options)
assert_equal(Syslog::LOG_USER, sl.facility) assert_equal(Syslog::LOG_USER, Syslog.facility)
# block # block
param = nil param = nil
Syslog.open { |param| } Syslog.open { |param| }
assert_equal(sl, param) assert_equal(Syslog, param)
ensure ensure
sl.close Syslog.close
end end
def test_opened? def test_opened?
sl = Syslog.instance assert_equal(false, Syslog.opened?)
assert_equal(false, sl.opened?)
sl.open Syslog.open
assert_equal(true, sl.opened?) assert_equal(true, Syslog.opened?)
sl.close Syslog.close
assert_equal(false, sl.opened?) assert_equal(false, Syslog.opened?)
sl.open { Syslog.open {
assert_equal(true, sl.opened?) assert_equal(true, Syslog.opened?)
} }
assert_equal(false, sl.opened?) assert_equal(false, Syslog.opened?)
end end
def test_mask def test_mask
sl = Syslog.open Syslog.open
orig = sl.mask orig = Syslog.mask
sl.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR) Syslog.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR)
assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), sl.mask) assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), Syslog.mask)
sl.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT) Syslog.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT)
assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), sl.mask) assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), Syslog.mask)
sl.mask = orig Syslog.mask = orig
ensure ensure
sl.close Syslog.close
end end
def test_log def test_log