Skip to content
Browse files

Merge branch 'stable'

* stable:
  Add a logger object for better integration with frameworks.
  • Loading branch information...
2 parents c041cad + f6250c6 commit 61b2162d9e606e3c5838e21de0104ba643d851ca @chriseppstein chriseppstein committed Jun 19, 2011
Showing with 160 additions and 5 deletions.
  1. +1 −0 lib/sass.rb
  2. +15 −0 lib/sass/logger.rb
  3. +32 −0 lib/sass/logger/base.rb
  4. +50 −0 lib/sass/logger/log_level.rb
  5. +3 −5 lib/sass/util.rb
  6. +59 −0 test/sass/logger_test.rb
View
1 lib/sass.rb
@@ -65,6 +65,7 @@ def self.compile_file(filename, *args)
end
end
+require 'sass/logger'
require 'sass/util'
require 'sass/engine'
View
15 lib/sass/logger.rb
@@ -0,0 +1,15 @@
+module Sass::Logger
+
+end
+
+require "sass/logger/log_level"
+require "sass/logger/base"
+
+module Sass
+
+ class << self
+ attr_accessor :logger
+ end
+
+ self.logger = Sass::Logger::Base.new
+end
View
32 lib/sass/logger/base.rb
@@ -0,0 +1,32 @@
+require 'sass/logger/log_level'
+
+class Sass::Logger::Base
+
+ include Sass::Logger::LogLevel
+
+ attr_accessor :log_level
+ attr_accessor :disabled
+
+ log_level :trace
+ log_level :debug
+ log_level :info
+ log_level :warn
+ log_level :error
+
+ def initialize(log_level = :debug)
+ self.log_level = log_level
+ end
+
+ def logging_level?(level)
+ !disabled && self.class.log_level?(level, log_level)
+ end
+
+ def log(level, message)
+ self._log(level, message) if logging_level?(level)
+ end
+
+ def _log(level, message)
+ Kernel::warn(message)
+ end
+
+end
View
50 lib/sass/logger/log_level.rb
@@ -0,0 +1,50 @@
+module Sass
+ module Logger
+ module LogLevel
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def inherited(subclass)
+ subclass.log_levels = subclass.superclass.log_levels.dup
+ end
+
+ def log_levels
+ @log_levels ||= {}
+ end
+
+ def log_levels=(levels)
+ @log_levels = levels
+ end
+
+ def log_level?(level, min_level)
+ log_levels[level] >= log_levels[min_level]
+ end
+
+ def log_level(name, options = {})
+ if options[:prepend]
+ level = log_levels.values.min
+ level = level.nil? ? 0 : level - 1
+ else
+ level = log_levels.values.max
+ level = level.nil? ? 0 : level + 1
+ end
+ # (require 'ruby-debug'; debugger)
+ log_levels.update(name => level)
+ define_logger(name)
+ end
+
+ def define_logger(name, options = {})
+ class_eval %Q{
+ def #{name}(message)
+ #{options.fetch(:to, :log)}(#{name.inspect}, message)
+ end
+ }
+ end
+ end
+
+ end
+ end
+end
View
8 lib/sass/util.rb
@@ -293,19 +293,17 @@ def silence_warnings
#
# @yield A block in which no Sass warnings will be printed
def silence_sass_warnings
- old_silence_warnings = @@silence_warnings
- @@silence_warnings = true
+ old_level, Sass.logger.log_level = Sass.logger.log_level, :error
yield
ensure
- @@silence_warnings = old_silence_warnings
+ Sass.logger.log_level = old_level
end
# The same as `Kernel#warn`, but is silenced by \{#silence\_sass\_warnings}.
#
# @param msg [String]
def sass_warn(msg)
- return if @@silence_warnings
- warn(msg)
+ Sass.logger.warn(msg)
end
## Cross Rails Version Compatibility
View
59 test/sass/logger_test.rb
@@ -0,0 +1,59 @@
+#!/usr/bin/env ruby
+require File.dirname(__FILE__) + '/../test_helper'
+require 'pathname'
+
+class LoggerTest < Test::Unit::TestCase
+
+ class InterceptedLogger < Sass::Logger::Base
+
+ attr_accessor :messages
+
+ def initialize(*args)
+ super
+ self.messages = []
+ end
+
+ def reset!
+ self.messages = []
+ end
+
+ def _log(*args)
+ messages << [args]
+ end
+
+ end
+
+ def test_global_sass_logger_instance_exists
+ assert Sass.logger.respond_to?(:warn)
+ end
+
+ def test_log_level_orders
+ logged_levels = {
+ :trace => [ [], [:trace, :debug, :info, :warn, :error]],
+ :debug => [ [:trace], [:debug, :info, :warn, :error]],
+ :info => [ [:trace, :debug], [:info, :warn, :error]],
+ :warn => [ [:trace, :debug, :info], [:warn, :error]],
+ :error => [ [:trace, :debug, :info, :warn], [:error]]
+ }
+ logged_levels.each do |level, (should_not_be_logged, should_be_logged)|
+ logger = Sass::Logger::Base.new(level)
+ should_not_be_logged.each do |should_level|
+ assert !logger.logging_level?(should_level)
+ end
+ should_be_logged.each do |should_level|
+ assert logger.logging_level?(should_level)
+ end
+ end
+ end
+
+ def test_logging_can_be_disabled
+ logger = InterceptedLogger.new
+ # (require 'ruby-debug'; debugger)
+ logger.error("message #1")
+ assert_equal 1, logger.messages.size
+ logger.reset!
+ logger.disabled = true
+ logger.error("message #2")
+ assert_equal 0, logger.messages.size
+ end
+end

0 comments on commit 61b2162

Please sign in to comment.
Something went wrong with that request. Please try again.