Class: Fluent::Auditify::Reporter::ConsoleReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/auditify/reporter/console.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger, options = {}) ⇒ ConsoleReporter



8
9
10
11
12
13
14
# File 'lib/fluent/auditify/reporter/console.rb', line 8

def initialize(logger, options={})
  @options = {
    format: :auto
  }
  @options.merge!(options)
  @logger = logger
end

Instance Method Details

#bombObject



16
17
18
# File 'lib/fluent/auditify/reporter/console.rb', line 16

def bomb
  "\u{1f4a3}"
end

#file_get_contents(path) ⇒ Object



20
21
22
# File 'lib/fluent/auditify/reporter/console.rb', line 20

def file_get_contents(path)
  File.open(path) do |f| f.readlines end
end

#run(charges, logger = nil) ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fluent/auditify/reporter/console.rb', line 24

def run(charges, logger=nil)
  charges.each do |entry|
    message = entry.first
    options = entry.last
    if options[:line] and options[:content]
      if @options[:format] == :auto
        lines = file_get_contents(options[:path])
        min = options[:line] - 2 > 0 ? options[:line] - 2 : 0
        max = options[:line] + 2 < lines.size ? options[:line] + 2 : lines.size - 1
        content = ""
        suggested = ""
        min.upto(max).each_with_index do |line, index|
          content << "#{min + index + 1}: #{lines[min + index].chomp}\n"
          if options[:suggest] and options[:line] == min + index + 1
            suggested << "#{min + index + 1}: #{options[:suggest].chomp}\n"
          else
            suggested << "#{min + index + 1}: #{lines[min + index].chomp}\n"
          end
        end
        if options[:suggest]
          diff_content = ''
          Diff::LCS.sdiff(content.chars, suggested.chars).each do |change|
            case change.action
            when '-'
              diff_content << Pastel.new.red(change.old_element)
            when '+'
              diff_content << Pastel.new.green(change.new_element)
            else
              diff_content << change.old_element
            end
          end
          @logger.error("#{bomb} [plugin:#{options[:plugin]},category:#{options[:category]}] #{message} at #{options[:path]}:#{options[:line]}\n#{diff_content}")
        else
          @logger.error("#{bomb} [plugin:#{options[:plugin]},category:#{options[:category]}] #{message} at #{options[:path]}:#{options[:line]}\n#{content}")
        end
      else
        @logger.error("#{bomb} [plugin:#{options[:plugin]},category:#{options[:category]}] #{message} at #{options[:path]}:#{options[:line]}: #{options[:content]}")
      end
    else
      @logger.error("#{bomb} [plugin:#{options[:plugin]},category:#{options[:category]}] #{message} at #{options[:path]}")
    end
  end
end