Class: ApacheCrunch::FormatParser

Inherits:
Object
  • Object
show all
Defined in:
lib/format.rb

Overview

Parses a log format definition

Instance Method Summary collapse

Constructor Details

#initializeFormatParser

Initializes the FormatParser

Takes a FormatElementFactory instance.



24
25
26
# File 'lib/format.rb', line 24

def initialize
    @_FormatTokenFactory = FormatTokenFactory
end

Instance Method Details

#_shift_token(format_def) ⇒ Object

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)



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object

Handles dependency injection



29
30
31
# File 'lib/format.rb', line 29

def dep_inject!(format_token_factory_cls)
    @_FormatTokenFactory = format_token_factory_cls
end

#parse_def(format_def) ⇒ Object

Parses the given format_def (e.g. “%h %u %s #Refereri”) and returns a list of tokens.

These tokens are all instances of a LogFormatElement subclass.



36
37
38
39
40
41
42
43
44
45
46
# 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