about summary refs log tree commit homepage
path: root/ext
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-24 13:23:32 -0800
committerEric Wong <normalperson@yhbt.net>2010-12-24 13:28:57 -0800
commit9b46379f75f384c86e42046ab03ce55231197c92 (patch)
tree33ffd8703db04f1bc299df03f12673fda2f888c7 /ext
parent8d58b42d0255880d732ba0700597b312a8219f8f (diff)
downloadclogger-9b46379f75f384c86e42046ab03ce55231197c92.tar.gz
This lessens confusion for people configuring Clogger in
config.ru, since "File" could be mistaken for Rack::File
and "::File" needs to be specified.
Diffstat (limited to 'ext')
-rw-r--r--ext/clogger_ext/clogger.c35
1 files changed, 25 insertions, 10 deletions
diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index ad5bbb9..09eeffd 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -213,11 +213,6 @@ static VALUE obj_fileno(VALUE obj)
         return rb_funcall(obj, rb_intern("fileno"), 0);
 }
 
-static VALUE obj_enable_sync(VALUE obj)
-{
-        return rb_funcall(obj, rb_intern("sync="), 1, Qtrue);
-}
-
 /* only for writing to regular files, not stupid crap like NFS  */
 static void write_full(int fd, const void *buf, size_t count)
 {
@@ -588,12 +583,35 @@ static VALUE cwrite(struct clogger *c)
         return Qnil;
 }
 
+static void init_logger(struct clogger *c, VALUE path)
+{
+        ID id;
+
+        if (!NIL_P(path) && !NIL_P(c->logger))
+                rb_raise(rb_eArgError, ":logger and :path are independent");
+        if (!NIL_P(path)) {
+                VALUE ab = rb_str_new2("ab");
+                id = rb_intern("open");
+                c->logger = rb_funcall(rb_cFile, id, 2, path, ab);
+        }
+
+        id = rb_intern("sync=");
+        if (rb_respond_to(c->logger, id))
+                rb_funcall(c->logger, id, 1, Qtrue);
+
+        id = rb_intern("fileno");
+        if (rb_respond_to(c->logger, id))
+                c->fd = raw_fd(rb_funcall(c->logger, id, 0));
+}
+
 /**
  * call-seq:
  *   Clogger.new(app, :logger => $stderr, :format => string) => obj
  *
  * Creates a new Clogger object that wraps +app+.  +:logger+ may
  * be any object that responds to the "<<" method with a string argument.
+ * If +:logger:+ is a string, it will be treated as a path to a
+ * File that will be opened in append mode.
  */
 static VALUE clogger_init(int argc, VALUE *argv, VALUE self)
 {
@@ -609,12 +627,9 @@ static VALUE clogger_init(int argc, VALUE *argv, VALUE self)
         if (TYPE(o) == T_HASH) {
                 VALUE tmp;
 
+                tmp = rb_hash_aref(o, ID2SYM(rb_intern("path")));
                 c->logger = rb_hash_aref(o, ID2SYM(rb_intern("logger")));
-                if (!NIL_P(c->logger)) {
-                        rb_rescue(obj_enable_sync, c->logger, NULL, 0);
-                        tmp = rb_rescue(obj_fileno, c->logger, NULL, 0);
-                        c->fd = raw_fd(tmp);
-                }
+                init_logger(c, tmp);
 
                 tmp = rb_hash_aref(o, ID2SYM(rb_intern("format")));
                 if (!NIL_P(tmp))