Class: Mnogootex::Log::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/mnogootex/log/processor.rb

Overview

This class exposes methods to convert strings into Lines that can be tagged, filtered, colored (using Levels and Matchers to define how) and finally rendered into printable content.

It can also be instantiated with a specific configuration to #run the whole process repeatably on multiple inputs.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matchers:, levels:, min_level:, colorize:, indent_width:) ⇒ Processor

Returns a new instance of Processor.

Parameters:

  • matchers (Array<Matcher>)
  • levels (Array<Level>)
  • indent_width (Fixnum)
  • min_level (Symbol)


86
87
88
89
90
91
92
# File 'lib/mnogootex/log/processor.rb', line 86

def initialize(matchers:, levels:, min_level:, colorize:, indent_width:)
  @matchers = matchers
  @levels = levels
  @min_level = min_level
  @colorize = colorize
  @indent_width = indent_width
end

Class Method Details

.colorize_lines!(lines, levels:) ⇒ Array<Line>

Applies Level#colors to the Lines, according the Levels hash.

Parameters:

Returns:



67
68
69
70
71
# File 'lib/mnogootex/log/processor.rb', line 67

def self.colorize_lines!(lines, levels:)
  lines.each do |line|
    line.text = line.text.colorize(levels.fetch(line.level).color)
  end
end

.filter_lines!(lines, levels:, min_level:) ⇒ Array<Line>

Discards Lines having Line#levels with Level#priority lower than the minimum, according the Levels hash.

Parameters:

  • lines (Array<Line>)
  • levels (Hash<Symbol, Level>)
  • min_level (Symbol)

Returns:



56
57
58
59
60
# File 'lib/mnogootex/log/processor.rb', line 56

def self.filter_lines!(lines, levels:, min_level:)
  lines.select! do |line|
    levels.fetch(line.level).priority >= levels.fetch(min_level).priority
  end
end

.render_lines!(lines, indent_width:) ⇒ Array<String>

Renders Lines to space-indented strings terminated by a newline.

Parameters:

  • lines (Array<Line>)
  • indent_width (Fixnum)

Returns:

  • (Array<String>)


78
79
80
# File 'lib/mnogootex/log/processor.rb', line 78

def self.render_lines!(lines, indent_width:)
  lines.map! { |line| "#{' ' * indent_width}#{line.text}\n" }
end

.strings_to_lines!(strings) ⇒ Array<Line>

Converts strings into Lines.

Parameters:

  • strings (Array<String>)

Returns:



25
26
27
28
29
# File 'lib/mnogootex/log/processor.rb', line 25

def self.strings_to_lines!(strings)
  strings.map! do |line|
    Line.new line.chomp
  end
end

.tag_lines!(lines, matchers:) ⇒ Array<Line>

Updates Line#levels of the given Lines using the Matchers.

Parameters:

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mnogootex/log/processor.rb', line 36

def self.tag_lines!(lines, matchers:)
  tail_length, matcher = 0 # , nil
  lines.each do |line|
    if tail_length.zero?
      matcher = matchers.detect { |m| m.regexp === line.text }
      tail_length = matcher&.length&.-(1) || 0
    else # still on the tail of the previous match
      tail_length -= 1
    end
    line.level = matcher&.level
  end
end

Instance Method Details

#run(lines) ⇒ Array<String>

Runs the Processor on the given strings to convert, tag, filter, color and render them using its initialization parameters.

Parameters:

  • lines (Array<String>)

Returns:

  • (Array<String>)


104
105
106
107
108
109
110
111
112
# File 'lib/mnogootex/log/processor.rb', line 104

def run(lines)
  @lines = lines.dup
  Processor.strings_to_lines! @lines
  Processor.tag_lines! @lines, matchers: @matchers
  Processor.filter_lines! @lines, levels: @levels, min_level: @min_level
  Processor.colorize_lines! @lines, levels: @levels if @colorize
  Processor.render_lines! @lines, indent_width: @indent_width
  @lines
end