Class: LogMerge::Merger
- Inherits:
-
Object
- Object
- LogMerge::Merger
- Defined in:
- lib/logmerge/merger.rb
Overview
Merges multiple Apache log files by date.
Class Method Summary collapse
-
.merge(output = STDOUT, *files) ⇒ Object
Merges
files
intooutput
.
Instance Method Summary collapse
-
#all_closed? ⇒ Boolean
Are all the streams closed?.
-
#buf_fill(slot) ⇒ Object
Adds a line to the buffer from input strem
slot
. -
#initialize(streams) ⇒ Merger
constructor
Creates a new Merger that will operate on an array of IO-like objects
streams
. -
#merge(out_stream) ⇒ Object
Merges streams into
out_stream
.
Constructor Details
#initialize(streams) ⇒ Merger
Creates a new Merger that will operate on an array of IO-like objects streams
.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/logmerge/merger.rb', line 30 def initialize(streams) @streams = streams @buf = [] # The next line causes failure when a stream is empty in the # initial call to buf_fill. When @streams[slot].eof? == true buf_fill # removes the element from from @streams and @buf causing the each_index # iterator to terminate early. # # @streams.each_index { |slot| buf_fill slot } # # Replace by # # @streams.size.downto(1) { |slot| buf_fill slot-1 } # # which removes empty streams from the end of the @streams and @buf arrays # making sure we iterate over all elements of @streams and @buf. @streams.size.downto(1) { |slot| buf_fill slot-1 } @all_closed = @streams.empty? end |
Class Method Details
.merge(output = STDOUT, *files) ⇒ Object
Merges files
into output
. If files
is nil, ARGV is used.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/logmerge/merger.rb', line 14 def self.merge(output = STDOUT, *files) files = ARGV if files.empty? merger = LogMerge::Merger.new files.map { |file| if file =~ /\.gz$/ Zlib::GzipReader.open file else File.open file end } merger.merge output end |
Instance Method Details
#all_closed? ⇒ Boolean
Are all the streams closed?
56 57 58 |
# File 'lib/logmerge/merger.rb', line 56 def all_closed? @all_closed end |
#buf_fill(slot) ⇒ Object
Adds a line to the buffer from input strem slot
.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/logmerge/merger.rb', line 63 def buf_fill(slot) line = @streams[slot].gets if line.nil? and @streams[slot].eof? then @streams.delete_at slot @buf.delete_at slot elsif line.nil? then raise "WTF? line.nil? and not eof?" else line =~ / \[(.+?)\] / time = Time.parse($1.sub(':', ' ').gsub('/', '-')).to_i @buf[slot] = [time, line] end end |
#merge(out_stream) ⇒ Object
Merges streams into out_stream
.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/logmerge/merger.rb', line 81 def merge(out_stream) index, min, line = nil until @streams.empty? do min = @buf.min { |a,b| a.first <=> b.first} slot = @buf.index min buf_fill slot out_stream.puts min.last end @all_closed = true end |