Class: LogTool::Parser

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

Class Method Summary collapse

Class Method Details

.open_log(file_path) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/logtool/parser.rb', line 6

def self.open_log(file_path)
  yield File.open(file_path)
rescue Errno::ENOENT
  puts "#{file_path}: No such file or directory"
  exit 1
rescue Errno::EACCES
  puts "#{file_path}: Permission denied"
  exit 2
end

.parse_blocks(file_path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/logtool/parser.rb', line 16

def self.parse_blocks(file_path)
  blocks = []
  block = nil
  open_log(file_path) do |fh|
    fh.read.split("\n").each do |line|
      if line =~ Block.head
        blocks << block if block
        block = Block.new
        block.method = $1
        block.ip = $2
        block.text = line + "\n"
      elsif line =~ Block.head_2_3
        blocks << block if block
        block = Block.new
        block.method = $2
        block.ip = $1
        block.text = line + "\n"
      elsif block and line =~ Block.tail
        block.response = $1
        block.time = $2
        block.text += line + "\n"
        block.freeze
      elsif block and line =~ Block.asset_tail
        block.response = $1
        block.time = $2
        block.text += line + "\n"
        block.freeze
      else
        block.text += line + "\n" if block and !block.frozen?
      end
    end
  end
  return blocks
end