Class: ApacheCrunch::EntryParser

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

Overview

Makes Entry instances based on log file text

Instance Method Summary collapse

Constructor Details

#initializeEntryParser

Initializes the instance given a ProgressMeter instance



33
34
35
36
37
38
39
# File 'lib/entry.rb', line 33

def initialize
    @_Entry = Entry
    @_Element = Element
    
    @_progress_meter = NullProgressMeter.new
    @_regex = nil
end

Instance Method Details

#_build_regex(format) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

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.



51
52
53
# File 'lib/entry.rb', line 51

def add_progress_meter!(meter)
    @_progress_meter = meter
end

#dep_inject!(entry_cls, element_cls) ⇒ Object

Handles dependency injection



42
43
44
45
# File 'lib/entry.rb', line 42

def dep_inject!(entry_cls, element_cls)
    @_Entry = entry_cls
    @_Element = element_cls
end

#parse(format, log_text) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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