class Fluent::Compat::TextParser

Private Class Methods

lookup(format) click to toggle source
# File lib/fluent/compat/parser.rb, line 91
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 43
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 80
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 55
def configure(conf, required=true)
  format = conf['format']

  @parser = TextParser.lookup(format)
  if ! @estimate_current_event.nil? && @parser.respond_to?(:'estimate_current_event=')
    @parser.estimate_current_event = @estimate_current_event
  end

  if @parser.respond_to?(:configure)
    @parser.configure(conf)
  end

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