diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-11-27 08:44:04 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-11-27 08:44:04 +0000 |
commit | c7c42db58fd69e91ef5f7293ac1568b58dd81dc5 (patch) | |
tree | 848915584f4fe8a631208692c328f1c77f75ccb4 /ext/syslog | |
parent | 252da304c5cb27edd0724ebf913cc197e30e29b7 (diff) | |
download | ruby-c7c42db58fd69e91ef5f7293ac1568b58dd81dc5.tar.gz ruby-c7c42db58fd69e91ef5f7293ac1568b58dd81dc5.tar.xz ruby-c7c42db58fd69e91ef5f7293ac1568b58dd81dc5.zip |
* ext/syslog/syslog.c: Cut redundancy.
* ext/syslog/syslog.c: Do not leak ident.
* ext/syslog/syslog.c, ext/syslog/test.rb: Syslog.close should
raise RuntimeError when not opened.
* ext/syslog/syslog.c, ext/syslog/test.rb:
Syslog.{ident,options,facility,mask} should all return nil when
not opened.
* ext/syslog/syslog.c, ext/syslog/test.rb: Change back the output
format of inspect().
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@3095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/syslog')
-rw-r--r-- | ext/syslog/syslog.c | 63 |
1 files changed, 41 insertions, 22 deletions
diff --git a/ext/syslog/syslog.c b/ext/syslog/syslog.c index 98fb5ae4f..3e86569f0 100644 --- a/ext/syslog/syslog.c +++ b/ext/syslog/syslog.c @@ -37,7 +37,15 @@ static void syslog_write(int pri, int argc, VALUE *argv) /* Syslog module methods */ static VALUE mSyslog_close(VALUE self) { + 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; @@ -51,26 +59,33 @@ static VALUE mSyslog_open(int argc, VALUE *argv, VALUE self) 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"); } - if (NIL_P(opt)) { - opt = INT2NUM(LOG_PID | LOG_CONS); - } - if (NIL_P(fac)) { - fac = INT2NUM(LOG_USER); - } - #ifdef SafeStringValue SafeStringValue(ident); #else Check_SafeStr(ident); #endif - syslog_ident = (const char *)strdup(RSTRING(ident)->ptr); - syslog_options = NUM2INT(opt); - syslog_facility = NUM2INT(fac); + syslog_ident = strdup(RSTRING(ident)->ptr); + + 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)); @@ -97,22 +112,22 @@ static VALUE mSyslog_isopen(VALUE self) static VALUE mSyslog_ident(VALUE self) { - return rb_str_new2(syslog_ident); + return syslog_opened ? rb_str_new2(syslog_ident) : Qnil; } static VALUE mSyslog_options(VALUE self) { - return INT2NUM(syslog_options); + return syslog_opened ? INT2NUM(syslog_options) : Qnil; } static VALUE mSyslog_facility(VALUE self) { - return INT2NUM(syslog_facility); + return syslog_opened ? INT2NUM(syslog_facility) : Qnil; } static VALUE mSyslog_get_mask(VALUE self) { - return INT2NUM(syslog_mask); + return syslog_opened ? INT2NUM(syslog_mask) : Qnil; } static VALUE mSyslog_set_mask(VALUE self, VALUE mask) @@ -150,14 +165,18 @@ static VALUE mSyslog_inspect(VALUE self) { char buf[1024]; - snprintf(buf, sizeof(buf), - "<#%s: ident=\"%s\", options=%d, facility=%d, mask=%d%s>", - rb_class2name(self), - syslog_ident, - syslog_options, - syslog_facility, - syslog_mask, - syslog_opened ? ", opened" : ""); + 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); } |