Parent

Logging::Appenders::Growl

This class provides an Appender that can send notifications to the Growl notification system on Mac OS X.

growlnotify must be installed somewhere in the path in order for the appender to function properly.

Public Class Methods

new( name, opts = {} ) click to toggle source

Create an appender that will log messages to the Growl framework on a Mac OS X machine.

    # File lib/logging/appenders/growl.rb, line 29
29:     def initialize( name, opts = {} )
30:       super
31: 
32:       @growl = "growlnotify -w -n \"#{@name}\" -t \"%s\" -m \"%s\" -p %d &"
33: 
34:       @coalesce = opts.getopt(:coalesce, false)
35:       @title_sep = opts.getopt(:separator)
36: 
37:       # provides a mapping from the default Logging levels
38:       # to the Growl notification levels
39:       @map = [2, 1, 0, 1, 2]
40: 
41:       map = opts.getopt(:map)
42:       self.map = map unless map.nil?
43:       setup_coalescing if @coalesce
44: 
45:       # make sure the growlnotify command can be called
46:       unless system('growlnotify -v >> /dev/null 2>&1')
47:         self.level = :off
48:         ::Logging.log_internal {'growl notifications have been disabled'}
49:       end
50:     end

Public Instance Methods

map = { logging_levels => growl_levels } click to toggle source

Configure the mapping from the Logging levels to the Growl notification levels. This is needed in order to log events at the proper Growl level.

Without any configuration, the following mapping will be used:

   :debug  =>  -2
   :info   =>  -1
   :warn   =>  0
   :error  =>  1
   :fatal  =>  2
    # File lib/logging/appenders/growl.rb, line 67
67:     def map=( levels )
68:       map = []
69:       levels.keys.each do |lvl|
70:         num = ::Logging.level_num(lvl)
71:         map[num] = growl_level_num(levels[lvl])
72:       end
73:       @map = map
74:     end

Private Instance Methods

call_growl( title, message, priority ) click to toggle source

Call the growlnotify application with the given parameters. If the system call fails, the growl appender will be disabled.

     # File lib/logging/appenders/growl.rb, line 191
191:     def call_growl( *args )
192:       unless system(@growl % args)
193:         self.level = :off
194:         ::Logging.log_internal {'growl notifications have been disabled'}
195:       end
196:     end
coalesce( title, message, priority ) click to toggle source

Attempt to coalesce the given message with any that might be pending in the queue to send to the growl notifier. Messages are coalesced with any in the queue that have the same title and priority.

There can be only one message in the queue, so if the title and/or priority don’t match, the message in the queue is sent immediately to the growl notifier, and the current message is queued.

     # File lib/logging/appenders/growl.rb, line 145
145:     def coalesce( *msg )
146:       @c_mutex.synchronize do
147:         if @c_queue.empty?
148:           @c_queue << msg
149:           @c_thread.run
150: 
151:         else
152:           qmsg = @c_queue.last
153:           if qmsg.first != msg.first or qmsg.last != msg.last
154:             @c_queue << msg
155:           else
156:             qmsg[1] << "\n" << msg[1]
157:           end
158:         end
159:       end
160:     end
growl( title, message, priority ) click to toggle source

Send the message to the growl notifier using the given title and priority.

     # File lib/logging/appenders/growl.rb, line 128
128:     def growl( title, message, priority )
129:       message.tr!("`", "'")
130:       if @coalesce then coalesce(title, message, priority)
131:       else call_growl(title, message, priority) end
132:     end
growl_level_num( level ) => integer click to toggle source

Takes the given level as a string or integer and returns the corresponding Growl notification level number.

     # File lib/logging/appenders/growl.rb, line 114
114:     def growl_level_num( level )
115:       level = Integer(level)
116:       if level < 2 or level > 2
117:         raise ArgumentError, "level '#{level}' is not in range -2..2"
118:       end
119:       level
120:     end
setup_coalescing click to toggle source

Setup the appender to handle coalescing of messages before sending them to the growl notifier. This requires the creation of a thread and mutex for passing messages from the appender thread to the growl notifier thread.

     # File lib/logging/appenders/growl.rb, line 170
170:     def setup_coalescing
171:       @c_mutex = Mutex.new
172:       @c_queue = []
173: 
174:       @c_thread = Thread.new do
175:         loop do
176:           Thread.stop if @c_queue.empty?
177:           sleep 1
178:           @c_mutex.synchronize {
179:             call_growl(*@c_queue.shift) until @c_queue.empty?
180:           }
181:         end  # loop
182:       end  # Thread.new
183:     end
write( event ) click to toggle source

Write the given event to the growl notification facility. The log event will be processed through the Layout associated with this appender. The message will be logged at the level specified by the event.

     # File lib/logging/appenders/growl.rb, line 87
 87:     def write( event )
 88:       title = ''
 89:       priority = 0
 90:       message = if event.instance_of?(::Logging::LogEvent)
 91:           priority = @map[event.level]
 92:           @layout.format(event)
 93:         else
 94:           event.to_s
 95:         end
 96:       return if message.empty?
 97: 
 98:       message = message.gsub(ColoredRegexp, '')
 99:       if @title_sep
100:         title, message = message.split(@title_sep)
101:         title, message = '', title if message.nil?
102:       end
103: 
104:       growl(title.strip, message.strip, priority)
105:       self
106:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.