class MySampler::FileRotating

Constants

SECOND

Attributes

header[RW]

Public Class Methods

new(params, *args) { |f| ... } click to toggle source
# File lib/mysampler/file.rb, line 11
def initialize (params, *args)
  @interval = params[:interval] || DAY
  @header   = params[:header]   || nil  # a header to put at the top of every file
  @args = args
  @root_fn = args[0]
  @stamp = get_date_stamp

  open_local do |f|
    f.puts @header if @header
    if block_given? 
      return yield f
    else 
      return f
    end
  end
end

Public Instance Methods

close() click to toggle source
# File lib/mysampler/file.rb, line 28
def close 
  if @f
    @f.flock(File::LOCK_UN)
    @f.close 
  end
end

Private Instance Methods

get_date_stamp() click to toggle source
# File lib/mysampler/file.rb, line 54
def get_date_stamp
  format = case @interval
    when YEAR   then '%Y'
    when MONTH  then '%Y%m'
    when DAY    then '%Y%m%d'
    when HOUR   then '%Y%m%d%H'
    when MINUTE then '%Y%m%d%H%M'
    when SECOND then '%Y%m%d%H%M%S'
    else raise "Invalid interval"
  end
  return Time.now.strftime(format)
end
method_missing(method, *args, &block) click to toggle source
# File lib/mysampler/file.rb, line 67
def method_missing(method, *args, &block)
  # check to see if we need to reopen
  stamp = get_date_stamp

  if @stamp != stamp
    @stamp = stamp
    close 
    open_local 
    @f.puts @header if @header
  end
  return @f.send(method, *args, &block)       
end
open_local() { |self| ... } click to toggle source
# File lib/mysampler/file.rb, line 36
  def open_local
    fn = sprintf("%s.%s",@root_fn,@stamp)
    args = @args
    args[0] = fn
#    puts fn

    begin
      @f = File.open(*args) 
      @f.flock(File::LOCK_EX) if @f
      if block_given?
        return yield self
      else
        return self
      end
    end
  end