diff options
| author | Nick Lewis <nick@puppetlabs.com> | 2010-07-06 12:06:39 -0700 |
|---|---|---|
| committer | Nick Lewis <nick@puppetlabs.com> | 2010-07-07 11:51:15 -0700 |
| commit | e419293e58addfd8e4f0612ad121f68038daa14a (patch) | |
| tree | b8ab8047c6d5ca3749219cd02085c6d0372a0b39 /lib | |
| parent | 4b00c6af911b447265fc3e7ab19bb076840bfef1 (diff) | |
| download | puppet-e419293e58addfd8e4f0612ad121f68038daa14a.tar.gz puppet-e419293e58addfd8e4f0612ad121f68038daa14a.tar.xz puppet-e419293e58addfd8e4f0612ad121f68038daa14a.zip | |
[#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.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/puppet/util/log.rb | 59 | ||||
| -rw-r--r-- | lib/puppet/util/log/destinations.rb | 11 |
2 files changed, 41 insertions, 29 deletions
diff --git a/lib/puppet/util/log.rb b/lib/puppet/util/log.rb index ba23e120a..237887eb5 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 002ca3624..403733d35 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 |
