1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.util.Stringifier;
    18  import org.as2lib.env.log.LogHandler;
    19  import org.as2lib.env.log.LogMessage;
    20  import org.as2lib.env.log.handler.AbstractLogHandler;
    21  
    22  /**
    23   * {@code TraceHandler} uses {@code trace} to log messages.
    24   * 
    25   * @author Simon Wacker
    26   */
    27  class org.as2lib.env.log.handler.TraceHandler extends AbstractLogHandler implements LogHandler {
    28  	
    29  	/** Holds a trace handler instance. */
    30  	private static var traceHandler:TraceHandler;
    31  	
    32  	/**
    33  	 * Returns an instance of this class.
    34  	 *
    35  	 * <p>This method always returns the same instance.
    36  	 *
    37  	 * <p>The {@code messageStringifier} argument is only recognized on first
    38  	 * invocation of this method.
    39  	 * 
    40  	 * @param messageStringifier (optional) the log message stringifier to be used by
    41  	 * the returned handler
    42  	 * @return a trace handler
    43  	 */
    44  	public static function getInstance(messageStringifier:Stringifier):TraceHandler {
    45  		if (!traceHandler) traceHandler = new TraceHandler(messageStringifier);
    46  		return traceHandler;
    47  	}
    48  	
    49  	/**	
    50  	 * Constructs a new {@code TraceHandler} instance.
    51  	 *
    52  	 * <p>You can use one and the same instance for multiple loggers. So think about
    53  	 * using the handler returned by the static {@link #getInstance} method. Using this
    54  	 * instance prevents the instantiation of unnecessary trace handlers and saves
    55  	 * storage.
    56  	 * 
    57  	 * @param messageStringifier (optional) the log message stringifier to use
    58  	 */
    59  	public function TraceHandler(messageStringifier:Stringifier) {
    60  		super (messageStringifier);
    61  	}
    62  	
    63  	/**
    64  	 * Writes the passed-in {@code message} using {@code trace}.
    65  	 *
    66  	 * <p>The string representation of the {@code message} to log is obtained via
    67  	 * the {@code convertMessage} method.
    68  	 * 
    69  	 * @param message the message to log
    70  	 */
    71  	public function write(message:LogMessage):Void {
    72  		trace(convertMessage(message));
    73  	}
    74  	
    75  }