From: Daniel Pittman <[email protected]>

We previously had an ordering dependency in the autoflush option, which was
statically read from defaults when the log destination was configured.

We add a hook in the defaults to update the log subsystem, which in turn
updates log destinations, when autoflush is changed.

This would work as desired:
  puppet agent --autoflush --logdest=file

This would not work, as autoflush would be false:
  puppet agent --logdest=file --autoflush

Now those changes propagate correctly.

Paired-with: [email protected]
---
 lib/puppet/defaults.rb                  |    6 +++++-
 lib/puppet/util/log.rb                  |    6 ++++++
 lib/puppet/util/log/destinations.rb     |    2 ++
 spec/unit/util/log/destinations_spec.rb |   13 +++++++++++++
 spec/unit/util/log_spec.rb              |    5 +++++
 5 files changed, 31 insertions(+), 1 deletions(-)

diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb
index 764cbbe..687ac4e 100644
--- a/lib/puppet/defaults.rb
+++ b/lib/puppet/defaults.rb
@@ -14,7 +14,11 @@ module Puppet
 
   setdefaults(:main,
     :trace => [false, "Whether to print stack traces on some errors"],
-    :autoflush => [false, "Whether log files should always flush to disk."],
+    :autoflush => {
+      :default => false,
+      :desc    => "Whether log files should always flush to disk.",
+      :hook    => proc { |value| Log.autoflush = value }
+    },
     :syslogfacility => ["daemon", "What syslog facility to use when logging to
       syslog.  Syslog has a fixed list of valid facilities, and you must
       choose one of those; you cannot just make one up."],
diff --git a/lib/puppet/util/log.rb b/lib/puppet/util/log.rb
index 3fdac3f..ba16900 100644
--- a/lib/puppet/util/log.rb
+++ b/lib/puppet/util/log.rb
@@ -67,6 +67,12 @@ class Puppet::Util::Log
     }
   end
 
+  def Log.autoflush=(v)
+    @destinations.each do |type, dest|
+      dest.autoflush = v if dest.respond_to?(:autoflush=)
+    end
+  end
+
   # Create a new log message.  The primary role of this method is to
   # avoid creating log messages below the loglevel.
   def Log.create(hash)
diff --git a/lib/puppet/util/log/destinations.rb 
b/lib/puppet/util/log/destinations.rb
index 22b3ded..c70edeb 100644
--- a/lib/puppet/util/log/destinations.rb
+++ b/lib/puppet/util/log/destinations.rb
@@ -50,6 +50,8 @@ Puppet::Util::Log.newdesttype :file do
     @file.flush if defined?(@file)
   end
 
+  attr_accessor :autoflush
+
   def initialize(path)
     @name = path
     # first make sure the directory exists
diff --git a/spec/unit/util/log/destinations_spec.rb 
b/spec/unit/util/log/destinations_spec.rb
index 6596c06..710a517 100755
--- a/spec/unit/util/log/destinations_spec.rb
+++ b/spec/unit/util/log/destinations_spec.rb
@@ -22,3 +22,16 @@ describe Puppet::Util::Log.desttypes[:report] do
     dest.handle "my log"
   end
 end
+
+
+describe Puppet::Util::Log.desttypes[:file] do
+  before do
+    File.stubs(:open)           # prevent actually creating the file
+    @class = Puppet::Util::Log.desttypes[:file]
+  end
+
+  it "should default to autoflush false" do
+    @class.new('/tmp/log').autoflush.should == false
+  end
+end
+
diff --git a/spec/unit/util/log_spec.rb b/spec/unit/util/log_spec.rb
index f3fd1b0..4a30d50 100755
--- a/spec/unit/util/log_spec.rb
+++ b/spec/unit/util/log_spec.rb
@@ -136,6 +136,11 @@ describe Puppet::Util::Log do
       Puppet::Util::Log.new(:level => "notice", :message => :foo)
     end
 
+    it "should update Log autoflush when Puppet[:autoflush] is set" do
+      Puppet::Util::Log.expects(:autoflush=).once.with(true)
+      Puppet[:autoflush] = true
+    end
+
     it "should have a method for determining if a tag is present" do
       Puppet::Util::Log.new(:level => "notice", :message => :foo).should 
respond_to(:tagged?)
     end
-- 
1.7.3.5

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

Reply via email to