Class: MyStuff::Logger::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/my_stuff/logger/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Reader

Returns a new instance of Reader.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/my_stuff/logger/reader.rb', line 12

def initialize options = {}
  @output = options[:device] || STDOUT
  if options.include? :colorize
    @colorize = options[:colorize]
  else
    @colorize = @output.tty?
  end

  @filters = (options[:filters] || [
    :PriorityColors,
  ]).map{ |filter| ReaderFilters.get(filter) }
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



8
9
10
# File 'lib/my_stuff/logger/reader.rb', line 8

def filters
  @filters
end

#outputObject (readonly)

Returns the value of attribute output.



9
10
11
# File 'lib/my_stuff/logger/reader.rb', line 9

def output
  @output
end

Instance Method Details

#colorize?Boolean

Returns:

  • (Boolean)


10
# File 'lib/my_stuff/logger/reader.rb', line 10

def colorize?; @colorize; end

#format_io(input) ⇒ Object

Read lines from IO, and pretty-print them.



26
27
28
29
30
# File 'lib/my_stuff/logger/reader.rb', line 26

def format_io input
  input.each_line do |line|
    output.write format_log_line(line)
  end
end

#format_log_line(x) ⇒ Object



32
33
34
35
36
37
# File 'lib/my_stuff/logger/reader.rb', line 32

def format_log_line x
  filters.each do |filter|
    x.replace(filter.call(x, :colorize => colorize?))
  end
  x
end

#write(x) ⇒ Object

Do a really poor job of emulating an IO device :p



40
41
42
43
44
# File 'lib/my_stuff/logger/reader.rb', line 40

def write x
  x.each_line do |line|
    output.write format_log_line(line)
  end
end