clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: clogger@librelist.org
Subject: [PATCH] avoid calling "<<" on env["rack.errors"]
Date: Sat, 3 Nov 2012 03:12:50 +0000	[thread overview]
Message-ID: <20121103031250.GA21670@dcvr.yhbt.net> (raw)
In-Reply-To: <20121103031250.GA21670@dcvr.yhbt.net>

Rack::Lint::ErrorWrapper forbids the "<<" method.  This
fallback only comes into play when no log destination
(via :logger or :path) is specified and is rarely an
issue in real setups.
---
 Pushed to 'master' on git://bogomips.org/clogger

 ext/clogger_ext/clogger.c |  9 +++++++--
 lib/clogger/pure.rb       | 10 +++++++++-
 test/test_clogger.rb      | 17 +++++++++++++++++
 3 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index 857ed9a..04359f9 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -119,6 +119,7 @@ struct clogger {
 	int reentrant; /* tri-state, -1:auto, 1/0 true/false */
 };
 
+static ID write_id;
 static ID ltlt_id;
 static ID call_id;
 static ID each_id;
@@ -687,9 +688,12 @@ static VALUE cwrite(struct clogger *c)
 	} else {
 		VALUE logger = c->logger;
 
-		if (NIL_P(logger))
+		if (NIL_P(logger)) {
 			logger = rb_hash_aref(c->env, g_rack_errors);
-		rb_funcall(logger, ltlt_id, 1, dst);
+                        rb_funcall(logger, write_id, 1, dst);
+                } else {
+			rb_funcall(logger, ltlt_id, 1, dst);
+		}
 	}
 
 	return Qnil;
@@ -1036,6 +1040,7 @@ void Init_clogger_ext(void)
 
 	check_clock();
 
+	write_id = rb_intern("write");
 	ltlt_id = rb_intern("<<");
 	call_id = rb_intern("call");
 	each_id = rb_intern("each");
diff --git a/lib/clogger/pure.rb b/lib/clogger/pure.rb
index 24392e7..44f4e62 100644
--- a/lib/clogger/pure.rb
+++ b/lib/clogger/pure.rb
@@ -161,7 +161,7 @@ def time_format(sec, usec, format, div)
   end
 
   def log(env, status, headers)
-    (@logger || env['rack.errors']) << @fmt_ops.map { |op|
+    str = @fmt_ops.map { |op|
       case op[0]
       when OP_LITERAL; op[1]
       when OP_REQUEST; byte_xs(env[op[1]] || "-")
@@ -182,5 +182,13 @@ def log(env, status, headers)
         raise "EDOOFUS #{op.inspect}"
       end
     }.join('')
+
+    l = @logger
+    if l
+      l << str
+    else
+      env['rack.errors'].write(str)
+    end
+    nil
   end
 end
diff --git a/test/test_clogger.rb b/test/test_clogger.rb
index 10c587a..9bff918 100644
--- a/test/test_clogger.rb
+++ b/test/test_clogger.rb
@@ -837,4 +837,21 @@ def test_full_uri
     expect = "\"GET http://example.com/hello?goodbye=true HTTP/1.0\"\n"
     assert_equal [ expect ], s
   end
+
+  def test_lint_error_wrapper
+    require 'rack/lobster'
+    @req["SERVER_NAME"] = "FOO"
+    @req["SERVER_PORT"] = "666"
+    @req["rack.version"] = [1,1]
+    @req["rack.multithread"] = true
+    @req["rack.multiprocess"] = true
+    @req["rack.run_once"] = false
+    app = Rack::ContentLength.new(Rack::ContentType.new(Rack::Lobster.new))
+    cl = Clogger.new(app, format: :Combined)
+    @req["rack.errors"] = err = StringIO.new
+    status, headers, body = r = Rack::Lint.new(cl).call(@req)
+    body.each { |x| assert_kind_of String, x.to_str }
+    body.close # might raise here
+    assert_match(%r{GET /hello}, err.string)
+  end
 end
-- 
Eric Wong


           reply	other threads:[~2012-11-03  3:13 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20121103031250.GA21670@dcvr.yhbt.net>]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/clogger/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121103031250.GA21670@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=clogger@librelist.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/clogger.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).