Nobuyoshi,
It's defined in FakeTime module which is prepended to Time.
Thank you for being so polite and taking the time to respond. That was a very dumb mistake on my part. I reached that conclusion when I was fighting a poor, old, MinGW embedded ruby 2.2.4 build in a commercial app. It was nice when I swapped in my build of 2.3 stable, and everything worked...
Anyway, I believe I've found the real issue. It may be occurring because I'm using Win7, or possibly because I have UAC enabled.
On my system, File.utime only sets atime, not mtime. I suspect that the OS may not allow setting mtime. As you probably know, LogDevice.new
uses mtime to determine whether it's time to create a new log file.
Normally, I can't see how that would be an issue, but re this test trying to mock things, it is.
If you have a minute, could you check the following code? For a MinGW build, it changes LogDevice to use atime to determine whether to create a new log.
require 'logger'
module PLogDevice
def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil)
@dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
mon_initialize
set_dev(log)
if @filename
@shift_age = shift_age || 7
@shift_size = shift_size || 1048576
@shift_period_suffix = shift_period_suffix || '%Y%m%d'
unless @shift_age.is_a?(Integer)
base_time = @dev.respond_to?(:stat) ?
(RUBY_PLATFORM !~ /mingw/ ? @dev.stat.mtime : @dev.stat.atime) :
# @dev.stat.mtime :
Time.now
@next_rotate_time = next_rotate_time(base_time, @shift_age)
end
end
end
end
class Logger::LogDevice ; prepend PLogDevice ; end
module FakeTime ; attr_accessor :now ; end
class << Time ; prepend FakeTime ; end
log = "log"
File.open(log, "w") {}
File.utime(*[Time.mktime(2014, 1, 2, 0, 0, 0)]*2, log)
Time.now = Time.mktime(2014, 1, 2, 23, 59, 59, 999000)
dev = Logger::LogDevice.new(log, shift_age: 'daily')
dev.write("#{Time.now} hello-1\n")
dev.close
puts "\nTime.now #{Time.now}\n" \
"atime #{File.atime(log)}\n" \
"mtime #{File.mtime(log)}\n"
Time.now = Time.mktime(2014, 1, 3, 1, 1, 1)
dev = Logger::LogDevice.new(log, shift_age: 'daily')
dev.write("#{Time.now} hello-2\n")
dev.close
puts "\nTime.now #{Time.now}\n" \
"atime #{File.atime(log)}\n" \
"mtime #{File.mtime(log)}\n"
As listed, the logs are written correctly to the same folder the script is in. If you uncomment
# @dev.stat.mtime :
and comment
(RUBY_PLATFORM !~ /mingw/ ? @dev.stat.mtime : @dev.stat.atime) :
it behaves as normal (using mtime) and will fail (appending to the log file, not creating another one).
Hence, I don't believe there's an issue with logger, it's an issue with trying to mock this.
If adding the prepend to LogDevice looks okay, someone should do a PR or commit.
Again, thanks for all your work, both Ruby in general and windows issues.