Class: Log

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Log

Returns a new instance of Log.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/logmerge.rb', line 18

def initialize(path)
  @offset = 0
  if path.include?(':')
    parts = path.split(':')
    path = parts[0]
    @offset = parts[1].to_i
    if parts.size > 2
      @tag = parts[2]
    end
  end
  @file = File.open(path)
  @finished = false
  buffer
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



17
18
19
# File 'lib/logmerge.rb', line 17

def file
  @file
end

#finishedObject

Returns the value of attribute finished.



17
18
19
# File 'lib/logmerge.rb', line 17

def finished
  @finished
end

#lineObject

Returns the value of attribute line.



17
18
19
# File 'lib/logmerge.rb', line 17

def line
  @line
end

#offetObject

Returns the value of attribute offet.



17
18
19
# File 'lib/logmerge.rb', line 17

def offet
  @offet
end

#tagObject

Returns the value of attribute tag.



17
18
19
# File 'lib/logmerge.rb', line 17

def tag
  @tag
end

#timestampObject

Returns the value of attribute timestamp.



17
18
19
# File 'lib/logmerge.rb', line 17

def timestamp
  @timestamp
end

Instance Method Details

#bufferObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logmerge.rb', line 49

def buffer
  begin
    @line = file.readline.strip
    @timestamp = parseTimestamp(line)
    line.prepend("[#{@tag}]") if @tag
  rescue Date::Error
    @timestamp = nil
  end
rescue EOFError
  @line = nil
  @finished = true
  @timestamp = nil
end

#parseTimestamp(line) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/logmerge.rb', line 33

def parseTimestamp(line)
  # would be great to generalize this more
  if line[0] == '['
    # agent logs
    ts = DateTime.parse(line[1...24])
    ts = ts + @offset.seconds
    line[1...24] = ts.to_s
  else
    #mms logs
    ts = DateTime.parse(line[0...28])
    ts = ts + @offset.seconds
    line[0...28] = "[#{ts.to_s}]"
  end
  ts
end

#takeObject



63
64
65
66
67
# File 'lib/logmerge.rb', line 63

def take
  ret = line
  buffer
  ret
end