Class: Compass::Logger

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

Constant Summary collapse

DEFAULT_ACTIONS =
[:directory, :exists, :remove, :create, :overwrite, :compile, :error, :identical, :warning]
COLORS =
{ :clear => 0, :red => 31, :green => 32, :yellow => 33 }
ACTION_COLORS =
{
  :error     => :red,
  :warning   => :yellow,
  :compile   => :green,
  :overwrite => :yellow,
  :create    => :green,
  :remove    => :yellow,
  :exists    => :green,
  :directory => :green,
  :identical => :green,
  :convert   => :green,
  :unchanged => :yellow
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*actions) ⇒ Logger

Returns a new instance of Logger.



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

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.



24
25
26
# File 'lib/compass/logger.rb', line 24

def actions
  @actions
end

#optionsObject

Returns the value of attribute options.



24
25
26
# File 'lib/compass/logger.rb', line 24

def options
  @options
end

Instance Method Details

#action_padding(action) ⇒ Object

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



82
83
84
# File 'lib/compass/logger.rb', line 82

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

#color(c) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/compass/logger.rb', line 60

def color(c)
  if Compass.configuration.color_output && 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



72
73
74
# File 'lib/compass/logger.rb', line 72

def emit(msg)
  print msg
end

#log(msg) ⇒ Object

Emit a log message



77
78
79
# File 'lib/compass/logger.rb', line 77

def log(msg)
  puts msg
end

#max_action_lengthObject

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



87
88
89
# File 'lib/compass/logger.rb', line 87

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



33
34
35
36
37
38
39
40
# File 'lib/compass/logger.rb', line 33

def record(action, *arguments)
  msg = ""
  msg << color(ACTION_COLORS[action]) if Compass.configuration.color_output
  msg << "#{action_padding(action)}#{action}"
  msg << color(:clear) if Compass.configuration.color_output
  msg << " #{arguments.join(' ')}"
  log msg
end

#redObject



42
43
44
45
46
47
48
49
# File 'lib/compass/logger.rb', line 42

def red
  $stderr.write(color(:red))
  $stdout.write(color(:red))
  yield
ensure
  $stderr.write(color(:clear))
  $stdout.write(color(:clear))
end

#yellowObject



51
52
53
54
55
56
57
58
# File 'lib/compass/logger.rb', line 51

def yellow
  $stderr.write(color(:yellow))
  $stdout.write(color(:yellow))
  yield
ensure
  $stderr.write(color(:clear))
  $stdout.write(color(:clear))
end