Class: ApacheCrunch::LogParser

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

Overview

Parses a log file given a path and a Format instance

Instance Method Summary collapse

Constructor Details

#initialize(entry_parser) ⇒ LogParser

Initializes the parser with the path to a log file and a EntryParser.



5
6
7
8
9
10
# File 'lib/log_parser.rb', line 5

def initialize(entry_parser)
    @_entry_parser = entry_parser
    @_log_file = nil

    @_File = File
end

Instance Method Details

#dep_inject!(file_cls) ⇒ Object

Handles dependency injection



13
14
15
# File 'lib/log_parser.rb', line 13

def dep_inject!(file_cls)
    @_File = file_cls
end

#next_entryObject

Returns the next parsed line in the log file as an Entry, or nil if we’ve reached EOF.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/log_parser.rb', line 18

def next_entry
    while line_text = @_log_file.gets
        # This is if we've reached EOF:
        return nil if line_text.nil?

        entry = @_entry_parser.parse(@_format, line_text)
        # The EntryParser returns nil and writes a warning if the line text doesn't
        # match our expected format.
        next if entry.nil?

        return entry
    end
end

#replace_file!(new_file) ⇒ Object

Replaces the LogParser current file with another. Like, for real, on the filesystem.



49
50
51
52
53
# File 'lib/log_parser.rb', line 49

def replace_file!(new_file)
    @_log_file.close
    @_File.rename(new_file.path, @_log_file.path)
    @_log_file = @_File.open(@_log_file.path)
end

#reset_file!Object

Resets the LogParser’s filehandle so we can start over.



33
34
35
36
# File 'lib/log_parser.rb', line 33

def reset_file!
    @_log_file.close
    @_log_file = @_File.open(@_log_file.path)
end

#set_file!(new_file) ⇒ Object

Makes the LogParser start parsing a new log file

new_target is a writable file object that the parser should start parsing, and if in_place is true, we actually replace the contents of the current target with those of the new target.



43
44
45
46
# File 'lib/log_parser.rb', line 43

def set_file!(new_file)
    @_log_file.close unless @_log_file.nil?
    @_log_file = new_file
end

#set_format!(format) ⇒ Object



55
56
57
# File 'lib/log_parser.rb', line 55

def set_format!(format)
    @_format = format
end