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.level.AbstractLogLevel;
    21  import org.as2lib.env.log.handler.AbstractLogHandler;
    22  
    23  /**
    24   * {@code FludgeHandler} logs messages with {@code Fludge.trace} method.
    25   * 
    26   * @author Simon Wacker
    27   * @see org.as2lib.env.log.logger.FludgeLogger
    28   * @see <a href="http://www.osflash.org/doku.php?id=fludge">Fludge</a>
    29   */
    30  class org.as2lib.env.log.handler.FludgeHandler extends AbstractLogHandler implements LogHandler {
    31  	
    32  	/** Holds a fludge handler. */
    33  	private static var fludgeHandler:FludgeHandler;
    34  	
    35  	/**
    36  	 * Returns an instance of this class.
    37  	 *
    38  	 * <p>This method always returns the same instance.
    39  	 *
    40  	 * <p>The {@code messageStringifier} argument is only recognized on first
    41  	 * invocation of this method.
    42  	 *
    43  	 * @param messageStringifier (optional) the log message stringifier to be used by
    44  	 * the returned handler
    45  	 * @return a fludge handler
    46  	 */
    47  	public static function getInstance(messageStringifier:Stringifier):FludgeHandler {
    48  		if (!fludgeHandler) fludgeHandler = new FludgeHandler(messageStringifier);
    49  		return fludgeHandler;
    50  	}
    51  	
    52  	/**	
    53  	 * Constructs a new {@code FludgeHandler} instance.
    54  	 *
    55  	 * <p>You can use one and the same instance for multiple loggers. So think about
    56  	 * using the handler returned by the static {@link #getInstance} method. Using this
    57  	 * instance prevents the instantiation of unnecessary fludge handlers and
    58  	 * saves storage.
    59  	 * 
    60  	 * @param messageStringifier (optional) the log message stringifier to use
    61  	 */
    62  	public function FludgeHandler(messageStringifier:Stringifier) {
    63  		super (messageStringifier);
    64  	}
    65  	
    66  	/**
    67  	 * Writes log messages using {@code Fludge.trace}.
    68  	 *
    69  	 * <p>The string representation of the {@code message} to log is obtained via
    70  	 * the {@code convertMessage} method.
    71  	 *
    72  	 * @param message the message to log
    73  	 */
    74  	public function write(message:LogMessage):Void {
    75  		var m:String = convertMessage(message);
    76  		switch (message.getLevel()) {
    77  			case AbstractLogLevel.DEBUG:
    78  				Fludge.trace(m, "debug");
    79  				break;
    80  			case AbstractLogLevel.INFO:
    81  				Fludge.trace(m, "info");
    82  				break;
    83  			case AbstractLogLevel.WARNING:
    84  				Fludge.trace(m, "warn");
    85  				break;
    86  			case AbstractLogLevel.ERROR:
    87  				Fludge.trace(m, "error");
    88  				break;
    89  			case AbstractLogLevel.FATAL:
    90  				Fludge.trace(m, "exception");
    91  				break;
    92  			default:
    93  				Fludge.trace(m, "info");
    94  				break;
    95  		}
    96  	}
    97  	
    98  }