class ApacheCrunch::FormatParser

Parses a log format definition

Public Class Methods

new() click to toggle source

Initializes the FormatParser

Takes a FormatElementFactory instance.

# File lib/format.rb, line 24
def initialize
    @_FormatTokenFactory = FormatTokenFactory
end

Public Instance Methods

_shift_token(format_def) click to toggle source

Finds the first token in a format definition

Returns a list containing the token and the new format definition (with the characters that correspond to the token removed)

# File lib/format.rb, line 52
def _shift_token(format_def)
    if format_def =~ /^%%(.*)/
        # Literal "%"
        return [@_FormatTokenFactory.from_abbrev("%%"), $1]
    elsif format_def =~ /^(%[A-Za-z])(.*)/
        # Simple element (e.g. "%h", "%u")
        return [@_FormatTokenFactory.from_abbrev($1), $2]
    elsif format_def =~ /^%[<>]([A-Za-z])(.*)/
        # No idea how to handle mod_log_config's "which request" system yet, so we
        # ignore it.
        return [@_FormatTokenFactory.from_abbrev("%" + $1), $2]
    elsif format_def =~ /^(%\{.+?\}[Ceinor])(.*)/
        # "Contents of" element (e.g. "%{Accept}i")
        return [@_FormatTokenFactory.from_abbrev($1), $2]
    elsif format_def =~ /^(.+?)(%.*|$)/
        # Bare string up until the next %, or up until the end of the format definition
        return [@_FormatTokenFactory.from_abbrev($1), $2]
    end
end
dep_inject!(format_token_factory_cls) click to toggle source

Handles dependency injection

# File lib/format.rb, line 29
def dep_inject!(format_token_factory_cls)
    @_FormatTokenFactory = format_token_factory_cls
end
parse_def(format_def) click to toggle source

Parses the given format_def (e.g. ā€œ%h %u %s #{Referer}iā€) and returns a list of tokens.

These tokens are all instances of a LogFormatElement subclass.

# File lib/format.rb, line 36
def parse_def(format_def)
    s = format_def
    tokens = []
    
    until s.empty?
        token, s = _shift_token(s)
        tokens << token
    end

    tokens
end