Permalink
Browse files

Unit test debug logging.

  • Loading branch information...
1 parent 97524da commit 30b9cbc5c1b9ade0aaa89f15df54963da340a73a @FooBarWidget FooBarWidget committed Jun 3, 2010
Showing with 164 additions and 2 deletions.
  1. +23 −2 lib/phusion_passenger/debug_logging.rb
  2. +141 −0 test/ruby/debug_logging_spec.rb
View
25 lib/phusion_passenger/debug_logging.rb
@@ -24,10 +24,19 @@
module PhusionPassenger
module DebugLogging
+ # We don't refer to STDERR directly because STDERR's reference might
+ # change during runtime.
@@log_level = 0
- # We don't refer to STDERR because STDERR's reference might change during runtime.
@@log_device = nil
@@log_filename = nil
+ @@stderr_evaluator = lambda { STDERR }
+
+ def self.included(klass)
+ klass.class_eval do
+ private :debug
+ private :trace
+ end
+ end
def self.log_level=(level)
@@log_level = level
@@ -43,6 +52,18 @@ def self.log_file=(filename)
@@log_device = nil
end
+ def self._log_device
+ return @@log_device
+ end
+
+ def self.stderr_evaluator=(block)
+ if block
+ @@stderr_evaluator = block
+ else
+ @@stderr_evaluator = lambda { STDERR }
+ end
+ end
+
def debug(message)
trace(1, message, 1)
end
@@ -56,7 +77,7 @@ def trace(level, message, nesting_level = 0)
end
output = @@log_device
else
- output = STDERR
+ output = @@stderr_evaluator.call
end
location = caller[nesting_level].sub(/.*phusion_passenger\//, '')
location.sub!(/(.*):.*/, '\1')
View
141 test/ruby/debug_logging_spec.rb
@@ -0,0 +1,141 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'phusion_passenger/debug_logging'
+require 'phusion_passenger/utils/tmpdir'
+require 'stringio'
+
+module PhusionPassenger
+
+describe DebugLogging do
+ after :each do
+ DebugLogging.log_level = 0
+ DebugLogging.log_file = nil
+ DebugLogging.stderr_evaluator = nil
+ end
+
+ class MyClass
+ include DebugLogging
+ end
+
+ def use_log_file!
+ @log_file = PhusionPassenger::Utils.passenger_tmpdir + "/debug.log"
+ DebugLogging.log_file = @log_file
+ end
+
+ describe "#debug" do
+ it "doesn't print the message if log level is 0" do
+ use_log_file!
+ DebugLogging.debug("hello")
+ File.exist?(@log_file).should be_false
+ end
+
+ it "prints the message if log level is 1" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ DebugLogging.debug("hello")
+ File.exist?(@log_file).should be_true
+ end
+
+ it "prints the message if log level is greater than 1" do
+ use_log_file!
+ DebugLogging.log_level = 2
+ DebugLogging.debug("hello")
+ File.exist?(@log_file).should be_true
+ end
+
+ it "prints the location of the calling function" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ DebugLogging.debug("hello")
+ File.read(@log_file).should include("debug_logging_spec.rb")
+ end
+
+ it "prints to STDERR by default" do
+ io = StringIO.new
+ DebugLogging.log_level = 1
+ DebugLogging.stderr_evaluator = lambda { io }
+ DebugLogging.debug("hello")
+ io.string.should include("hello")
+ end
+
+ it "reopens the log file handle if it has been closed" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ DebugLogging.debug("hello")
+ DebugLogging._log_device.close
+ DebugLogging.debug("world")
+ File.read(@log_file).should include("world")
+ end
+
+ it "also works as included method" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ MyClass.new.send(:debug, "hello")
+ File.read(@log_file).should include("hello")
+ end
+
+ it "is private when included" do
+ MyClass.private_method_defined?(:debug)
+ end
+ end
+
+ describe "#trace" do
+ specify "#trace(x, ...) doesn't print the message if the log level is lower than x" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ DebugLogging.trace(2, "hello")
+ File.exist?(@log_file).should be_false
+ end
+
+ specify "#trace(x, ...) prints the message if the log level equals x" do
+ use_log_file!
+ DebugLogging.log_level = 2
+ DebugLogging.trace(2, "hello")
+ File.exist?(@log_file).should be_true
+ end
+
+ specify "#trace(x, ...) prints the message if the log level is greater than x" do
+ use_log_file!
+ DebugLogging.log_level = 3
+ DebugLogging.trace(2, "hello")
+ File.exist?(@log_file).should be_true
+ end
+
+ specify "#trace prints the location of the calling function" do
+ io = StringIO.new
+ DebugLogging.log_level = 1
+ DebugLogging.stderr_evaluator = lambda { io }
+ DebugLogging.trace(1, "hello")
+ io.string.should include("hello")
+ end
+
+ it "prints to STDERR by default" do
+ io = StringIO.new
+ DebugLogging.log_level = 1
+ DebugLogging.stderr_evaluator = lambda { io }
+ DebugLogging.trace(1, "hello")
+ io.string.should include("hello")
+ end
+
+ it "reopens the log file handle if it has been closed" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ DebugLogging.trace(1, "hello")
+ DebugLogging._log_device.close
+ DebugLogging.trace(1, "world")
+ File.read(@log_file).should include("world")
+ end
+
+ it "also works as included method" do
+ use_log_file!
+ DebugLogging.log_level = 1
+ MyClass.new.send(:trace, 1, "hello")
+ File.read(@log_file).should include("hello")
+ end
+
+ it "is private when included" do
+ MyClass.private_method_defined?(:trace)
+ end
+ end
+end
+
+end # module PhusionPassenger

0 comments on commit 30b9cbc

Please sign in to comment.