Class: LogMerge

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, startTime = Time.at(0), endTime = Time.at(2000000000000), autotag = false) ⇒ LogMerge

Returns a new instance of LogMerge.



132
133
134
135
136
# File 'lib/logmerge.rb', line 132

def initialize(files, startTime=Time.at(0), endTime=Time.at(2000000000000), autotag=false)
  @logs = files.map {|f| Log.new(f, autotag)}
  @startTime = DateTime.parse(startTime.to_s)
  @endTime = DateTime.parse(endTime.to_s)
end

Instance Attribute Details

#endTimeObject (readonly)

Returns the value of attribute endTime.



131
132
133
# File 'lib/logmerge.rb', line 131

def endTime
  @endTime
end

#logsObject (readonly)

Returns the value of attribute logs.



131
132
133
# File 'lib/logmerge.rb', line 131

def logs
  @logs
end

#startTimeObject (readonly)

Returns the value of attribute startTime.



131
132
133
# File 'lib/logmerge.rb', line 131

def startTime
  @startTime
end

Instance Method Details

#applyColor(loglines, color) ⇒ Object



156
157
158
# File 'lib/logmerge.rb', line 156

def applyColor(loglines, color)
  loglines.map{|l| Colored.colorize(l, foreground: color)}
end

#mergeObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/logmerge.rb', line 138

def merge
  colors = Colored::COLORS.keys.dup
  colors.delete('black')
  while true
    min_log = logs.select {|l| !l.finished}.sort { |a,b| a.timestamp && b.timestamp ? a.timestamp <=> b.timestamp : b.timestamp ? -1 : 1 }.first #could be priority queue instead
    break if min_log.nil?
    next min_log.take unless min_log.timestamp
    next min_log.take if min_log.timestamp < startTime
    break if min_log.nil?
    break if min_log.timestamp > endTime
    index = logs.index(min_log)
    color_index = index % colors.size
    color = colors[color_index]
    loglines = min_log.take
    puts applyColor(loglines, color).join("\n")
  end
end