From b32d1da1d760f2e193b293af6dde9da272a85e8d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 3 Sep 2009 19:25:39 -0700 Subject: add output record separator option (:ORS) 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. --- ext/clogger_ext/clogger.c | 2 +- lib/clogger.rb | 8 +++++--- lib/clogger/pure.rb | 2 +- test/test_clogger.rb | 16 ++++++++++++++++ 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 "", s + end + end -- cgit v1.2.3-24-ge0c7