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)) ⇒ LogMerge

Returns a new instance of LogMerge.



72
73
74
75
76
# File 'lib/logmerge.rb', line 72

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

Instance Attribute Details

#endTimeObject (readonly)

Returns the value of attribute endTime.



71
72
73
# File 'lib/logmerge.rb', line 71

def endTime
  @endTime
end

#logsObject (readonly)

Returns the value of attribute logs.



71
72
73
# File 'lib/logmerge.rb', line 71

def logs
  @logs
end

#startTimeObject (readonly)

Returns the value of attribute startTime.



71
72
73
# File 'lib/logmerge.rb', line 71

def startTime
  @startTime
end

Instance Method Details

#mergeObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/logmerge.rb', line 78

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]
    puts Colored.colorize(min_log.take, foreground: color)
  end
end