class ApacheCrunch::LogParser

Parses a log file given a path and a Format instance

Public Class Methods

new(entry_parser) click to toggle source

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

# File lib/log_parser.rb, line 5
def initialize(entry_parser)
    @_entry_parser = entry_parser
    @_log_file = nil

    @_File = File
end

Public Instance Methods

dep_inject!(file_cls) click to toggle source

Handles dependency injection

# File lib/log_parser.rb, line 13
def dep_inject!(file_cls)
    @_File = file_cls
end
next_entry() click to toggle source

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

# 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) click to toggle source

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

# 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!() click to toggle source

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

# 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) click to toggle source

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.

# 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) click to toggle source
# File lib/log_parser.rb, line 55
def set_format!(format)
    @_format = format
end