Class: Optimus::Reader::LogfileParser

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

Defined Under Namespace

Classes: Column, ColumnList, Frame

Constant Summary collapse

FRAME_START =

Handles parsing optimus log files, which are essentially a blow-by-blow log of everything that happened during an optimus run.

'*** LogFrame Start ***'
FRAME_END =
'*** LogFrame End ***'
HEADER_START =
'*** Header Start ***'
HEADER_END =
'*** Header End ***'
LEVEL_KEY =
'Level'
LEVEL_NAME_KEY =
'LevelName'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ LogfileParser

Valid things for the options hash:

:columns => an array of strings, predefining the expected columns 
            (and their order)
:force => true, if you want to ignore things such as column added
            warnings and if the file is incomplete


34
35
36
37
38
39
40
# File 'lib/log_file_parser.rb', line 34

def initialize(file, options = {})
  @columns = options[:columns]
  @force = options[:force]
  @file = file
  @levels = [''] # The 0 index should be blank.
  @found_cols = ColumnList.new()
end

Instance Attribute Details

#framesObject (readonly)

Returns the value of attribute frames.



26
27
28
# File 'lib/log_file_parser.rb', line 26

def frames
  @frames
end

#levelsObject (readonly)

Returns the value of attribute levels.



27
28
29
# File 'lib/log_file_parser.rb', line 27

def levels
  @levels
end

Class Method Details

.can_parse?(lines) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/log_file_parser.rb', line 80

def self.can_parse?(lines)
  lines[0].include?(HEADER_START)
end

Instance Method Details

#leaf_framesObject



71
72
73
# File 'lib/log_file_parser.rb', line 71

def leaf_frames
  return frames.find_all { |frame| frame.leaf? }
end

#make_frames!Object



42
43
44
45
46
47
# File 'lib/log_file_parser.rb', line 42

def make_frames!
  read_levels(@file)
  @frames = frameify(@file)
  set_parents!
  set_counters!
end

#skip_column(col_name) ⇒ Object

Define this as a column we *should not* include in out output.



76
77
78
# File 'lib/log_file_parser.rb', line 76

def skip_column(col_name)
  @skip_columns[col_name] = true
end

#to_optimusObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/log_file_parser.rb', line 49

def to_optimus
  begin
    if @frames.nil? or @frames.empty?
      make_frames!
    end
  rescue Exception => e
    raise e unless @force
  end
    
  @columns ||= @found_cols.names
  data = Optimus::Data.new(@columns)
  self.leaf_frames.each do |frame|
    row = data.add_row
    @found_cols.names_with_cols.each do |pair|
      name, col = *pair
      val = frame.get(col)
      row[name] = val
    end
  end
  return data
end