Class: Covet::LogCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/covet/log_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LogCollection



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#flushesObject (readonly)

Returns the value of attribute flushes.



5
6
7
# File 'lib/covet/log_collection.rb', line 5

def flushes
  @flushes
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/covet/log_collection.rb', line 5

def size
  @size
end

Instance Method Details

#<<(logs) ⇒ Object Also known as: append



16
17
18
19
20
21
22
23
24
25
26
# 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

#finish!Object



29
30
31
32
33
34
35
36
# 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