class Log

Attributes

file[RW]
finished[RW]
line[RW]
next[RW]
next_timestamp[RW]
offet[RW]
tag[RW]
timestamp[RW]

Public Class Methods

new(path, autotag=false) click to toggle source
# File lib/logmerge.rb, line 39
def initialize(path, autotag=false)
  @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
  if @tag.nil? && autotag
    @tag = File.basename(path)
  end
  @file = File.open(path)
  @finished = false
  buffer
end

Public Instance Methods

applyTimestamp(current, timestamp) click to toggle source
# File lib/logmerge.rb, line 79
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.prepend("[#{@tag}]") if @tag && timestamp
  current
end
buffer() click to toggle source
# File lib/logmerge.rb, line 89
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}"
    @line.push(current)
    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?
    @line.push(current)
  end
rescue EOFError
  @finished = true if @line.empty?
end
parseTimestamp(current) click to toggle source
# File lib/logmerge.rb, line 57
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
take() click to toggle source
# File lib/logmerge.rb, line 123
def take
  ret = line
  buffer
  ret
end