class Fluent::Compat::TextParser

Constants

ParserError

Keep backward compatibility for existing plugins

TypeConverter

TODO: will be removed at v1

Attributes

estimate_current_event[RW]

SET false BEFORE CONFIGURE, to return nil when time not parsed ‘configure()’ may raise errors for unexpected configurations

parser[R]

Private Class Methods

lookup(format) click to toggle source
# File lib/fluent/compat/parser.rb, line 95
def self.lookup(format)
  # TODO: warn when deprecated to use Plugin.new_parser or RegexpParser.new directly
  if format.nil?
    raise ConfigError, "'format' parameter is required"
  end

  if format[0] == ?/ && format[format.length-1] == ?/
    # regexp
    begin
      regexp = Regexp.new(format[1..-2])
      if regexp.named_captures.empty?
        raise "No named captures"
      end
    rescue
      raise ConfigError, "Invalid regexp '#{format[1..-2]}': #{$!}"
    end

    RegexpParser.new(regexp)
  else
    # built-in template
    begin
      Fluent::Plugin.new_parser(format)
    rescue ConfigError # keep same error message
      raise ConfigError, "Unknown format template '#{format}'"
    end
  end
end
new() click to toggle source
# File lib/fluent/compat/parser.rb, line 46
def initialize
  # TODO: warn when deprecated
  @parser = nil
  @estimate_current_event = nil
end
register_template(type, template, time_format=nil) click to toggle source
# File lib/fluent/compat/parser.rb, line 84
def self.register_template(type, template, time_format=nil)
  # TODO: warn when deprecated to use Plugin.register_parser directly
  if template.is_a?(Class) || template.respond_to?(:call)
    Fluent::Plugin.register_parser(type, template)
  elsif template.is_a?(Regexp)
    Fluent::Plugin.register_parser(type, Proc.new { RegexpParser.new(template, {'time_format' => time_format}) })
  else
    raise ArgumentError, "Template for parser must be a Class, callable object or regular expression object"
  end
end

Private Instance Methods

configure(conf, required=true) click to toggle source
# File lib/fluent/compat/parser.rb, line 58
def configure(conf, required=true)
  format = conf['format']

  @parser = TextParser.lookup(format)

  if @parser.respond_to?(:configure)
    @parser.configure(conf)
  end
  if !@estimate_current_event.nil? && @parser.respond_to?(:'estimate_current_event=')
    # external code sets parser.estimate_current_event = false
    @parser.estimate_current_event = @estimate_current_event
  end

  return true
end
parse(text, &block) click to toggle source
# File lib/fluent/compat/parser.rb, line 74
def parse(text, &block)
  if block
    @parser.parse(text, &block)
  else
    @parser.parse(text) { |time, record|
      return time, record
    }
  end
end