summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2005-06-28 04:15:48 +0000
committerLuke Kanies <luke@madstop.com>2005-06-28 04:15:48 +0000
commitb6b1f5a8f08859d6c3975beb3ecbcd39ff5f250e (patch)
tree5a3998d34392cf94472c23cad8df9b0b5e1a7351 /lib
parente3c32834c4cc5f7e378d2929b99253982ca264d8 (diff)
downloadpuppet-b6b1f5a8f08859d6c3975beb3ecbcd39ff5f250e.tar.gz
puppet-b6b1f5a8f08859d6c3975beb3ecbcd39ff5f250e.tar.xz
puppet-b6b1f5a8f08859d6c3975beb3ecbcd39ff5f250e.zip
logging now exactly supports the list of levels that syslog supports, and multiple destinations (syslog, files, and console) are now supported
git-svn-id: https://reductivelabs.com/svn/puppet/library/trunk@311 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet.rb36
-rw-r--r--lib/puppet/event.rb8
-rw-r--r--lib/puppet/log.rb115
-rw-r--r--lib/puppet/statechange.rb2
-rw-r--r--lib/puppet/transaction.rb6
-rw-r--r--lib/puppet/transportable.rb6
-rw-r--r--lib/puppet/type.rb4
-rw-r--r--lib/puppet/type/component.rb2
-rw-r--r--lib/puppet/type/file.rb12
-rw-r--r--lib/puppet/type/package.rb16
-rw-r--r--lib/puppet/type/service.rb2
-rw-r--r--lib/puppet/type/state.rb4
12 files changed, 141 insertions, 72 deletions
diff --git a/lib/puppet.rb b/lib/puppet.rb
index 5daa2b1a6..c144a6bb4 100644
--- a/lib/puppet.rb
+++ b/lib/puppet.rb
@@ -3,17 +3,10 @@
# $Id$
require 'singleton'
+require 'puppet/log'
# XXX see the bottom of the file for further inclusions
-PINK=""
-GREEN=""
-YELLOW=""
-SLATE=""
-ORANGE=""
-BLUE=""
-RESET=""
-
#------------------------------------------------------------
# the top-level module
#
@@ -31,32 +24,18 @@ module Puppet
@@config[:statefile] = "/var/puppet/log/state"
- loglevels = [:debug,:verbose,:notice,:warning,:error]
-
# handle the different message levels
# XXX this should be redone to treat log-levels like radio buttons
# pick one, and it and all above it will be logged
- loglevels.each { |level|
+ Puppet::Log.levels.each { |level|
define_method(level,proc { |args|
- Puppet.message(level,args)
+ Puppet::Log.create(level,args)
})
module_function level
# default to enabling all notice levels except debug
- @@config[level] = true unless level == :debug
+ @@config[level] = true unless level == :notice
}
- def Puppet.message(level,*ary)
- msg = ary.join(" ")
-
- if @@config[level]
- Puppet::Message.new(
- :level => level,
- :source => "Puppet",
- :message => msg
- )
- end
- end
-
# set up our configuration
def Puppet.init(args)
args.each {|p,v|
@@ -64,12 +43,6 @@ module Puppet
}
end
- # just print any messages we get
- # we should later behave differently depending on the message
- def Puppet.newmessage(msg)
- puts msg
- end
-
# configuration parameter access and stuff
def Puppet.[](param)
return @@config[param]
@@ -83,5 +56,4 @@ module Puppet
end
require 'puppet/storage'
-require 'puppet/message'
require 'puppet/type'
diff --git a/lib/puppet/event.rb b/lib/puppet/event.rb
index 51f97e06e..a8d818b39 100644
--- a/lib/puppet/event.rb
+++ b/lib/puppet/event.rb
@@ -38,22 +38,22 @@ module Puppet
# to the "old" object rather than "new"
# but we're pretty far from that being a problem
if transaction.triggercount(self) > 0
- Puppet.verbose "%s has already run" % self
+ Puppet.debug "%s has already run" % self
else
- Puppet.verbose "'%s' matched '%s'; triggering '%s' on '%s'" %
+ Puppet.debug "'%s' matched '%s'; triggering '%s' on '%s'" %
[@source,@event,@method,@target]
begin
if @target.respond_to?(@method)
@target.send(@method)
else
- Puppet.verbose "'%s' of type '%s' does not respond to '%s'" %
+ Puppet.debug "'%s' of type '%s' does not respond to '%s'" %
[@target,@target.class,@method.inspect]
end
rescue => detail
# um, what the heck do i do when an object fails to refresh?
# shouldn't that result in the transaction rolling back?
# XXX yeah, it should
- Puppet.error "'%s' failed to %s: '%s'" %
+ Puppet.err "'%s' failed to %s: '%s'" %
[@target,@method,detail]
raise
#raise "We need to roll '%s' transaction back" %
diff --git a/lib/puppet/log.rb b/lib/puppet/log.rb
index e9d2066f9..9cfb80193 100644
--- a/lib/puppet/log.rb
+++ b/lib/puppet/log.rb
@@ -1,29 +1,125 @@
# $Id$
+PINK=""
+GREEN=""
+YELLOW=""
+SLATE=""
+ORANGE=""
+BLUE=""
+RESET=""
+
+require 'syslog'
+
module Puppet
#------------------------------------------------------------
# provide feedback of various types to the user
# modeled after syslog messages
# each level of message prints in a different color
- class Message
+ class Log
@@messages = Array.new
+
+ @@levels = [:debug,:info,:notice,:warning,:err,:alert,:emerg,:crit]
+ @@loglevel = :notice
+ @@logdest = :console
+
@@colors = {
:debug => SLATE,
:info => ORANGE,
:notice => PINK,
:warning => GREEN,
:err => YELLOW,
- :alert => RESET,
+ :alert => BLUE,
:emerg => RESET,
:crit => RESET
}
- attr_accessor :level, :message, :source
+ def Log.close
+ if defined? @@logfile
+ @@logfile.close
+ @@logfile = nil
+ end
+
+ if defined? @@syslog
+ @@syslog = nil
+ end
+ end
+
+ def Log.create(level,*ary)
+ msg = ary.join(" ")
+
+ if @@levels.index(@@loglevel) >= @@levels.index(level)
+ Puppet::Log.new(
+ :level => level,
+ :source => "Puppet",
+ :message => msg
+ )
+ end
+ end
+
+ def Log.levels
+ return @@levels.dup
+ end
+
+ def Log.destination(dest)
+ if dest == "syslog" || dest == :syslog
+ unless defined? @@syslog
+ @@syslog = Syslog.open("puppet")
+ end
+ @@logdest = :syslog
+ elsif dest =~ /^\//
+ if defined? @@logfile
+ @@logfile.close
+ end
+ begin
+ @@logfile = File.open(dest,"w")
+ rescue => detail
+ raise
+ end
+ @@logdest = :file
+ else
+ @@logdest = :console
+ end
+ end
+
+ def Log.level(level)
+ unless level.is_a?(Symbol)
+ level = level.intern
+ end
+
+ unless @@loglevels.include?(level)
+ raise "Invalid loglevel %s" % level
+ end
+
+ @@loglevel = @@loglevels.index(level)
+ end
+
+ def Log.newmessage(msg)
+ case @@logdest
+ when :syslog:
+ if msg.source == "Puppet"
+ @@syslog.send(msg.level,msg.to_s)
+ else
+ @@syslog.send(msg.level,"(%s) %s" % [msg.source,msg.to_s])
+ end
+ when :file:
+ unless defined? @@logfile
+ raise "Log file must be defined before we can log to it"
+ end
+ @@logfile.puts("%s %s (%s): %s" %
+ [msg.time,msg.source,msg.level,msg.to_s])
+ else
+ puts @@colors[msg.level] + "%s (%s): %s" % [
+ msg.source, msg.level, msg.to_s
+ ] + RESET
+ end
+ end
+
+ attr_accessor :level, :message, :source, :time
def initialize(args)
unless args.include?(:level) && args.include?(:message) &&
args.include?(:source)
- raise "Puppet::Message called incorrectly"
+ raise "Puppet::Log called incorrectly"
end
if args[:level].class == String
@@ -34,7 +130,7 @@ module Puppet
raise "Level is not a string or symbol: #{args[:level].class}"
end
@message = args[:message]
- @source = args[:source]
+ @source = args[:source] || "Puppet"
@time = Time.now
# this should include the host name, and probly lots of other
# stuff, at some point
@@ -42,8 +138,8 @@ module Puppet
raise "Invalid message level #{level}"
end
+ Log.newmessage(self)
@@messages.push(self)
- Puppet.newmessage(self)
end
def to_s
@@ -53,9 +149,10 @@ module Puppet
#return @@colors[@level] + "%s %s (%s): %s" % [
# @time, @source, @level, @message
#] + RESET
- return @@colors[@level] + "%s (%s): %s" % [
- @source, @level, @message
- ] + RESET
+ return @message
+ #return @@colors[@level] + "%s (%s): %s" % [
+ # @source, @level, @message
+ #] + RESET
end
end
#------------------------------------------------------------
diff --git a/lib/puppet/statechange.rb b/lib/puppet/statechange.rb
index 17f5c80f9..4b0cdda5a 100644
--- a/lib/puppet/statechange.rb
+++ b/lib/puppet/statechange.rb
@@ -52,7 +52,7 @@ module Puppet
:message => self.to_s
)
rescue => detail
- Puppet.error "%s failed: %s" % [self.to_s,detail]
+ Puppet.err "%s failed: %s" % [self.to_s,detail]
raise
# there should be a way to ask the state what type of event
# it would have generated, but...
diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb
index e1b42aedd..1a3a7361a 100644
--- a/lib/puppet/transaction.rb
+++ b/lib/puppet/transaction.rb
@@ -51,7 +51,7 @@ class Transaction
events = [change.forward].flatten
#@@changed.push change.state.parent
rescue => detail
- Puppet.error("%s failed: %s" % [change,detail])
+ Puppet.err("%s failed: %s" % [change,detail])
raise
# at this point, we would normally do error handling
# but i haven't decided what to do for that yet
@@ -62,7 +62,7 @@ class Transaction
end
if events.nil?
- Puppet.verbose "No events returned?"
+ Puppet.debug "No events returned?"
else
# first handle the subscriptions on individual objects
events.each { |event|
@@ -126,7 +126,7 @@ class Transaction
change.backward
#@@changed.push change.state.parent
rescue => detail
- Puppet.error("%s rollback failed: %s" % [change,detail])
+ Puppet.err("%s rollback failed: %s" % [change,detail])
# at this point, we would normally do error handling
# but i haven't decided what to do for that yet
# so just record that a sync failed for a given object
diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb
index 1f25e2e97..519ca7460 100644
--- a/lib/puppet/transportable.rb
+++ b/lib/puppet/transportable.rb
@@ -56,7 +56,7 @@ module Puppet
# which may or may not be a good thing...
retobj = type.new(self)
#rescue => detail
- # Puppet.error "Failed to create %s: %s" % [type.name,detail]
+ # Puppet.err "Failed to create %s: %s" % [type.name,detail]
# puts self.class
# puts self.inspect
# exit
@@ -89,7 +89,7 @@ module Puppet
if type.allowedmethod(name)
type.send(self.name,self.args)
else
- Puppet.error("%s does not respond to %s" % [self.type,self.name])
+ Puppet.err("%s does not respond to %s" % [self.type,self.name])
end
else
raise "Could not find object type %s" % setting.type
@@ -124,7 +124,7 @@ module Puppet
raise "TransBuckets must have names"
end
unless defined? @type
- Puppet.verbose "TransBucket '%s' has no type" % @name
+ Puppet.debug "TransBucket '%s' has no type" % @name
end
hash = {
:name => @name,
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index 02661f64a..81c20ceec 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -166,7 +166,7 @@ class Type < Puppet::Element
def Type.initvars
@objects = Hash.new
@actions = Hash.new
- #Puppet.verbose "initing validstates for %s" % self
+ #Puppet.debug "initing validstates for %s" % self
@validstates = {}
@validparameters = {}
@@ -670,7 +670,7 @@ class Type < Puppet::Element
# rather than 'each'
def evaluate
unless defined? @evalcount
- Puppet.error "No evalcount defined on '%s' of type '%s'" %
+ Puppet.err "No evalcount defined on '%s' of type '%s'" %
[self.name,self.class]
end
# if we're a metaclass and we've already evaluated once...
diff --git a/lib/puppet/type/component.rb b/lib/puppet/type/component.rb
index f6d127d48..74db00d00 100644
--- a/lib/puppet/type/component.rb
+++ b/lib/puppet/type/component.rb
@@ -26,7 +26,7 @@ module Puppet
def initialize(args)
@children = []
super(args)
- Puppet.verbose "Made component with name %s" % self.name
+ Puppet.debug "Made component with name %s" % self.name
end
# now we decide whether a transaction is dumb, and just accepts
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb
index 2330d2622..f9d966608 100644
--- a/lib/puppet/type/file.rb
+++ b/lib/puppet/type/file.rb
@@ -56,7 +56,7 @@ module Puppet
if hash.include?(@checktype)
@should = hash[@checktype]
else
- Puppet.verbose "Found checksum for %s but not of type %s" %
+ Puppet.debug "Found checksum for %s but not of type %s" %
[self.parent[:path],@checktype]
@should = nil
end
@@ -113,7 +113,7 @@ module Puppet
[state[self.parent[:path]][@checktype],@is]
result = true
else
- Puppet.verbose "Creating checksum %s for %s of type %s" %
+ Puppet.debug "Creating checksum %s for %s of type %s" %
[@is,self.parent[:path],@checktype]
result = false
end
@@ -159,7 +159,7 @@ module Puppet
end
unless self.parent.stat
- Puppet.error "File '%s' does not exist; cannot chown" %
+ Puppet.err "File '%s' does not exist; cannot chown" %
self.parent[:path]
end
@@ -210,7 +210,7 @@ module Puppet
end
unless self.parent.stat
- Puppet.error "File '%s' does not exist; cannot chmod" %
+ Puppet.err "File '%s' does not exist; cannot chmod" %
self.parent[:path]
return
end
@@ -307,7 +307,7 @@ module Puppet
end
unless self.parent.stat
- Puppet.error "File '%s' does not exist; cannot chgrp" %
+ Puppet.err "File '%s' does not exist; cannot chgrp" %
self.parent[:path]
return
end
@@ -347,7 +347,7 @@ module Puppet
# a wrapper method to make sure the file exists before doing anything
def retrieve
unless stat = self.stat(true)
- Puppet.verbose "File %s does not exist" % self[:path]
+ Puppet.debug "File %s does not exist" % self[:path]
@states.each { |name,state|
state.is = -1
}
diff --git a/lib/puppet/type/package.rb b/lib/puppet/type/package.rb
index b2b0f8832..2da861507 100644
--- a/lib/puppet/type/package.rb
+++ b/lib/puppet/type/package.rb
@@ -115,10 +115,10 @@ module Puppet
}
hash.each { |var,value|
if states.include?(var)
- Puppet.verbose "%s is a set state" % var.inspect
+ Puppet.debug "%s is a set state" % var.inspect
states[var].is = value
else
- Puppet.verbose "%s is not a set state" % var.inspect
+ Puppet.debug "%s is not a set state" % var.inspect
if object[var] and object[var] != value
Puppet.warning "Overriding %s => %s on %s with %s" %
[var,object[var],name,value]
@@ -128,12 +128,12 @@ module Puppet
# swap the values if we're a state
if states.include?(var)
- Puppet.verbose "Swapping %s because it's a state" % var
+ Puppet.debug "Swapping %s because it's a state" % var
states[var].is = value
states[var].should = nil
else
- Puppet.verbose "%s is not a state" % var.inspect
- Puppet.verbose "States are %s" % states.keys.collect { |st|
+ Puppet.debug "%s is not a state" % var.inspect
+ Puppet.debug "States are %s" % states.keys.collect { |st|
st.inspect
}.join(" ")
end
@@ -286,10 +286,10 @@ module Puppet
# of information
process.each { |line|
case line
- when /^$/ then
+ when /^$/:
packages.push Puppet::Type::Package.installedpkg(hash)
hash.clear
- when /\s*(\w+):\s+(.+)/
+ when /\s*(\w+):\s+(.+)/:
name = $1
value = $2
if names.include?(name)
@@ -297,7 +297,7 @@ module Puppet
else
raise "Could not find %s" % name
end
- when /\s+\d+.+/
+ when /\s+\d+.+/:
# nothing; we're ignoring the FILES info
end
}
diff --git a/lib/puppet/type/service.rb b/lib/puppet/type/service.rb
index 98004ca58..99c43b1d5 100644
--- a/lib/puppet/type/service.rb
+++ b/lib/puppet/type/service.rb
@@ -140,7 +140,7 @@ module Puppet
begin
retvalue = ::File.stat(dir).directory?
rescue => detail
- Puppet.verbose("Directory %s does not exist: %s" % [dir,detail])
+ Puppet.debug("Directory %s does not exist: %s" % [dir,detail])
# just ignore it
end
# disallow relative paths
diff --git a/lib/puppet/type/state.rb b/lib/puppet/type/state.rb
index 0ec91d4c1..306b2e5ce 100644
--- a/lib/puppet/type/state.rb
+++ b/lib/puppet/type/state.rb
@@ -41,10 +41,10 @@ class State < Puppet::Element
# if we're not in sync, return a statechange capable of putting us
# in sync
def evaluate
- Puppet.verbose "evaluating %s" % self
+ Puppet.debug "evaluating %s" % self
self.retrieve
if self.insync?
- Puppet.verbose "%s is in sync" % self
+ Puppet.debug "%s is in sync" % self
return nil
else
return Puppet::StateChange.new(self)