From 6df21cb46945250439a82f515b2971a1b6ae8f21 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 27 Oct 2015 07:18:14 +0000 Subject: [PATCH] logger.rb: end of week should be Saturday * lib/logger.rb (Logger::Period#previous_period_end): as weekly rotation shifts the log file on Sundays, the end date of the previous period should be Saturdays. fix r45072. [ruby-dev:49314] [Bug #11622] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52297 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 ++++++ lib/logger.rb | 2 +- test/logger/test_logdevice.rb | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index bf3c23f58e..eb6dc638ee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Oct 27 16:18:12 2015 Nobuyoshi Nakada + + * lib/logger.rb (Logger::Period#previous_period_end): as weekly + rotation shifts the log file on Sundays, the end date of the + previous period should be Saturdays. fix r45072. + [ruby-dev:49314] [Bug #11622] + Tue Oct 27 16:12:37 2015 NARUSE, Yui * vm_dump.c (rb_print_backtrace): our addr2line doesn't work on sparc. diff --git a/lib/logger.rb b/lib/logger.rb index 354d5aff71..f15a85077e 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -556,7 +556,7 @@ private when 'daily' t = Time.mktime(now.year, now.month, now.mday) - SiD / 2 when 'weekly' - t = Time.mktime(now.year, now.month, now.mday) - (SiD * (now.wday + 1) + SiD / 2) + t = Time.mktime(now.year, now.month, now.mday) - (SiD * now.wday + SiD / 2) when 'monthly' t = Time.mktime(now.year, now.month, 1) - SiD / 2 else diff --git a/test/logger/test_logdevice.rb b/test/logger/test_logdevice.rb index bdd6e35b01..ff39ebde1e 100644 --- a/test/logger/test_logdevice.rb +++ b/test/logger/test_logdevice.rb @@ -367,6 +367,49 @@ class TestLogDevice < Test::Unit::TestCase env_tz_works = /linux|darwin|freebsd/ =~ RUBY_PLATFORM # borrow from test/ruby/test_time_tz.rb + def test_shifting_weekly + Dir.mktmpdir do |tmpdir| + assert_in_out_err([{"TZ"=>"UTC"}, *%W"-rlogger -C#{tmpdir} -"], <<-'end;') + begin + module FakeTime + attr_accessor :now + end + + class << Time + prepend FakeTime + end + + log = "log" + File.open(log, "w") {} + + Time.now = Time.utc(2015, 12, 14, 0, 1, 1) + dev = Logger::LogDevice.new("log", shift_age: 'weekly') + + Time.now = Time.utc(2015, 12, 19, 12, 34, 56) + dev.write("#{Time.now} hello-1\n") + File.utime(Time.now, Time.now, log) + + Time.now = Time.utc(2015, 12, 20, 0, 1, 1) + File.utime(Time.now, Time.now, log) + dev.write("#{Time.now} hello-2\n") + ensure + dev.close if dev + end + end; + log = File.join(tmpdir, "log") + cont = File.read(log) + assert_match(/hello-2/, cont) + assert_not_match(/hello-1/, cont) + log = Dir.glob(log+".*") + assert_equal(1, log.size) + log, = *log + cont = File.read(log) + assert_match(/hello-1/, cont) + assert_equal("2015-12-19", cont[/^[-\d]+/]) + assert_equal("20151219", log[/\d+\z/]) + end + end if env_tz_works + def test_shifting_dst_change Dir.mktmpdir do |tmpdir| assert_in_out_err([{"TZ"=>"Europe/London"}, *%W"--disable=gems -rlogger -C#{tmpdir} -"], <<-'end;')