From e8c00b7027f1bb0a574f982f7e16ecaa6be2757a Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 27 Oct 2015 03:00:38 +0000 Subject: [PATCH] logger.rb: fix weekly rotation * lib/logger.rb (Logger::Period#next_rotate_time): get rid of adding to mday not to exceed the days of the month. [ruby-core:71185] [Bug #11620] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ lib/logger.rb | 8 +++++--- test/logger/test_logdevice.rb | 32 +++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 95a87063d5..b773c88ce0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Oct 27 12:00:33 2015 Nobuyoshi Nakada + + * lib/logger.rb (Logger::Period#next_rotate_time): get rid of + adding to mday not to exceed the days of the month. + [ruby-core:71185] [Bug #11620] + Mon Oct 26 22:43:03 2015 yui-knk * test/ruby/test_module.rb (test_method_defined): Add test cases diff --git a/lib/logger.rb b/lib/logger.rb index bb4953e437..354d5aff71 100644 --- a/lib/logger.rb +++ b/lib/logger.rb @@ -539,12 +539,14 @@ private t = Time.mktime(now.year, now.month, now.mday) + SiD * (7 - now.wday) when 'monthly' t = Time.mktime(now.year, now.month, 1) + SiD * 31 - mday = (1 if t.mday > 1) + return Time.mktime(t.year, t.month, 1) if t.mday > 1 else return now end - if mday or t.hour.nonzero? or t.min.nonzero? or t.sec.nonzero? - t = Time.mktime(t.year, t.month, mday || (t.mday + (t.hour > 12 ? 1 : 0))) + if t.hour.nonzero? or t.min.nonzero? or t.sec.nonzero? + hour = t.hour + t = Time.mktime(t.year, t.month, t.mday) + t += SiD if hour > 12 end t end diff --git a/test/logger/test_logdevice.rb b/test/logger/test_logdevice.rb index becf53191d..bdd6e35b01 100644 --- a/test/logger/test_logdevice.rb +++ b/test/logger/test_logdevice.rb @@ -365,6 +365,8 @@ class TestLogDevice < Test::Unit::TestCase end end + env_tz_works = /linux|darwin|freebsd/ =~ RUBY_PLATFORM # borrow from test/ruby/test_time_tz.rb + def test_shifting_dst_change Dir.mktmpdir do |tmpdir| assert_in_out_err([{"TZ"=>"Europe/London"}, *%W"--disable=gems -rlogger -C#{tmpdir} -"], <<-'end;') @@ -401,7 +403,35 @@ class TestLogDevice < Test::Unit::TestCase assert_not_match(/hello-1/, cont) assert_file.exist?(log+".20140330") end - end if /linux|darwin|freebsd/ =~ RUBY_PLATFORM # borrow from test/ruby/test_time_tz.rb + end if env_tz_works + + def test_shifting_weekly_dst_change + Dir.mktmpdir do |tmpdir| + assert_separately([{"TZ"=>"Europe/London"}, *%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.mktime(2015, 10, 25, 0, 1, 1) + dev = Logger::LogDevice.new("log", shift_age: 'weekly') + dev.write("#{Time.now} hello-1\n") + ensure + dev.close if dev + end + end; + log = File.join(tmpdir, "log") + cont = File.read(log) + assert_match(/hello-1/, cont) + end + end if env_tz_works private