From b700e189bd60f68de89d5785f57edf84588b0a4a Mon Sep 17 00:00:00 2001 From: knu Date: Mon, 25 Feb 2002 12:13:30 +0000 Subject: [PATCH] * 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 --- ChangeLog | 5 ++ ext/syslog/syslog.c | 132 ++++++++++++++++-------------------------- ext/syslog/syslog.txt | 81 +++++++++++++------------- ext/syslog/test.rb | 84 +++++++++++++-------------- 4 files changed, 136 insertions(+), 166 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2a4142e08d..caeef8f329 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Feb 25 21:12:08 2002 Akinori MUSHA + + * 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 * sample/test.rb (system): test with scripts under the source diff --git a/ext/syslog/syslog.c b/ext/syslog/syslog.c index 43392a3cef..7e0e7d1984 100644 --- a/ext/syslog/syslog.c +++ b/ext/syslog/syslog.c @@ -3,7 +3,7 @@ * Amos Gouaux, University of Texas at Dallas * * - * $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$ */ @@ -11,9 +11,9 @@ #include /* Syslog class */ -static VALUE cSyslog, mSyslogConstants; -static VALUE syslog_instance = Qnil, syslog_ident, syslog_options, - syslog_facility, syslog_mask; +static VALUE mSyslog, mSyslogConstants; +static VALUE syslog_ident = Qnil, syslog_options = INT2FIX(-1), + syslog_facility = INT2FIX(-1), syslog_mask = INT2FIX(-1); static int syslog_opened = 0; /* Package helper routines */ @@ -34,8 +34,8 @@ static void syslog_write(int pri, int argc, VALUE *argv) syslog(pri, "%s", RSTRING(str)->ptr); } -/* Syslog instance methods */ -static VALUE cSyslog_close(VALUE self) +/* Syslog module methods */ +static VALUE mSyslog_close(VALUE self) { closelog(); syslog_opened = 0; @@ -43,7 +43,7 @@ static VALUE cSyslog_close(VALUE self) 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; int mask; @@ -62,7 +62,7 @@ static VALUE cSyslog_open(int argc, VALUE *argv, VALUE self) fac = INT2NUM(LOG_USER); } - SafeStringValue(ident); + Check_SafeStr(ident); syslog_ident = ident; syslog_options = opt; syslog_facility = fac; @@ -74,55 +74,45 @@ static VALUE cSyslog_open(int argc, VALUE *argv, VALUE self) /* be like File.new.open {...} */ if (rb_block_given_p()) { - rb_ensure(rb_yield, self, cSyslog_close, self); + rb_ensure(rb_yield, self, mSyslog_close, self); } return self; } -static VALUE cSyslog_init(VALUE self) +static VALUE mSyslog_reopen(int argc, VALUE *argv, VALUE self) { - syslog_instance = self; - syslog_opened = 0; - syslog_ident = Qnil; - syslog_options = syslog_facility = syslog_mask = INT2FIX(-1); + mSyslog_close(self); - return self; + return mSyslog_open(argc, argv, self); } -static VALUE cSyslog_reopen(int argc, VALUE *argv, VALUE self) -{ - cSyslog_close(self); - - return cSyslog_open(argc, argv, self); -} - -static VALUE cSyslog_isopen(VALUE self) +static VALUE mSyslog_isopen(VALUE self) { return syslog_opened ? Qtrue : Qfalse; } -static VALUE cSyslog_ident(VALUE self) +static VALUE mSyslog_ident(VALUE self) { return syslog_ident; } -static VALUE cSyslog_options(VALUE self) +static VALUE mSyslog_options(VALUE self) { return syslog_options; } -static VALUE cSyslog_facility(VALUE self) +static VALUE mSyslog_facility(VALUE self) { return syslog_facility; } -static VALUE cSyslog_get_mask(VALUE self) +static VALUE mSyslog_get_mask(VALUE self) { return syslog_mask; } -static VALUE cSyslog_set_mask(VALUE self, VALUE mask) +static VALUE mSyslog_set_mask(VALUE self, VALUE mask) { if (!syslog_opened) { 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; } -static VALUE cSyslog_log(int argc, VALUE *argv, VALUE self) +static VALUE mSyslog_log(int argc, VALUE *argv, VALUE self) { VALUE pri; @@ -154,7 +144,7 @@ static VALUE cSyslog_log(int argc, VALUE *argv, VALUE self) return self; } -static VALUE cSyslog_inspect(VALUE self) +static VALUE mSyslog_inspect(VALUE self) { #define N 7 int argc = N; @@ -163,7 +153,7 @@ static VALUE cSyslog_inspect(VALUE self) "<#%s: opened=%s, ident=\"%s\", options=%d, facility=%d, mask=%d>"; argv[0] = rb_str_new(fmt, sizeof(fmt) - 1); - argv[1] = cSyslog; + argv[1] = mSyslog; argv[2] = syslog_opened ? Qtrue : Qfalse; argv[3] = syslog_ident; argv[4] = syslog_options; @@ -174,8 +164,13 @@ static VALUE cSyslog_inspect(VALUE self) #undef N } +static VALUE mSyslog_instance(VALUE self) +{ + return self; +} + #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); \ \ @@ -207,38 +202,12 @@ define_syslog_shortcut_method(LOG_INFO, info) define_syslog_shortcut_method(LOG_DEBUG, debug) #endif -/* Syslog class methods */ -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) +static VALUE mSyslogConstants_LOG_MASK(VALUE klass, VALUE 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))); } @@ -246,35 +215,34 @@ static VALUE cSyslog_s_LOG_UPTO(VALUE klass, VALUE pri) /* Init for package 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(cSyslog, "instance", cSyslog_s_instance, 0); - rb_define_module_function(cSyslog, "LOG_MASK", cSyslog_s_LOG_MASK, 1); - rb_define_module_function(cSyslog, "LOG_UPTO", cSyslog_s_LOG_UPTO, 1); + 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); - 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_method(cSyslog, "open", cSyslog_open, -1); - rb_define_method(cSyslog, "reopen", cSyslog_reopen, -1); - rb_define_method(cSyslog, "open!", cSyslog_reopen, -1); - rb_define_method(cSyslog, "opened?", cSyslog_isopen, 0); + 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); - rb_define_method(cSyslog, "ident", cSyslog_ident, 0); - rb_define_method(cSyslog, "options", cSyslog_options, 0); - rb_define_method(cSyslog, "facility", cSyslog_facility, 0); + rb_define_module_function(mSyslog, "LOG_MASK", mSyslogConstants_LOG_MASK, 1); + rb_define_module_function(mSyslog, "LOG_UPTO", mSyslogConstants_LOG_UPTO, 1); - rb_define_method(cSyslog, "log", cSyslog_log, -1); - rb_define_method(cSyslog, "close", cSyslog_close, 0); - rb_define_method(cSyslog, "mask", cSyslog_get_mask, 0); - rb_define_method(cSyslog, "mask=", cSyslog_set_mask, 1); + rb_define_module_function(mSyslog, "inspect", mSyslog_inspect, 0); + rb_define_module_function(mSyslog, "instance", mSyslog_instance, 0); - 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) \ rb_define_const(mSyslogConstants, #id, INT2NUM(id)) @@ -371,7 +339,7 @@ void Init_syslog() #endif #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 */ #ifdef LOG_EMERG diff --git a/ext/syslog/syslog.txt b/ext/syslog/syslog.txt index d9dcfc4315..9aed35133d 100644 --- a/ext/syslog/syslog.txt +++ b/ext/syslog/syslog.txt @@ -1,5 +1,5 @@ .\" 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$ UNIX Syslog extension for Ruby @@ -9,11 +9,9 @@ Amos Gouaux, University of Texas at Dallas Akinori MUSHA -** Syslog(Class) +** Syslog(Module) -Superclass: Object - -Mix-ins: Syslog::Constants +Included Modules: Syslog::Constants require 'syslog' @@ -22,39 +20,18 @@ 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 platform. -Class Methods: +Module Methods: open(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER) [{ |syslog| ... }] - Opens syslog with the given options and returns the singleton - object of the Syslog class. If a block is given, calls it - with an argument of the object. If syslog is already opened, - raises RuntimeError. + Opens syslog with the given options and returns the module + itself. If a block is given, calls it with an argument of + itself. If syslog is already opened, raises RuntimeError. Example: - sl = Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, - 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. + Syslog.open('ftpd', Syslog::LOG_PID | Syslog::LOG_NDELAY, + Syslog::LOG_FTP) open!(ident = $0, logopt = Syslog::LOG_PID | Syslog::LOG_CONS, facility = Syslog::LOG_USER) @@ -72,14 +49,14 @@ Methods: facility 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, ...) Writes message to syslog. 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, ...) emerg(message, ...) @@ -90,21 +67,21 @@ Methods: info(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. Example: - sl.crit("the sky is falling in %d seconds!",5) + Syslog.crit("the sky is falling in %d seconds!", 5) mask mask=(mask) Returns or sets the log priority mask. The value of the mask - is persistent and Syslog::open/Syslog#open/Syslog#close don't - reset it. + is persistent and will not be reset by Syslog::open or + Syslog::close. Example: - sl.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR) + Syslog.mask = Syslog::LOG_UPTO(Syslog::LOG_ERR) close @@ -112,13 +89,33 @@ Methods: 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) -Superclass: Object - require 'syslog' include Syslog::Constants 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. diff --git a/ext/syslog/test.rb b/ext/syslog/test.rb index fd94f1e590..6cd861b2b2 100644 --- a/ext/syslog/test.rb +++ b/ext/syslog/test.rb @@ -1,5 +1,5 @@ #!/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$ # Please only run this test on machines reasonable for testing. @@ -14,94 +14,94 @@ $:.unshift('.') require 'syslog' class TestSyslog < RUNIT::TestCase - def test_s_new + def test_new assert_exception(NameError) { Syslog.new } end - def test_s_instance + def test_instance sl1 = Syslog.instance sl2 = Syslog.open sl3 = Syslog.instance - assert_equal(sl1, sl2) - assert_equal(sl1, sl3) + assert_equal(Syslog, sl1) + assert_equal(Syslog, sl2) + assert_equal(Syslog, sl3) ensure - sl1.close + Syslog.close end - def test_s_open + def test_open # default parameters - sl = Syslog.open + Syslog.open - assert_equal($0, sl.ident) - assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, sl.options) - assert_equal(Syslog::LOG_USER, sl.facility) + assert_equal($0, Syslog.ident) + assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options) + assert_equal(Syslog::LOG_USER, Syslog.facility) # open without close assert_exception(RuntimeError) { - sl.open + Syslog.open } - sl.close + Syslog.close # 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(Syslog::LOG_NDELAY | Syslog::LOG_PERROR, sl.options) - assert_equal(Syslog::LOG_DAEMON, sl.facility) + assert_equal('foo', Syslog.ident) + assert_equal(Syslog::LOG_NDELAY | Syslog::LOG_PERROR, Syslog.options) + assert_equal(Syslog::LOG_DAEMON, Syslog.facility) - sl.close + Syslog.close # default parameters again (after close) - sl = Syslog.open - sl.close + Syslog.open + Syslog.close - assert_equal($0, sl.ident) - assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, sl.options) - assert_equal(Syslog::LOG_USER, sl.facility) + assert_equal($0, Syslog.ident) + assert_equal(Syslog::LOG_PID | Syslog::LOG_CONS, Syslog.options) + assert_equal(Syslog::LOG_USER, Syslog.facility) # block param = nil Syslog.open { |param| } - assert_equal(sl, param) + assert_equal(Syslog, param) ensure - sl.close + Syslog.close end def test_opened? - sl = Syslog.instance - assert_equal(false, sl.opened?) + assert_equal(false, Syslog.opened?) - sl.open - assert_equal(true, sl.opened?) + Syslog.open + assert_equal(true, Syslog.opened?) - sl.close - assert_equal(false, sl.opened?) + Syslog.close + assert_equal(false, Syslog.opened?) - sl.open { - assert_equal(true, sl.opened?) + Syslog.open { + assert_equal(true, Syslog.opened?) } - assert_equal(false, sl.opened?) + assert_equal(false, Syslog.opened?) end def test_mask - sl = Syslog.open + Syslog.open - orig = sl.mask + orig = Syslog.mask - sl.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR) - assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), sl.mask) + Syslog.mask = Syslog.LOG_UPTO(Syslog::LOG_ERR) + assert_equal(Syslog.LOG_UPTO(Syslog::LOG_ERR), Syslog.mask) - sl.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT) - assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), sl.mask) + Syslog.mask = Syslog.LOG_MASK(Syslog::LOG_CRIT) + assert_equal(Syslog.LOG_MASK(Syslog::LOG_CRIT), Syslog.mask) - sl.mask = orig + Syslog.mask = orig ensure - sl.close + Syslog.close end def test_log