Class: Mnogootex::Log::Processor
- Defined in:
- lib/mnogootex/log/processor.rb
Overview
Class Method Summary collapse
-
.colorize_lines!(lines, levels:) ⇒ Array<Line>
Applies Level#colors to the Lines, according the Levels hash.
-
.filter_lines!(lines, levels:, min_level:) ⇒ Array<Line>
Discards Lines having Line#levels with Level#priority lower than the minimum, according the Levels hash.
-
.render_lines!(lines, indent_width:) ⇒ Array<String>
Renders Lines to space-indented strings terminated by a newline.
-
.strings_to_lines!(strings) ⇒ Array<Line>
Converts strings into Lines.
-
.tag_lines!(lines, matchers:) ⇒ Array<Line>
Updates Line#levels of the given Lines using the Matchers.
Instance Method Summary collapse
-
#initialize(matchers:, levels:, min_level:, colorize:, indent_width:) ⇒ Processor
constructor
A new instance of Processor.
- #run(lines) ⇒ Array<String>
Constructor Details
#initialize(matchers:, levels:, min_level:, colorize:, indent_width:) ⇒ Processor
Returns a new instance of Processor.
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.
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.
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.
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.
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.
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>
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 |