class Covet::LogCollection

Attributes

flushes[R]
size[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/covet/log_collection.rb, line 7
def initialize(options = {})
  @bufsize = options[:bufsize] || 100 # max log buffer size to keep in memory
  @log_file = LogFile.new(:filename => options[:filename], :mode => 'w')
  @buf = []
  @flushes = 0
  @size = 0
end

Public Instance Methods

<<(logs) click to toggle source

@param [Array] logs

# File lib/covet/log_collection.rb, line 16
def <<(logs)
  unless Array === logs
    raise TypeError, "expecting Array, got #{logs.class}"
  end
  @buf << logs
  if @buf.size == @bufsize
    flush!
  end
  @size += 1
  true
end
Also aliased as: append
append(logs)
Alias for: <<
finish!() click to toggle source
# File lib/covet/log_collection.rb, line 29
def finish!
  if @flushes == 0 && @buf.size == 0
    return # avoid writing to file if no collections
  end
  flush! if @buf.any?
  @log_file.write_end
  true
end

Private Instance Methods

flush!() click to toggle source

Flushes buffer to file

# File lib/covet/log_collection.rb, line 41
def flush!
  if @flushes == 0
    @log_file.write_start
  end
  @log_file.write_buf(@buf)
  @buf.clear
  @flushes += 1
end