~ubuntu-branches/ubuntu/karmic/puppet/karmic-security

« back to all changes in this revision

Viewing changes to lib/puppet/util/log.rb

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2009-04-13 17:12:47 UTC
  • mfrom: (1.1.9 upstream) (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090413171247-qou3gsiihama09cm
Tags: 0.24.8-1
* New upstream release
* debian/control: Add Nigel Kersten and myself as uploaders
* debian/changelog: wrap long lines
* debian/watch: ignore release candidates
* debian/compat: bump to 5
* debian/control: bump Standards-Version (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require 'syslog'
 
2
require 'puppet/util/tagging'
2
3
 
3
4
# Pass feedback to the user.  Log levels are modeled after syslog's, and it is
4
5
# expected that that will be the most common log destination.  Supports
5
6
# multiple destinations, one of which is a remote server.
6
7
class Puppet::Util::Log
7
8
    include Puppet::Util
 
9
    include Puppet::Util::Tagging
8
10
 
9
11
    @levels = [:debug,:info,:notice,:warning,:err,:alert,:emerg,:crit]
10
12
    @loglevel = 2
244
246
    newdesttype :console do
245
247
                
246
248
        
247
 
        PINK = {:console => "", :html => "FFA0A0"}
248
 
        GREEN = {:console => "", :html => "00CD00"}
249
 
        YELLOW = {:console => "", :html => "FFFF60"}
250
 
        SLATE = {:console => "", :html => "80A0FF"}
251
 
        ORANGE = {:console => "", :html => "FFA500"}
252
 
        BLUE = {:console => "", :html => "40FFFF"}
253
 
        RESET = {:console => "", :html => ""}
 
249
        RED     = {:console => "", :html => "FFA0A0"}
 
250
        GREEN   = {:console => "", :html => "00CD00"}
 
251
        YELLOW  = {:console => "", :html => "FFFF60"}
 
252
        BLUE    = {:console => "", :html => "80A0FF"}
 
253
        PURPLE  = {:console => "", :html => "FFA500"}
 
254
        CYAN    = {:console => "", :html => "40FFFF"}
 
255
        WHITE   = {:console => "", :html => "FFFFFF"}
 
256
        HRED    = {:console => "", :html => "FFA0A0"}
 
257
        HGREEN  = {:console => "", :html => "00CD00"}
 
258
        HYELLOW = {:console => "", :html => "FFFF60"}
 
259
        HBLUE   = {:console => "", :html => "80A0FF"}
 
260
        HPURPLE = {:console => "", :html => "FFA500"}
 
261
        HCYAN   = {:console => "", :html => "40FFFF"}
 
262
        HWHITE  = {:console => "", :html => "FFFFFF"}
 
263
        RESET   = {:console => "", :html => ""}
254
264
 
255
265
        @@colormap = {
256
 
            :debug => SLATE,
 
266
            :debug => WHITE,
257
267
            :info => GREEN,
258
 
            :notice => PINK,
259
 
            :warning => ORANGE,
260
 
            :err => YELLOW,
261
 
            :alert => BLUE,
262
 
            :emerg => RESET,
263
 
            :crit => RESET
 
268
            :notice => CYAN,
 
269
            :warning => YELLOW,
 
270
            :err => HPURPLE,
 
271
            :alert => RED,
 
272
            :emerg => HRED,
 
273
            :crit => HRED
264
274
        }
265
275
        
266
276
        def colorize(level, str)
359
369
        end
360
370
 
361
371
        def handle(msg)
362
 
            # Only add messages from objects, since anything else is
363
 
            # probably unrelated to this run.
364
 
            if msg.objectsource?
365
 
                @report.newlog(msg)
366
 
            end
 
372
            @report.newlog(msg)
367
373
        end
368
374
    end
369
375
 
462
468
        @levels.include?(level)
463
469
    end
464
470
 
465
 
    attr_accessor :level, :message, :time, :tags, :remote
 
471
    attr_accessor :level, :message, :time, :remote
466
472
    attr_reader :source
467
473
 
468
474
    def initialize(args)
469
475
        unless args.include?(:level) && args.include?(:message)
470
 
            raise Puppet::DevError, "Puppet::Util::Log called incorrectly"
 
476
            raise ArgumentError, "Puppet::Util::Log called incorrectly"
471
477
        end
472
478
 
473
479
        if args[:level].class == String
475
481
        elsif args[:level].class == Symbol
476
482
            @level = args[:level]
477
483
        else
478
 
            raise Puppet::DevError,
479
 
                "Level is not a string or symbol: #{args[:level].class}"
 
484
            raise ArgumentError, "Level is not a string or symbol: #{args[:level].class}"
480
485
        end
481
486
 
482
 
        # Just return unless we're actually at a level we should send
483
 
        #return unless self.class.sendlevel?(@level)
484
 
 
485
487
        @message = args[:message].to_s
486
488
        @time = Time.now
487
 
        # this should include the host name, and probly lots of other
488
 
        # stuff, at some point
489
 
        unless self.class.validlevel?(level)
490
 
            raise Puppet::DevError, "Invalid message level #{level}"
491
 
        end
492
 
 
493
 
        if args.include?(:tags)
494
 
            @tags = args[:tags]
495
 
        end
496
 
 
497
 
        if args.include?(:source)
498
 
            self.source = args[:source]
499
 
        else
500
 
            @source = "Puppet"
501
 
        end
 
489
 
 
490
        raise ArgumentError, "Invalid log level %s" % level unless self.class.validlevel?(level)
 
491
 
 
492
        if tags = args[:tags]
 
493
            tags.each { |t| self.tag(t) }
 
494
        end
 
495
 
 
496
        self.source = args[:source] || "Puppet"
 
497
 
 
498
        # Tag myself with my log level
 
499
        tag(level)
502
500
 
503
501
        Log.newmessage(self)
504
502
    end
505
503
 
506
 
    # Was the source of this log an object?
507
 
    def objectsource?
508
 
        if defined? @objectsource and @objectsource
509
 
            @objectsource
510
 
        else
511
 
            false
512
 
        end
513
 
    end
514
 
 
515
504
    # If they pass a source in to us, we make sure it is a string, and
516
505
    # we retrieve any tags we can.
517
506
    def source=(source)
519
508
        # We can't just check for whether it responds to :path, because
520
509
        # plenty of providers respond to that in their normal function.
521
510
        if (source.is_a?(Puppet::Type) or source.is_a?(Puppet::Parameter)) and source.respond_to?(:path)
522
 
            @objectsource = true
523
511
            @source = source.path
524
512
        else
525
 
            @objectsource = false
526
513
            @source = source.to_s
527
514
        end
528
 
        unless defined? @tags and @tags
529
 
            if source.respond_to?(:tags)
530
 
                @tags = source.tags
531
 
            end
 
515
        if source.respond_to?(:tags)
 
516
            source.tags.each { |t| tag(t) }
532
517
        end
533
518
    end
534
519
 
535
 
    def tagged?(tag)
536
 
        @tags.detect { |t| t.to_s == tag.to_s }
537
 
    end
538
 
 
539
520
    def to_report
540
521
        "%s %s (%s): %s" % [self.time, self.source, self.level, self.to_s]
541
522
    end
544
525
        return @message
545
526
    end
546
527
end
 
528
 
 
529
# This is for backward compatibility from when we changed the constant to Puppet::Util::Log
 
530
# because the reports include the constant name.  Apparently the alias was created in
 
531
# March 2007, should could probably be removed soon.
547
532
Puppet::Log = Puppet::Util::Log
548