Class: LogWeaver::CombinedLog

Inherits:
Object
  • Object
show all
Defined in:
lib/log_weaver/combined_log.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logs) ⇒ CombinedLog

Returns a new instance of CombinedLog.



6
7
8
9
# File 'lib/log_weaver/combined_log.rb', line 6

def initialize(logs)
  @logs = logs
  @index = CombinedLog::build_index(@logs)
end

Instance Attribute Details

#logsObject

Returns the value of attribute logs.



4
5
6
# File 'lib/log_weaver/combined_log.rb', line 4

def logs
  @logs
end

Class Method Details

.build_index(logs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/log_weaver/combined_log.rb', line 11

def self.build_index(logs)
  # need to sort by timestamp, then prefix
  index = {}
  logs.each do |log|
    log.lines.each do |t,l|
      key = CombinedLogIndexKey.new(log.prefix, t)
      index[key] = l
    end
  end
  #TODO: sorting at this point may have seriously bad performance for large logs; consider a
  # data structure that stays sorted as you insert
  Hash[index.sort]
end

Instance Method Details

#to_sObject



25
26
27
28
29
30
31
32
33
# File 'lib/log_weaver/combined_log.rb', line 25

def to_s
  res = ""
  @index.each do |key, lines|
    lines.each do |l|
      res << "#{key.prefix}#{l}\n"
    end
  end
  res
end