class Indy::LogDefinition

Attributes

entry_fields[RW]
entry_regexp[RW]
multiline[RW]
time_format[RW]

Public Class Methods

new(args=:default) click to toggle source
# File lib/indy/log_definition.rb, line 7
def initialize(args=:default)
  case args
  when :default, {}
    params_hash = set_defaults
  when Array, Hash
    params_hash = parse_enumerable_params(args)
  end
  raise ArgumentError, "Values for entry_regexp and/or entry_fields were not supplied" unless (params_hash[:entry_fields] && params_hash[:entry_regexp])
  if params_hash[:multiline]
    @entry_regexp = Regexp.new(params_hash[:entry_regexp], Regexp::MULTILINE)
    @multiline = true
  else
    @entry_regexp = Regexp.new(params_hash[:entry_regexp])
  end
  @entry_fields = params_hash[:entry_fields]
  @time_format = params_hash[:time_format] || Indy::LogFormats::DEFAULT_DATE_TIME # '%Y-%m-%d %H:%M:%S'
  define_struct
end

Public Instance Methods

assert_valid_field_list(values) click to toggle source

Ensure number of fields is expected

# File lib/indy/log_definition.rb, line 107
def assert_valid_field_list(values)
  if values.length == @entry_fields.length + 1 # values also includes raw_entry
    @field_list_is_valid = true
  else
    raise Indy::Source::FieldMismatchException, "Number of expected fields does not match those captured via the regexp pattern.\nThe expected fields are:\n=> #{@entry_fields.join("\n=> ")}\nThe log entry and captured fields are:\n=> #{values.join("\n=> ")}"
  end
end
create_struct( entry_hash ) click to toggle source

Return a Struct::Entry object from a hash of values from a log entry

@param [Hash] entry_hash a hash of :field_name => value pairs for one log entry

# File lib/indy/log_definition.rb, line 51
def create_struct( entry_hash )
  values = entry_hash.keys.sort_by{|entry|entry.to_s}.collect {|key| entry_hash[key]}
  result = Struct::Entry.new( *values )
  result
end
define_struct() click to toggle source

Define Struct::Entry. Ignore warnings.

# File lib/indy/log_definition.rb, line 60
def define_struct
  fields = (@entry_fields + [:raw_entry]).sort_by{|key|key.to_s}
  verbose = $VERBOSE
  $VERBOSE = nil
  Struct.new( "Entry", *fields )
  $VERBOSE = verbose
end
entry_hash(values) click to toggle source

Convert log entry into hash

# File lib/indy/log_definition.rb, line 71
def entry_hash(values)
  assert_valid_field_list(values) unless @field_list_is_valid # just do it once
  raw_entry = values.shift
  hash = Hash[ *@entry_fields.zip( values ).flatten ]
  hash[:raw_entry] = raw_entry.strip
  hash
end
parse_entry(raw_entry) click to toggle source

Return a hash of field=>value pairs for the log entry

@param [String] raw_entry The raw log entry

# File lib/indy/log_definition.rb, line 85
def parse_entry(raw_entry)
  match_data = /#{@entry_regexp}/.match(raw_entry)
  return nil unless match_data
  values = match_data.captures
  values.shift if @multiline
  entry_hash([raw_entry, values].flatten)
end
parse_entry_captures( capture_array ) click to toggle source

Return a hash of field=>value pairs for the array of captured values from a log entry

@param [Array] capture_array The array of values captured by the LogDefinition regexp

# File lib/indy/log_definition.rb, line 98
def parse_entry_captures( capture_array )
  entire_entry = capture_array.shift
  values = capture_array
  entry_hash([entire_entry, values].flatten)
end
parse_enumerable_params(args) click to toggle source
# File lib/indy/log_definition.rb, line 34
def parse_enumerable_params(args)
  params_hash = {}
  params_hash.merge!(args)
  if args.keys.include? :log_format
    # support 0.3.4 params
    params_hash[:entry_regexp] = args[:log_format][0]
    params_hash[:entry_fields] = args[:log_format][1..-1]
    params_hash.delete :log_format
  end
  params_hash
end
set_defaults() click to toggle source
# File lib/indy/log_definition.rb, line 26
def set_defaults
  params_hash = {}
  params_hash[:entry_regexp] = Indy::LogFormats::DEFAULT_ENTRY_REGEXP
  params_hash[:entry_fields] = Indy::LogFormats::DEFAULT_ENTRY_FIELDS
  params_hash[:time_format] = Indy::LogFormats::DEFAULT_DATE_TIME
  params_hash
end