From: Markus Roberts <mar...@reality.com>

This commit adds a method analogous to Puppet.warn which 1) only logs each
message the first time it is received and 2) only logs the first 100 messages
it receives.  Messages are logged via warn.

This could easily be made more flexible by making the hard limit and effective
log level user settable, if desired.

Signed-off-by: Nick Lewis <n...@puppetlabs.com>
---
Local-branch: ticket/next/5027
 lib/puppet/util/logging.rb     |   11 +++++++++++
 spec/unit/util/logging_spec.rb |   25 +++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/lib/puppet/util/logging.rb b/lib/puppet/util/logging.rb
index bc52b17..b6845b8 100644
--- a/lib/puppet/util/logging.rb
+++ b/lib/puppet/util/logging.rb
@@ -15,6 +15,17 @@ module Puppet::Util::Logging
     end
   end
 
+  def deprication_warning(message)
+    $deprication_warnings ||= Hash.new(0)
+    if $deprication_warnings.length < 100 and ($deprication_warnings[message] 
+= 1) == 1
+      warn message
+    end
+  end
+
+  def clear_deprication_warnings
+    $deprication_warnings.clear if $deprication_warnings
+  end
+
   private
 
   def is_resource?
diff --git a/spec/unit/util/logging_spec.rb b/spec/unit/util/logging_spec.rb
index edc88f1..1585c3e 100755
--- a/spec/unit/util/logging_spec.rb
+++ b/spec/unit/util/logging_spec.rb
@@ -92,4 +92,29 @@ describe Puppet::Util::Logging do
       end
     end
   end
+
+  describe "when sending a deprication warning" do
+    before do
+      @logger.clear_deprication_warnings
+    end
+
+    it "should the message with warn" do
+      @logger.expects(:warn).with('foo')
+      @logger.deprication_warning 'foo'
+    end
+
+    it "should only log each unique message once" do
+      @logger.expects(:warn).with('foo').once
+      5.times { @logger.deprication_warning 'foo' }
+    end
+
+    it "should only log the first 100 messages" do
+      (1..100).each { |i|
+          @logger.expects(:warn).with(i).once
+          @logger.deprication_warning i
+      }
+      @logger.expects(:warn).with(101).never
+      @logger.deprication_warning 101
+    end
+  end
 end
-- 
1.7.4.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to