module Log

  def Log::each which
    date = Date.new
    ARGF.each_line do |line|
      fields = line.split(/ /, 12)
      r = {}
      which.each do | field |
        case field 
        when :host then r[:host] = fields[0]
        when :date then r[:date] = fields[3][1 .. -1]
        when :year, :month, :day, :hour then
          date.parse fields[3][1 .. -1]
          r[field] = date[field]
        when :resource then r[:resource] = fields[6]
        when :status then r[:status] = fields[8].to_i
        when :size then r[:size] = fields[9].to_i
        end
      end
      yield r
    end
  end
    
  class Date
    def init
      @last = nil
    end
    def parse(str)
      if str != @last
        
        if (str =~ %r{(\d\d)/(...)/(\d\d\d\d):(\d\d)})
          @day = "#{$1}/#{$2}/#{$3}"
          @year = $3
          @hour = $4
        end
        @last = str
      end
    end
      
    def [] which
      case which
      when :day then @day
      when :year then @year
      when :hour then @hour
      end
    end
  end
end