2009-02-01 22:17:07 +00:00
|
|
|
module Compass
|
2009-12-01 17:50:31 +00:00
|
|
|
|
2009-02-01 22:17:07 +00:00
|
|
|
class Logger
|
|
|
|
|
2010-02-22 01:32:23 +00:00
|
|
|
DEFAULT_ACTIONS = [:directory, :exists, :remove, :create, :overwrite, :compile, :error, :identical, :warning]
|
2009-11-30 04:17:30 +00:00
|
|
|
|
2009-12-01 17:50:31 +00:00
|
|
|
COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 }
|
|
|
|
|
2009-11-30 04:17:30 +00:00
|
|
|
ACTION_COLORS = {
|
2010-04-12 03:14:52 +00:00
|
|
|
:error => :red,
|
|
|
|
:warning => :yellow,
|
|
|
|
:compile => :green,
|
2009-11-30 04:17:30 +00:00
|
|
|
:overwrite => :yellow,
|
2010-04-12 03:14:52 +00:00
|
|
|
:create => :green,
|
|
|
|
:remove => :yellow,
|
|
|
|
:exists => :green,
|
2009-11-30 04:17:30 +00:00
|
|
|
:directory => :green,
|
2010-04-12 03:14:52 +00:00
|
|
|
:identical => :green,
|
2010-10-31 07:32:45 +00:00
|
|
|
:convert => :green,
|
|
|
|
:unchanged => :yellow
|
2009-11-30 04:17:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-01 22:17:07 +00:00
|
|
|
|
|
|
|
attr_accessor :actions, :options
|
|
|
|
|
|
|
|
def initialize(*actions)
|
|
|
|
self.options = actions.last.is_a?(Hash) ? actions.pop : {}
|
|
|
|
@actions = DEFAULT_ACTIONS.dup
|
|
|
|
@actions += actions
|
|
|
|
end
|
|
|
|
|
|
|
|
# Record an action that has occurred
|
|
|
|
def record(action, *arguments)
|
2009-12-01 17:50:31 +00:00
|
|
|
msg = ""
|
|
|
|
msg << color(ACTION_COLORS[action]) if Compass.configuration.color_output
|
2010-10-31 07:32:45 +00:00
|
|
|
msg << "#{action_padding(action)}#{action}"
|
2009-12-01 17:50:31 +00:00
|
|
|
msg << color(:clear) if Compass.configuration.color_output
|
2010-10-31 07:32:45 +00:00
|
|
|
msg << " #{arguments.join(' ')}"
|
2009-12-01 17:50:31 +00:00
|
|
|
log msg
|
|
|
|
end
|
|
|
|
|
|
|
|
def red
|
|
|
|
$stderr.write(color(:red))
|
|
|
|
$stdout.write(color(:red))
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$stderr.write(color(:clear))
|
|
|
|
$stdout.write(color(:clear))
|
2009-02-01 22:17:07 +00:00
|
|
|
end
|
|
|
|
|
2011-04-16 07:16:37 +00:00
|
|
|
def yellow
|
|
|
|
$stderr.write(color(:yellow))
|
|
|
|
$stdout.write(color(:yellow))
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$stderr.write(color(:clear))
|
|
|
|
$stdout.write(color(:clear))
|
|
|
|
end
|
|
|
|
|
2009-11-30 04:17:30 +00:00
|
|
|
def color(c)
|
2010-01-21 01:33:13 +00:00
|
|
|
if Compass.configuration.color_output && c && COLORS.has_key?(c.to_sym)
|
2010-02-14 01:44:49 +00:00
|
|
|
if defined?($boring) && $boring
|
|
|
|
""
|
|
|
|
else
|
|
|
|
"\e[#{COLORS[c.to_sym]}m"
|
|
|
|
end
|
2009-11-30 04:17:30 +00:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def emit(msg)
|
|
|
|
print msg
|
|
|
|
end
|
2009-12-01 17:50:31 +00:00
|
|
|
|
2009-02-01 22:17:07 +00:00
|
|
|
# Emit a log message
|
|
|
|
def log(msg)
|
|
|
|
puts msg
|
|
|
|
end
|
|
|
|
|
|
|
|
# add padding to the left of an action that was performed.
|
|
|
|
def action_padding(action)
|
|
|
|
' ' * [(max_action_length - action.to_s.length), 0].max
|
|
|
|
end
|
|
|
|
|
|
|
|
# the maximum length of all the actions known to the logger.
|
|
|
|
def max_action_length
|
|
|
|
@max_action_length ||= actions.inject(0){|memo, a| [memo, a.to_s.length].max}
|
|
|
|
end
|
|
|
|
end
|
2009-05-08 03:08:19 +00:00
|
|
|
|
|
|
|
class NullLogger
|
|
|
|
def record(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log(msg)
|
|
|
|
end
|
2009-12-01 17:50:31 +00:00
|
|
|
|
|
|
|
def red
|
|
|
|
yield
|
|
|
|
end
|
2009-05-08 03:08:19 +00:00
|
|
|
end
|
2009-11-30 04:17:30 +00:00
|
|
|
end
|