When a component sends a log message, it must create a LogEvent
object, then broadcast that object to all LogListener
objects attached to the component.
This operation is best placed in a method that can be called quickly. For example, the following implementation includes such convenience methods:
Vector mLogListeners; public synchronized void addLogListener (LogListener pListener) { if (mLogListeners == null) mLogListeners = new Vector (); mLogListeners.addElement (pListener); } public synchronized void removeLogListener (LogListener pListener) { if (mLogListeners != null) mLogListeners.removeElement (pListener); } public int getLogListenerCount () { return (mLogListeners == null) ? 0 : mLogListeners.size (); } public synchronized void sendLogEvent (LogEvent pLogEvent) { if (mLogListeners != null) { int len = mLogListeners.size (); for (int i = 0; i < len; i++) { ((LogListener) mLogListeners.elementAt (i)).logEvent (pLogEvent); } } } public void logError (String pMessage) { logError (pMessage, null); } public void logError (Throwable pThrowable) { logError (null, pThrowable); } public void logError (String pMessage, Throwable pThrowable) { sendLogEvent (new ErrorLogEvent (pMessage, pThrowable)); }
With these methods available, the component can now send error events like this:
// Log an error
if (isLoggingError ()) {
logError ("Look out, it's gonna blow!");
}