class Fluent::JournalParser

Public Instance Methods

parse(text) { |nil, ret| ... } click to toggle source
# File lib/fluent/plugin/parser_journal.rb, line 71
def parse(text)
  ret = []
  length = text.length
  n = 0
  while n < length
    record = {}
    while (m = text.index(/[=\n]/, n)) != n
      raise ParserError if m.nil?
      key = text.slice(n, m-n).force_encoding(Encoding::UTF_8)
      raise ParserError unless key.valid_encoding?
      n = m+1 # continue parsing after newline/equal sign
      if text[m] == '=' # simple field
        m = text.index("\n", n)
        value = text.slice(n, m-n).force_encoding(Encoding::UTF_8)
        raise ParserError unless value.valid_encoding?
      else # text[m] == "\n" # binary safe field
        m = text.slice(n, 8).unpack('Q<')[0]
        n += 8
        value = text.slice(n, m).force_encoding(Encoding::UTF_8)
        value.force_encoding(Encoding::ASCII_8BIT) unless value.valid_encoding?
        m = n+m
      end
      record[key] = value
      n = m+1 # continue parsing after ending newline
    end
    # set timestamp from __REALTIME_TIMESTAMP field
    ns = record['__REALTIME_TIMESTAMP']
    if ns
      record['time'] = ns.to_i / 1000000
    end
    ret.push(record)
    n = m+1 # continue parsing after empty line
  end
  yield nil, ret
end