Class: Morpheus::Logging::DarkPrinter

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/morpheus/logging.rb

Overview

An IO class for printing debugging info This is used as a proxy for ::RestClient.log printing right now.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, color = nil, is_dark = true) ⇒ DarkPrinter

Returns a new instance of DarkPrinter.



107
108
109
110
111
# File 'lib/morpheus/logging.rb', line 107

def initialize(io, color=nil, is_dark=true)
  @io = io # || $stdout
  @color = color # || cyan
  @is_dark = is_dark
end

Instance Attribute Details

#colorObject

String

ansi color code for output. Default is dark



88
89
90
# File 'lib/morpheus/logging.rb', line 88

def color
  @color
end

#ioObject

IO

to write to



85
86
87
# File 'lib/morpheus/logging.rb', line 85

def io
  @io
end

Class Method Details

.<<(*messages) ⇒ Object



103
104
105
# File 'lib/morpheus/logging.rb', line 103

def self.<<(*messages)
  instance.<<(*messages)
end

.instanceObject

DarkPrinter with io STDOUT



91
92
93
# File 'lib/morpheus/logging.rb', line 91

def self.instance
  @instance ||= self.new(STDOUT, nil, true)
end


95
96
97
# File 'lib/morpheus/logging.rb', line 95

def self.print(*messages)
  instance.print(*messages)
end

.puts(*messages) ⇒ Object



99
100
101
# File 'lib/morpheus/logging.rb', line 99

def self.puts(*messages)
  instance.puts(*messages)
end

Instance Method Details

#<<(*messages) ⇒ Object



158
159
160
# File 'lib/morpheus/logging.rb', line 158

def <<(*messages)
  print(*messages)
end


136
137
138
139
140
141
142
143
144
145
# File 'lib/morpheus/logging.rb', line 136

def print(*messages)
  if @io
    messages = messages.flatten.collect {|it| scrub_message(it) }
    print_with_color do 
      messages.each do |msg|
        @io.print msg
      end
    end
  end
end


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/morpheus/logging.rb', line 124

def print_with_color(&block)
  if Term::ANSIColor.coloring?
    @io.print Term::ANSIColor.reset
    @io.print @color if @color
    @io.print Term::ANSIColor.dark if @is_dark
  end
  yield
  if Term::ANSIColor.coloring?
    @io.print Term::ANSIColor.reset
  end
end

#puts(*messages) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/morpheus/logging.rb', line 147

def puts(*messages)
  if @io
    messages = messages.flatten.collect {|it| scrub_message(it) }
    print_with_color do 
      messages.each do |msg|
        @io.puts msg
      end
    end
  end
end

#scrub_message(msg) ⇒ Object

mask well known secrets



114
115
116
117
118
119
120
121
122
# File 'lib/morpheus/logging.rb', line 114

def scrub_message(msg)
  if msg.is_a?(String)
    msg.gsub!(/Authorization\"\s?\=\>\s?\"Bearer [^"]+/, 'Authorization"=>"Bearer ************')
    msg.gsub!(/Authorization\:\s?Bearer [^"]+/, 'Authorization"=>"Bearer ************')
    msg.gsub!(/password\"\s?\=\>\s?\"[^"]+/, 'password"=>"************')
    msg.gsub!(/password\=\"[^"]+/, 'password="************')
  end
  msg
end