Class: Sassmagic::Installers::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/sassmagic/installer.rb

Overview

logger

Direct Known Subclasses

NullLogger

Constant Summary collapse

COLORS =
{ :clear => 0, :red => 31, :green => 32, :yellow => 33, :blue => 34 }
ACTION_COLORS =
{
    :error     => :red,
    :warning   => :yellow,
    :info      => :green,
    :compile   => :green,
    :overwrite => :yellow,
    :modified  => :yellow,
    :clean     => :yellow,
    :write     => :green,
    :create    => :green,
    :remove    => :yellow,
    :delete    => :yellow,
    :deleted   => :yellow,
    :created   => :yellow,
    :exists    => :green,
    :directory => :green,
    :identical => :green,
    :convert   => :green,
    :unchanged => :yellow
}
DEFAULT_ACTIONS =
ACTION_COLORS.keys
ACTION_CAN_BE_QUIET =
{
    :error     => false,
    :warning   => true,
    :info      => true,
    :compile   => true,
    :overwrite => true,
    :modified  => true,
    :clean     => true,
    :write     => true,
    :create    => true,
    :remove    => true,
    :delete    => true,
    :deleted   => true,
    :created   => true,
    :exists    => true,
    :directory => true,
    :identical => true,
    :convert   => true,
    :unchanged => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*actions) ⇒ Logger

Returns a new instance of Logger.



213
214
215
216
217
# File 'lib/sassmagic/installer.rb', line 213

def initialize(*actions)
  self.options = actions.last.is_a?(Hash) ? actions.pop : {}
  @actions = DEFAULT_ACTIONS.dup
  @actions += actions
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



211
212
213
# File 'lib/sassmagic/installer.rb', line 211

def actions
  @actions
end

#optionsObject

Returns the value of attribute options.



211
212
213
# File 'lib/sassmagic/installer.rb', line 211

def options
  @options
end

#timeObject

Returns the value of attribute time.



211
212
213
# File 'lib/sassmagic/installer.rb', line 211

def time
  @time
end

Instance Method Details

#action_padding(action) ⇒ Object

add padding to the left of an action that was performed.



280
281
282
# File 'lib/sassmagic/installer.rb', line 280

def action_padding(action)
  ' ' * [(max_action_length - action.to_s.length), 0].max
end

#color(c) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/sassmagic/installer.rb', line 255

def color(c)
  if c && COLORS.has_key?(c.to_sym)
    if defined?($boring) && $boring
      ""
    else
      "\e[#{COLORS[c.to_sym]}m"
    end
  else
    ""
  end
end

#emit(msg) ⇒ Object

Emit a log message without a trailing newline



268
269
270
271
# File 'lib/sassmagic/installer.rb', line 268

def emit(msg)
  print msg
  $stdout.flush
end

#greenObject



233
234
235
# File 'lib/sassmagic/installer.rb', line 233

def green
  wrap(:green) { yield }
end

#log(msg) ⇒ Object

Emit a log message with a trailing newline



274
275
276
277
# File 'lib/sassmagic/installer.rb', line 274

def log(msg)
  puts msg
  $stdout.flush
end

#max_action_lengthObject

the maximum length of all the actions known to the logger.



285
286
287
# File 'lib/sassmagic/installer.rb', line 285

def max_action_length
  @max_action_length ||= actions.inject(0){|memo, a| [memo, a.to_s.length].max}
end

#record(action, *arguments) ⇒ Object

Record an action that has occurred



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/sassmagic/installer.rb', line 220

def record(action, *arguments)
  return if options[:quiet] && ACTION_CAN_BE_QUIET[action]
  msg = ""
  if time
    msg << Time.now.strftime("%I:%M:%S.%3N %p")
  end
  msg << color(ACTION_COLORS[action])
  msg << "#{action_padding(action)}#{action}"
  msg << color(:clear)
  msg << " #{arguments.join(' ')}"
  log msg
end

#redObject



237
238
239
# File 'lib/sassmagic/installer.rb', line 237

def red
  wrap(:red) { yield }
end

#wrap(c, reset_to = :clear) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/sassmagic/installer.rb', line 245

def wrap(c, reset_to = :clear)
  $stderr.write(color(c))
  $stdout.write(color(c))
  yield
ensure
  $stderr.write(color(reset_to))
  $stdout.write(color(reset_to))
  $stdout.flush
end

#yellowObject



241
242
243
# File 'lib/sassmagic/installer.rb', line 241

def yellow
  wrap(:yellow) { yield }
end