class ApacheCrunch::EntryParser

Makes Entry instances based on log file text

Public Class Methods

new() click to toggle source

Initializes the instance given a ProgressMeter instance

# File lib/entry.rb, line 33
def initialize
    @_Entry = Entry
    @_Element = Element
    
    @_progress_meter = NullProgressMeter.new
    @_regex = nil
end

Public Instance Methods

_build_regex(format) click to toggle source
# File lib/entry.rb, line 84
def _build_regex(format)
    r = "^"
    format.tokens.each do |tok|
        # We only care to remember the captured LogFormatElements.  No need to put
        # parentheses around StringElements that aren't interpolated.
        if tok.captured?
            r += "(" + tok.regex + ")"
        else
            r += tok.regex
        end
    end
    r += "$"

    Regexp.compile(r)
end
add_progress_meter!(meter) click to toggle source

Applies the given ProgressMeter to the parser so that it will output progress.

The meter’s output_progress method will get called every time we finish parsing a log entry.

# File lib/entry.rb, line 51
def add_progress_meter!(meter)
    @_progress_meter = meter
end
dep_inject!(entry_cls, element_cls) click to toggle source

Handles dependency injection

# File lib/entry.rb, line 42
def dep_inject!(entry_cls, element_cls)
    @_Entry = entry_cls
    @_Element = element_cls
end
parse(format, log_text) click to toggle source

Returns an Entry instance built from a line of text, or nil if the line was malformatted

# File lib/entry.rb, line 56
def parse(format, log_text)
    @_regex = _build_regex(format) if @_regex.nil?

    match = (log_text =~ @_regex)
    if match.nil?
        warn "Log line did not match expected format: #{log_text.rstrip}"
        return nil
    end

    match_groups = Regexp.last_match.to_a
    match_groups.shift # First value is the whole matched string, which we do not want

    entry = @_Entry.new
    format.captured_tokens.each_with_index do |tok,i|
         element = Element.new
         element.populate!(tok, match_groups[i])
         entry.captured_elements[tok.name] = element
    end

    # Add the full text of the log entry to the Entry instance as well.
    text_element = Element.new
    text_element.populate!(StringToken.new, log_text)
    entry.captured_elements[:text] = text_element

    @_progress_meter.output_progress(entry)
    entry
end