[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 0.25.5-639-g8f94f35

Nick Lewis nick at puppetlabs.com
Wed Jul 14 10:36:41 UTC 2010


The following commit has been merged in the upstream branch:
commit e419293e58addfd8e4f0612ad121f68038daa14a
Author: Nick Lewis <nick at puppetlabs.com>
Date:   Tue Jul 6 12:06:39 2010 -0700

    [#4114] Added queueing to the log
    
    The log will now queue any log messages created when there is no
    destination, and will flush the queue when a destination is added.

diff --git a/lib/puppet/util/log.rb b/lib/puppet/util/log.rb
index ba23e12..237887e 100644
--- a/lib/puppet/util/log.rb
+++ b/lib/puppet/util/log.rb
@@ -31,6 +31,8 @@ class Puppet::Util::Log
 
     @destinations = {}
 
+    @queued = []
+
     class << self
         include Puppet::Util
         include Puppet::Util::ClassGen
@@ -38,34 +40,24 @@ class Puppet::Util::Log
         attr_reader :desttypes
     end
 
-    # Reset all logs to basics.  Basically just closes all files and undefs
-    # all of the other objects.
-    def Log.close(dest = nil)
-        if dest
-            if @destinations.include?(dest)
-                if @destinations.respond_to?(:close)
-                    @destinations[dest].close
-                end
-                @destinations.delete(dest)
+    # Reset log to basics.  Basically just flushes and closes files and
+    # undefs other objects.
+    def Log.close(destination)
+        if @destinations.include?(destination)
+            if @destinations[destination].respond_to?(:flush)
+                @destinations[destination].flush
             end
-        else
-            @destinations.each { |name, dest|
-                if dest.respond_to?(:flush)
-                    dest.flush
-                end
-                if dest.respond_to?(:close)
-                    dest.close
-                end
-            }
-            @destinations = {}
+            if @destinations[destination].respond_to?(:close)
+                @destinations[destination].close
+            end
+            @destinations.delete(destination)
         end
     end
 
     def self.close_all
-        # And close all logs except the console.
-        destinations.each do |dest|
+        destinations.keys.each { |dest|
             close(dest)
-        end
+        }
     end
 
     # Flush any log destinations that support such operations.
@@ -94,7 +86,7 @@ class Puppet::Util::Log
     end
 
     def Log.destinations
-        return @destinations.keys
+        @destinations
     end
 
     # Yield each valid level in turn
@@ -145,6 +137,8 @@ class Puppet::Util::Log
             else
                 @destinations[dest] = type.new()
             end
+            flushqueue
+            @destinations[dest]
         rescue => detail
             if Puppet[:debug]
                 puts detail.backtrace
@@ -158,15 +152,16 @@ class Puppet::Util::Log
     end
 
     # Route the actual message. FIXME There are lots of things this method
-    # should do, like caching, storing messages when there are not yet
-    # destinations, a bit more.  It's worth noting that there's a potential
-    # for a loop here, if the machine somehow gets the destination set as
+    # should do, like caching and a bit more.  It's worth noting that there's
+    # a potential for a loop here, if the machine somehow gets the destination set as
     # itself.
     def Log.newmessage(msg)
         if @levels.index(msg.level) < @loglevel
             return
         end
 
+        queuemessage(msg) if @destinations.length == 0
+
         @destinations.each do |name, dest|
             threadlock(dest) do
                 dest.handle(msg)
@@ -174,6 +169,18 @@ class Puppet::Util::Log
         end
     end
 
+    def Log.queuemessage(msg)
+        @queued.push(msg)
+    end
+
+    def Log.flushqueue
+        return unless @destinations.size >= 1
+        @queued.each do |msg|
+            Log.newmessage(msg)
+        end
+        @queued.clear
+    end
+
     def Log.sendlevel?(level)
         @levels.index(level) >= @loglevel
     end
diff --git a/lib/puppet/util/log/destinations.rb b/lib/puppet/util/log/destinations.rb
index 002ca36..403733d 100644
--- a/lib/puppet/util/log/destinations.rb
+++ b/lib/puppet/util/log/destinations.rb
@@ -218,12 +218,17 @@ end
 Puppet::Util::Log.newdesttype :array do
     match "Array"
 
-    def initialize(array)
-        @array = array
+    attr_accessor :messages
+    def initialize
+        @messages = []
     end
 
     def handle(msg)
-        @array << msg
+        @messages << msg
+    end
+
+    def close
+        @messages.clear
     end
 end
 
diff --git a/spec/unit/util/log_spec.rb b/spec/unit/util/log_spec.rb
index 7aaa580..df3c36f 100755
--- a/spec/unit/util/log_spec.rb
+++ b/spec/unit/util/log_spec.rb
@@ -5,8 +5,19 @@ Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f
 require 'puppet/util/log'
 
 describe Puppet::Util::Log do
+    it "should write a given message to the specified destination" do
+        Puppet::Util::Log.newdestination(:array)
+        Puppet::Util::Log.new(:level => :notice, :message => "foo")
+        message = Puppet::Util::Log.destinations[:array].messages.shift.message
+        message.should == "foo"
+
+        Puppet::Util::Log.close_all
+    end
+
     it "should be able to close all log destinations" do
-        Puppet::Util::Log.expects(:destinations).returns %w{foo bar}
+        destinations = stub_everything('destinations')
+        destinations.stubs(:keys).returns %w{foo bar}
+        Puppet::Util::Log.expects(:destinations).returns(destinations)
         Puppet::Util::Log.expects(:close).with("foo")
         Puppet::Util::Log.expects(:close).with("bar")
 
@@ -84,6 +95,12 @@ describe Puppet::Util::Log do
             Puppet::Util::Log.new(:level => :notice, :message => :foo).message.should == "foo"
         end
 
+        it "should flush the log queue when the first destination is specified" do
+            Puppet::Util::Log.expects(:flushqueue)
+            Puppet::Util::Log.newdestination(:array)
+            Puppet::Util::Log.close_all
+        end
+
         it "should convert the level to a symbol if it's passed in as a string" do
             Puppet::Util::Log.new(:level => "notice", :message => :foo).level.should == :notice
         end
diff --git a/spec/unit/util/logging_spec.rb b/spec/unit/util/logging_spec.rb
index aee308e..41b07d4 100755
--- a/spec/unit/util/logging_spec.rb
+++ b/spec/unit/util/logging_spec.rb
@@ -40,6 +40,12 @@ describe Puppet::Util::Logging do
             @logger.notice "foo"
         end
 
+        it "should queue logs sent without a specified destination" do
+            Puppet::Util::Log.expects(:queuemessage)
+
+            @logger.notice "foo"
+        end
+
         it "should use the path of any provided resource type" do
             resource = Puppet::Type.type(:mount).new :name => "foo"
 

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list