bb.util.logging
Class HandlerAudio

java.lang.Object
  extended by java.util.logging.Handler
      extended by bb.util.logging.HandlerAbstract
          extended by bb.util.logging.HandlerAudio

public class HandlerAudio
extends HandlerAbstract

Plays a major error sound if a Level.SEVERE LogRecord is encountered, plays a minor error sound if a Level.WARNING LogRecord is encountered, or plays a notify sound if any other LogRecord is encountered. (A LogRecord is "encountered" if it is passed to publish and publish accepts it for publication.) If, while the sound is playing, more LogRecords arrive, then the worst Level seen during this time determines what the next sound will be.

It can be useful to attach an instance to the root Logger, and have it publish serious logs.

It is unclear whether or not this class is multithread safe. There may be issues with its superclass; see HandlerAbstract for more discussion. Concerning the state added by this class, every method is synchronized.

Author:
Brent Boyer

Nested Class Summary
private static class HandlerAudio.SoundPlayer
           
static class HandlerAudio.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private  Set<Level> levels
          Stores the set of Levels encountered by publish since the last call to getLevelWorst.
private  Thread threadPlayer
          Executes a HandlerAudio.SoundPlayer instance.
 
Constructor Summary
HandlerAudio()
          Constructor.
 
Method Summary
 void close()
          First call releases all resources used by this instance.
 void flush()
          Implementation here does nothing, since the notify done by publish effectively acts as an auto-flush if threadPlayer is waiting inside getLevelWorst.
protected  Level getLevelDefault()
          Implementation here returns Level.WARNING.
private  Level getLevelWorst()
           
 void publish(LogRecord record)
          If isAlive or isLoggable(record) returns false, then immediately returns.
 
Methods inherited from class bb.util.logging.HandlerAbstract
configure, getEncodingDefault, getFilterDefault, getFilterProperty, getFormatterDefault, getFormatterProperty, getIntProperty, getLevelProperty, getStringProperty, isAlive, isLoggable, setAlive
 
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

levels

private Set<Level> levels
Stores the set of Levels encountered by publish since the last call to getLevelWorst.

Contract: is never null after construction, but is nulled out once and for all when close is first called.


threadPlayer

private Thread threadPlayer
Executes a HandlerAudio.SoundPlayer instance.

Contract: is null until lazy initialized by publish, then is nulled out once and for all when close is first called.

Constructor Detail

HandlerAudio

public HandlerAudio()
Constructor.

All this instance's initial configuration comes from the LogManager properties described in HandlerAbstract.configure.

Method Detail

close

public void close()
First call releases all resources used by this instance. All future calls to publish will be silently ignored.

Overrides:
close in class HandlerAbstract

flush

public void flush()
Implementation here does nothing, since the notify done by publish effectively acts as an auto-flush if threadPlayer is waiting inside getLevelWorst.

Specified by:
flush in class Handler

publish

public void publish(LogRecord record)
If isAlive or isLoggable(record) returns false, then immediately returns. Else, adds record's Level to levels and notifies threadPlayer if is waiting inside getLevelWorst.

Specified by:
publish in class Handler

getLevelDefault

protected Level getLevelDefault()
Implementation here returns Level.WARNING.

Overrides:
getLevelDefault in class HandlerAbstract

getLevelWorst

private Level getLevelWorst()
                     throws InterruptedException
Throws:
InterruptedException