clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Josh Natanson <josh@natanson.net>
To: Eric Wong <e@yhbt.net>
Cc: clogger-public@yhbt.net
Subject: Re: Clogger request_time formatting
Date: Tue, 28 Jul 2020 09:50:33 -0400	[thread overview]
Message-ID: <CAPdx2sz4Cm=yzhRBqC9bwVFRAKh82GEUUHHZ7gquYnsL1F1M7Q@mail.gmail.com> (raw)
In-Reply-To: <20200727183856.GA26578@dcvr>

Thanks.  I had some time to work on this, and I'm a little stuck.  The
changes I've made are working as expected (both new syntax & legacy)
when frozen into a test rails application. I've written a test for the
new syntax, but I'm not getting the same behavior there - it looks to
behave the same as the legacy option.  I can't see why the same code
would behave differently in an app vs. the test, and I'm having some
challenges getting useful debug out of the code in test.  I'm sure I'm
just unfamiliar with something about the gem's layout, but maybe you
have some ideas?

Here's the changes so far.  Don't mind the names, I'm planning to come
up with something better before submitting.

 $ git diff --staged | cat
diff --git a/lib/clogger.rb b/lib/clogger.rb
index be1bdce..608909d 100644
--- a/lib/clogger.rb
+++ b/lib/clogger.rb
@@ -51,7 +51,7 @@ private

   SCAN = /([^$]*)(\$+(?:env\{\w+(?:\.[\w\.]+)?\}|
                         e\{[^\}]+\}|
-                        (?:request_)?time\{\d+\}|
+                        (?:request_)?time\{\d+(?:,\d+)?\}|
                         time_(?:utc|local)\{[^\}]+\}|
                         \w*))?([^$]*)/x

@@ -91,8 +91,10 @@ private
           rv << [ OP_TIME_UTC, fmt, longest_day.strftime(fmt) ]
         when /\A\$time\{(\d+)\}\z/
           rv << [ OP_TIME, *usec_conv_pair(tok, $1.to_i) ]
+        when /\A\$request_time\{(\d+),(\d+)\}\z/
+          rv << [ OP_REQUEST_TIME, *usec_conv_pair(tok, $2.to_i), $1.to_i ]
         when /\A\$request_time\{(\d+)\}\z/
-          rv << [ OP_REQUEST_TIME, *usec_conv_pair(tok, $1.to_i) ]
+          rv << [ OP_REQUEST_TIME, *usec_conv_pair(tok, $1.to_i), 0 ]
         else
           tok_sym = tok[1..-1].to_sym
           if special_code = SPECIAL_VARS[tok_sym]
diff --git a/lib/clogger/pure.rb b/lib/clogger/pure.rb
index fddfe79..8f1f706 100644
--- a/lib/clogger/pure.rb
+++ b/lib/clogger/pure.rb
@@ -166,6 +166,7 @@ private
       when OP_TIME_UTC; Time.now.utc.strftime(op[1])
       when OP_REQUEST_TIME
         t = mono_now - start
+        t = t * (10 ** op[3])
         time_format(t.to_i, (t - t.to_i) * 1000000, op[1], op[2])
       when OP_TIME
         t = Time.now
diff --git a/test/test_clogger.rb b/test/test_clogger.rb
index ca3bd55..3fc9e4a 100644
--- a/test/test_clogger.rb
+++ b/test/test_clogger.rb
@@ -167,10 +167,11 @@ class TestClogger < Test::Unit::TestCase
       ary = compile_format(
         '$remote_addr - $remote_user [$time_local] ' \
         '"$request" $status $body_bytes_sent "$http_referer" ' \
-        '"$http_user_agent" "$http_cookie" $request_time ' \
+        '"$http_user_agent" "$http_cookie" $request_time $request_time{6,0} ' \
         '$env{rack.url_scheme}' \
         "\n")
     }
+
     expect = [
       [ Clogger::OP_REQUEST, "REMOTE_ADDR" ],
       [ Clogger::OP_LITERAL, " - " ],
@@ -190,7 +191,9 @@ class TestClogger < Test::Unit::TestCase
       [ Clogger::OP_LITERAL, "\" \"" ],
       [ Clogger::OP_REQUEST, "HTTP_COOKIE" ],
       [ Clogger::OP_LITERAL, "\" " ],
-      [ Clogger::OP_REQUEST_TIME, '%d.%03d', 1000 ],
+      [ Clogger::OP_REQUEST_TIME, '%d.%03d', 1000, 0],
+      [ Clogger::OP_LITERAL, " " ],
+      [ Clogger::OP_REQUEST_TIME, '%d', 1, 6],
       [ Clogger::OP_LITERAL, " " ],
       [ Clogger::OP_REQUEST, "rack.url_scheme" ],
       [ Clogger::OP_LITERAL, "\n" ],
@@ -721,6 +724,16 @@ class TestClogger < Test::Unit::TestCase
     assert s[-1].to_f <= 0.110
   end

+  def test_request_time_with_multiple
+    s = []
+    app = lambda { |env| sleep(0.1) ; [302, [], [] ] }
+    cl = Clogger.new(app, :logger => s, :format => "$request_time{6,0}")
+    status, headers, body = cl.call(@req)
+    assert_nothing_raised { body.each { |x| } ; body.close }
+    assert s[-1].to_f >= 100000
+    assert s[-1].to_f <= 110000
+  end
+
   def test_insanely_long_time_format
     s = []
     app = lambda { |env| [200, [], [] ] }

On Mon, Jul 27, 2020 at 2:38 PM Eric Wong <e@yhbt.net> wrote:
>
> Josh Natanson <josh@natanson.net> wrote:
> > I'm hoping to find some time this week to work on it.  What's your
> > preferred way to review?
>
> I do everything through email.  "git format-patch" + "git send-email"
> are the recommended tools.  It's the same way git.git and the Linux
> kernel work (see Documentation/SubmittingPatches in git)
>
> https://git.kernel.org/cgit/git/git.git/tree/Documentation/SubmittingPatches
>
> (I don't care about Signed-off-by or real names, though)

  reply	other threads:[~2020-07-28 13:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-09 16:30 Clogger request_time formatting Josh Natanson
2020-07-09 20:28 ` Eric Wong
     [not found]   ` <CAPdx2syEU5WLBx9PezfQ6nFdpnouscXwnm-bg0+t5ERi7EPnJg@mail.gmail.com>
2020-07-09 22:57     ` Eric Wong
2020-07-26  4:46 ` Eric Wong
2020-07-27 15:15   ` Josh Natanson
2020-07-27 18:38     ` Eric Wong
2020-07-28 13:50       ` Josh Natanson [this message]
2020-07-29  9:20         ` Eric Wong
2020-07-29 19:50           ` Josh Natanson
2020-07-30  3:54             ` Eric Wong
2020-07-31 14:08               ` Josh Natanson
2020-07-31 14:09                 ` Josh Natanson
2020-07-31 19:44                 ` Eric Wong
2020-08-02 13:47                   ` Josh Natanson

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='CAPdx2sz4Cm=yzhRBqC9bwVFRAKh82GEUUHHZ7gquYnsL1F1M7Q@mail.gmail.com' \
    --to=josh@natanson.net \
    --cc=clogger-public@yhbt.net \
    --cc=e@yhbt.net \
    /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).