Sha256: e1635a72470a1ed79263c10031e0ed1b686135ce804daa7e125d4e57ddd5656b

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module Log2mail

  class File

    require_relative 'file/parser'
    include Parser


    # FIXME: redundant
    def log(msg, sev = ::Logger::DEBUG)
      $logger.log sev, '%s: %s%s  [%s]' % [@path, msg, $/, caller.first]
    end

    def warn(msg)
      log(msg, ::Logger::WARN)
    end

    attr_reader :path, :patterns

    def initialize( path, patterns, maxbufsize = 65536 )
      @path = path
      self.patterns = patterns
      @maxbufsize = maxbufsize
      log "Maximum buffer size: #{@maxbufsize}"
      log "Patterns: #{patterns.inspect}"
    end

    def patterns=(patterns)
      @patterns = patterns.map(&:to_r)
    end

    # ----------------------------
    # - File management (public) -
    # ----------------------------

    def open
      @f = ::File.open(@path, 'r', :encoding => "BINARY")
      log "file opened"
      @ino = @f.stat.ino
      @size = 0
      @f
    rescue Errno::ENOENT
      warn "does not exist"
      false
    end

    def seek_to_end
      @f.seek(0, IO::SEEK_END)
      @size = @f.stat.size
      @f
    end

    def read_to_end
      return unless @f
      s = @f.gets(nil)
      @size += s.length if s
      s
    end

    def eof?
      !@f or @f.eof?
    end

    def rotated?
      if inum_changed?
        log "inode number changed"
        true
      elsif file_size_changed?
        log "file size changed; probably truncated"
        true
      else
        false
      end
    end

    private

    # -------------------
    # - File management -
    # -------------------

    def inum_changed?
      ::File.stat(@path).ino != @ino
    rescue Errno::ENOENT
      true
    end

    def file_size_changed?
      @f.stat.size != @size
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log2mail-0.0.1.pre4 lib/log2mail/file.rb
log2mail-0.0.1.pre3 lib/log2mail/file.rb