about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-09-03 19:25:39 -0700
committerEric Wong <normalperson@yhbt.net>2009-09-03 21:04:31 -0700
commitb32d1da1d760f2e193b293af6dde9da272a85e8d (patch)
tree3bc5627bf6ddd4df796a6164ad510127f4b62dfc
parentaf12d387069d017494daa23534fa7f87aea9ff3b (diff)
downloadclogger-b32d1da1d760f2e193b293af6dde9da272a85e8d.tar.gz
This allows overriding the default of "\n".  Behavior remains
similar to IO#puts, the :ORS (output record separator) is
appended iff the format doesn't already end with that string.
-rw-r--r--ext/clogger_ext/clogger.c2
-rw-r--r--lib/clogger.rb8
-rw-r--r--lib/clogger/pure.rb2
-rw-r--r--test/test_clogger.rb16
4 files changed, 23 insertions, 5 deletions
diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index de58a9e..fa8f9f6 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -674,7 +674,7 @@ static VALUE clogger_init(int argc, VALUE *argv, VALUE self)
         }
 
         init_buffers(c);
-        c->fmt_ops = rb_funcall(self, rb_intern("compile_format"), 1, fmt);
+        c->fmt_ops = rb_funcall(self, rb_intern("compile_format"), 2, fmt, o);
 
         if (Qtrue == rb_funcall(self, rb_intern("need_response_headers?"),
                                 1, c->fmt_ops))
diff --git a/lib/clogger.rb b/lib/clogger.rb
index fbcd220..7366d35 100644
--- a/lib/clogger.rb
+++ b/lib/clogger.rb
@@ -48,8 +48,9 @@ private
                         time_(?:utc|local)\{[^\}]+\}|
                         \w*))?([^$]*)/x
 
-  def compile_format(str)
+  def compile_format(str, opt = {})
     rv = []
+    opt ||= {}
     str.scan(SCAN).each do |pre,tok,post|
       rv << [ OP_LITERAL, pre ] if pre && pre != ""
 
@@ -97,8 +98,9 @@ private
     # auto-append a newline
     last = rv.last or return rv
     op = last.first
-    if (op == OP_LITERAL && /\n\z/ !~ last.last) || op != OP_LITERAL
-      rv << [ OP_LITERAL, "\n" ]
+    ors = opt[:ORS] || "\n"
+    if (op == OP_LITERAL && /#{ors}\z/ !~ last.last) || op != OP_LITERAL
+      rv << [ OP_LITERAL, ors ] if ors.size > 0
     end
 
     rv
diff --git a/lib/clogger/pure.rb b/lib/clogger/pure.rb
index 57e727b..108d036 100644
--- a/lib/clogger/pure.rb
+++ b/lib/clogger/pure.rb
@@ -9,7 +9,7 @@ class Clogger
     @app = app
     @logger = opts[:logger]
     (@logger.sync = true) rescue nil
-    @fmt_ops = compile_format(opts[:format] || Format::Common)
+    @fmt_ops = compile_format(opts[:format] || Format::Common, opts)
     @wrap_body = need_wrap_body?(@fmt_ops)
     @reentrant = nil
     @body_bytes_sent = 0
diff --git a/test/test_clogger.rb b/test/test_clogger.rb
index d3e315e..71dbad8 100644
--- a/test/test_clogger.rb
+++ b/test/test_clogger.rb
@@ -453,4 +453,20 @@ class TestClogger < Test::Unit::TestCase
     assert_nothing_raised { Clogger.new(app, :logger => logger) }
   end
 
+  def test_clogger_no_ORS
+    s = ''
+    app = lambda { |env| [302, [ %w(a) ], []] }
+    cl = Clogger.new(app, :logger => s, :format => "$request", :ORS => "")
+    cl.call(@req)
+    assert_equal "GET /hello?goodbye=true HTTP/1.0", s
+  end
+
+  def test_clogger_weird_ORS
+    s = ''
+    app = lambda { |env| [302, [ %w(a) ], []] }
+    cl = Clogger.new(app, :logger => s, :format => "<$request", :ORS => ">")
+    cl.call(@req)
+    assert_equal "<GET /hello?goodbye=true HTTP/1.0>", s
+  end
+
 end