summaryrefslogtreecommitdiffstats
path: root/lib/puppet/log.rb
blob: 85058840d72c8a726cec7196a58def7108707c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
require 'syslog'

module Puppet
    # Pass feedback to the user.  Log levels are modeled after syslog's, and it is
    # expected that that will be the most common log destination.  Supports
    # multiple destinations, one of which is a remote server.
	class Log
        PINK=""
        GREEN=""
        YELLOW=""
        SLATE=""
        ORANGE=""
        BLUE=""
        RESET=""

        @levels = [:debug,:info,:notice,:warning,:err,:alert,:emerg,:crit]
        @loglevel = 2

        @desttypes = {}

        # A type of log destination.
        class Destination
            class << self
                attr_accessor :name
            end

            # Mark the things we're supposed to match.
            def self.match(obj)
                @matches ||= []
                @matches << obj
            end

            # See whether we match a given thing.
            def self.match?(obj)
                if obj.is_a? String and obj =~ /^\w+$/
                    obj = obj.downcase.intern
                end
                @matches.each do |thing|
                    return true if thing === obj
                end
                return false
            end

            def name
                if defined? @name
                    return @name
                else
                    return self.class.name
                end
            end

            # Set how to handle a message.
            def self.sethandler(&block)
                define_method(:handle, &block)
            end

            # Mark how to initialize our object.
            def self.setinit(&block)
                define_method(:initialize, &block)
            end
        end

        # Create a new destination type.
        def self.newdesttype(name, options = {}, &block)
            dest = Class.new(Destination)
            name = name.to_s.downcase.intern
            dest.name = name
            dest.match(dest.name)

            const_set("Dest" + name.to_s.capitalize, dest)

            @desttypes[dest.name] = dest

            options.each do |option, value|
                begin
                    dest.send(option.to_s + "=", value)
                rescue NoMethodError
                    raise "%s is an invalid option for log destinations" % option
                end
            end

            dest.class_eval(&block)

            return dest
        end

        @destinations = {}

        class << self
            include Puppet::Util
        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)
                end
            else
                @destinations.each { |name, dest|
                    if dest.respond_to?(:flush)
                        dest.flush
                    end
                    if dest.respond_to?(:close)
                        dest.close
                    end
                }
                @destinations = {}
            end
        end

        # Flush any log destinations that support such operations.
        def Log.flush
            @destinations.each { |type, dest|
                if dest.respond_to?(:flush)
                    dest.flush
                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)
            unless hash.include?(:level)
                raise Puppet::DevError, "Logs require a level"
            end
            unless @levels.index(hash[:level])
                raise Puppet::DevError, "Invalid log level %s" % hash[:level]
            end
            if @levels.index(hash[:level]) >= @loglevel 
                return Puppet::Log.new(hash)
            else
                return nil
            end
        end

        def Log.destinations
            return @destinations.keys
        end

        # Yield each valid level in turn
        def Log.eachlevel
            @levels.each { |level| yield level }
        end

        # Return the current log level.
        def Log.level
            return @levels[@loglevel]
        end

        # Set the current log level.
        def Log.level=(level)
            unless level.is_a?(Symbol)
                level = level.intern
            end

            unless @levels.include?(level)
                raise Puppet::DevError, "Invalid loglevel %s" % level
            end

            @loglevel = @levels.index(level)
        end

        def Log.levels
            @levels.dup
        end

        newdesttype :syslog do
            def close
                Syslog.close
            end

            def initialize
                if Syslog.opened?
                    Syslog.close
                end
                name = Puppet.name
                name = "puppet-#{name}" unless name =~ /puppet/

                options = Syslog::LOG_PID | Syslog::LOG_NDELAY

                # XXX This should really be configurable.
                facility = Syslog::LOG_DAEMON

                @syslog = Syslog.open(name, options, facility)
            end

            def handle(msg)
                # XXX Syslog currently has a bug that makes it so you
                # cannot log a message with a '%' in it.  So, we get rid
                # of them.
                if msg.source == "Puppet"
                    @syslog.send(msg.level, msg.to_s.gsub("%", '%%'))
                else
                    @syslog.send(msg.level, "(%s) %s" %
                        [msg.source.to_s.gsub("%", ""),
                            msg.to_s.gsub("%", '%%')
                        ]
                    )
                end
            end
        end

        newdesttype :file do
            match /^\//

            def close
                if defined? @file
                    @file.close
                    @file = nil
                end
            end

            def flush
                if defined? @file
                    @file.flush
                end
            end

            def initialize(path)
                @name = path
                # first make sure the directory exists
                # We can't just use 'Config.use' here, because they've
                # specified a "special" destination.
                unless FileTest.exist?(File.dirname(path))
                    Puppet.recmkdir(File.dirname(path))
                    Puppet.info "Creating log directory %s" % File.dirname(path)
                end

                # create the log file, if it doesn't already exist
                file = File.open(path, File::WRONLY|File::CREAT|File::APPEND)

                @file = file
            end

            def handle(msg)
                @file.puts("%s %s (%s): %s" %
                    [msg.time, msg.source, msg.level, msg.to_s])
            end
        end

        newdesttype :console do
            @@colors = {
                :debug => SLATE,
                :info => GREEN,
                :notice => PINK,
                :warning => ORANGE,
                :err => YELLOW,
                :alert => BLUE,
                :emerg => RESET,
                :crit => RESET
            }

            def initialize
                # Flush output immediately.
                $stdout.sync = true
            end

            def handle(msg)
                color = ""
                reset = ""
                if Puppet[:color]
                    color = @@colors[msg.level]
                    reset = RESET
                end
                if msg.source == "Puppet"
                    puts color + "%s: %s" % [
                        msg.level, msg.to_s
                    ] + reset
                else
                    puts color + "%s: %s: %s" % [
                        msg.level, msg.source, msg.to_s
                    ] + reset
                end
            end
        end

        newdesttype :host do
            def initialize(host)
                Puppet.info "Treating %s as a hostname" % host
                args = {}
                if host =~ /:(\d+)/
                    args[:Port] = $1
                    args[:Server] = host.sub(/:\d+/, '')
                else
                    args[:Server] = host
                end

                @name = host

                @driver = Puppet::Client::LogClient.new(args)
            end

            def handle(msg)
                unless msg.is_a?(String) or msg.remote
                    unless defined? @hostname
                        @hostname = Facter["hostname"].value
                    end
                    unless defined? @domain
                        @domain = Facter["domain"].value
                        if @domain
                            @hostname += "." + @domain
                        end
                    end
                    if msg.source =~ /^\//
                        msg.source = @hostname + ":" + msg.source
                    elsif msg.source == "Puppet"
                        msg.source = @hostname + " " + msg.source
                    else
                        msg.source = @hostname + " " + msg.source
                    end
                    begin
                        #puts "would have sent %s" % msg
                        #puts "would have sent %s" %
                        #    CGI.escape(YAML.dump(msg))
                        begin
                            tmp = CGI.escape(YAML.dump(msg))
                        rescue => detail
                            puts "Could not dump: %s" % detail.to_s
                            return
                        end
                        # Add the hostname to the source
                        @driver.addlog(tmp)
                        sleep(0.5)
                    rescue => detail
                        if Puppet[:debug]
                            puts detail.backtrace
                        end
                        Puppet.err detail
                        Puppet::Log.close(self)
                    end
                end
            end
        end

        # Create a new log destination.
        def Log.newdestination(dest)
            # Each destination can only occur once.
            if @destinations.find { |name, obj| obj.name == dest }
                return
            end


            name, type = @desttypes.find do |name, klass|
                klass.match?(dest)
            end

            unless type
                raise Puppet::DevError, "Unknown destination type %s" % dest
            end

            begin
                if type.instance_method(:initialize).arity == 1
                    @destinations[dest] = type.new(dest)
                else
                    @destinations[dest] = type.new()
                end
            rescue => detail
                if Puppet[:debug]
                    puts detail.backtrace
                end

                # If this was our only destination, then add the console back in.
                if @destinations.empty? and (dest != :console and dest != "console")
                    newdestination(:console)
                end
            end
        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
        # itself.
        def Log.newmessage(msg)
            if @levels.index(msg.level) < @loglevel 
                return
            end

            @destinations.each do |name, dest|
                threadlock(dest) do
                    dest.handle(msg)
                end
            end
        end

        def Log.sendlevel?(level)
            @levels.index(level) >= @loglevel 
        end

        # Reopen all of our logs.
        def Log.reopen
            types = @destinations.keys
            @destinations.each { |type, dest|
                if dest.respond_to?(:close)
                    dest.close
                end
            }
            @destinations.clear
            # We need to make sure we always end up with some kind of destination
            begin
                types.each { |type|
                    Log.newdestination(type)
                }
            rescue => detail
                if @destinations.empty?
                    Log.newdestination(:syslog)
                    Puppet.err detail.to_s
                end
            end
        end

        # Is the passed level a valid log level?
        def self.validlevel?(level)
            @levels.include?(level)
        end

		attr_accessor :level, :message, :time, :tags, :remote
        attr_reader :source

		def initialize(args)
			unless args.include?(:level) && args.include?(:message)
				raise Puppet::DevError, "Puppet::Log called incorrectly"
			end

			if args[:level].class == String
				@level = args[:level].intern
			elsif args[:level].class == Symbol
				@level = args[:level]
			else
				raise Puppet::DevError,
                    "Level is not a string or symbol: #{args[:level].class}"
			end

            # Just return unless we're actually at a level we should send
            #return unless self.class.sendlevel?(@level)

			@message = args[:message].to_s
			@time = Time.now
			# this should include the host name, and probly lots of other
			# stuff, at some point
			unless self.class.validlevel?(level)
				raise Puppet::DevError, "Invalid message level #{level}"
			end

            if args.include?(:tags)
                @tags = args[:tags]
            end

            if args.include?(:source)
                self.source = args[:source]
            else
                @source = "Puppet"
            end

            Log.newmessage(self)
		end

        # If they pass a source in to us, we make sure it is a string, and
        # we retrieve any tags we can.
        def source=(source)
            # We can't store the actual source, we just store the path.  This
            # is a bit of a stupid hack, specifically testing for elements, but
            # eh.
            if source.is_a?(Puppet::Element) and source.respond_to?(:path)
                @source = source.path
            else
                @source = source.to_s
            end
            unless defined? @tags and @tags
                if source.respond_to?(:tags)
                    @tags = source.tags
                end
            end
        end

        def tagged?(tag)
            @tags.include?(tag)
        end

		def to_report
            "%s %s (%s): %s" % [self.time, self.source, self.level, self.to_s]
		end

		def to_s
            return @message
		end
	end
end

# $Id$