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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/logmerge.rb', line 39

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.



38
39
40
# File 'lib/logmerge.rb', line 38

def file
  @file
end

#finishedObject

Returns the value of attribute finished.



38
39
40
# File 'lib/logmerge.rb', line 38

def finished
  @finished
end

#lineObject

Returns the value of attribute line.



38
39
40
# File 'lib/logmerge.rb', line 38

def line
  @line
end

#nextObject

Returns the value of attribute next.



38
39
40
# File 'lib/logmerge.rb', line 38

def next
  @next
end

#next_timestampObject

Returns the value of attribute next_timestamp.



38
39
40
# File 'lib/logmerge.rb', line 38

def next_timestamp
  @next_timestamp
end

#offetObject

Returns the value of attribute offet.



38
39
40
# File 'lib/logmerge.rb', line 38

def offet
  @offet
end

#tagObject

Returns the value of attribute tag.



38
39
40
# File 'lib/logmerge.rb', line 38

def tag
  @tag
end

#timestampObject

Returns the value of attribute timestamp.



38
39
40
# File 'lib/logmerge.rb', line 38

def timestamp
  @timestamp
end

Instance Method Details

#append(current, timestamp = nil) ⇒ Object



85
86
87
88
# File 'lib/logmerge.rb', line 85

def append(current, timestamp=nil)
  current.prepend("[#{@tag}]") if @tag && timestamp
  @line.push(current)
end

#applyTimestamp(current, timestamp) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/logmerge.rb', line 76

def applyTimestamp(current, timestamp)
  if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/
    current[1...24] = timestamp.to_s
  elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/
    current[0...28] = "[#{timestamp.to_s}]"
  end
  current
end

#bufferObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/logmerge.rb', line 90

def buffer
  @count ||= 0
  # puts "buffer: #{@count}"
  # puts "file buffer: #{file.buf}"
  @count = @count + 1
  # TODO: add support for multiline log statements
  # basically read until next timestamp found including all lines as an entry
  @line = []
  @timestamp = nil
  timestamp_index = 0
  while @timestamp.nil? do
    current = file.readline.strip
    # puts "current: #{current}"
    @timestamp = parseTimestamp(current)
    # puts "timestamp: #{@timestamp}"
    append(current, @timestamp)
    timestamp_index = timestamp_index + 1 if @timestamp.nil?
  end

  applyTimestamp(line[timestamp_index], @timestamp) if line.size > timestamp_index

  next_timestamp = nil
  while next_timestamp.nil? do
    current = file.readline.strip
    # puts "nextcurrent: #{current}"
    next_timestamp = parseTimestamp(current)
    # puts "next_timestamp: #{next_timestamp}"
    file.unreadline(current + "\n") && break unless next_timestamp.nil?
    append(current)
  end
rescue EOFError
  @finished = true if @line.empty?
end

#parseTimestamp(current) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/logmerge.rb', line 54

def parseTimestamp(current)
  # puts "parsingTimestamp: #{current}"
  # would be great to generalize this more
  ts = nil
  if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/
    # agent logs
    ts_part = current[1...24]
    # puts "ts_part: #{ts_part}"
    ts = DateTime.parse(ts_part)
    ts = ts + @offset.seconds
  elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/
    #mms logs
    ts_part = current[0...28]
    # puts "ts_part: #{ts_part}"
    ts = DateTime.parse(current[0...28])
    ts = ts + @offset.seconds
  end
  ts
rescue Date::Error
  nil
end

#takeObject



124
125
126
127
128
# File 'lib/logmerge.rb', line 124

def take
  ret = line
  buffer
  ret
end