Class: Compass::Logger

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

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.



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

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.



51
52
53
# File 'lib/compass/logger.rb', line 51

def actions
  @actions
end

#optionsObject

Returns the value of attribute options.



51
52
53
# File 'lib/compass/logger.rb', line 51

def options
  @options
end

#timeObject

Returns the value of attribute time.



51
52
53
# File 'lib/compass/logger.rb', line 51

def time
  @time
end

Instance Method Details

#action_padding(action) ⇒ Object

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



120
121
122
# File 'lib/compass/logger.rb', line 120

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

#color(c) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/compass/logger.rb', line 95

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

Emit a log message without a trailing newline



108
109
110
111
# File 'lib/compass/logger.rb', line 108

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

#greenObject



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

def green
  wrap(:green) { yield }
end

#log(msg) ⇒ Object

Emit a log message with a trailing newline



114
115
116
117
# File 'lib/compass/logger.rb', line 114

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

#max_action_lengthObject

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



125
126
127
# File 'lib/compass/logger.rb', line 125

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



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

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]) 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



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

def red
  wrap(:red) { yield }
end

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



85
86
87
88
89
90
91
92
93
# File 'lib/compass/logger.rb', line 85

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



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

def yellow
  wrap(:yellow) { yield }
end