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 FlashoutHandler} logs messages to the Flashout console.
    25   *
    26   * <p>The {@code Flashout} class is needed.
    27   * 
    28   * <p>Note that if you are working with the Flashout 0.2.* you have to specify
    29   * Flashout's custom trace method as trace replacement when compiling your classes
    30   * with MTASC.
    31   * 
    32   * @author Simon Wacker
    33   * @see org.as2lib.env.log.logger.FlashoutLogger
    34   * @see <a href="http://www.potapenko.com/flashout">Flashout</a>
    35   */
    36  class org.as2lib.env.log.handler.FlashoutHandler extends AbstractLogHandler implements LogHandler {
    37  	
    38  	/** Holds a flashout handler. */
    39  	private static var flashoutHandler:FlashoutHandler;
    40  	
    41  	/**
    42  	 * Returns an instance of this class.
    43  	 *
    44  	 * <p>This method always returns the same instance.
    45  	 *
    46  	 * <p>The {@code messageStringifier} argument is only recognized on first
    47  	 * invocation of this method.
    48  	 *
    49  	 * @param messageStringifier (optional) the log message stringifier to be used by
    50  	 * the returned handler
    51  	 * @return a flashout handler
    52  	 */
    53  	public static function getInstance(messageStringifier:Stringifier):FlashoutHandler {
    54  		if (!flashoutHandler) flashoutHandler = new FlashoutHandler(messageStringifier);
    55  		return flashoutHandler;
    56  	}
    57  	
    58  	/**	
    59  	 * Constructs a new {@code FlashoutHandler} instance.
    60  	 *
    61  	 * <p>You can use one and the same instance for multiple loggers. So think about
    62  	 * using the handler returned by the static {@link #getInstance} method. Using this
    63  	 * instance prevents the instantiation of unnecessary flashout handlers and
    64  	 * saves storage.
    65  	 * 
    66  	 * @param messageStringifier (optional) the log message stringifier to use
    67  	 */
    68  	public function FlashoutHandler(messageStringifier:Stringifier) {
    69  		super (messageStringifier);
    70  	}
    71  	
    72  	/**
    73  	 * Writes log messages to the Flashout console.
    74  	 *
    75  	 * <p>The string representation of the {@code message} to log is obtained via
    76  	 * the {@code convertMessage} method.
    77  	 * 
    78  	 * <p>Every As2lib Logging API log level that has a mapping to the log levels of
    79  	 * Flashout is mapped to the correct one. If no mapping is found, {@code Flashout.log}
    80  	 * is used.
    81  	 *
    82  	 * @param message the message to log
    83  	 */
    84  	public function write(message:LogMessage):Void {
    85  		var m:String = convertMessage(message);
    86  		switch (message.getLevel()) {
    87  			case AbstractLogLevel.DEBUG:
    88  				Flashout.debug(m);
    89  				break;
    90  			case AbstractLogLevel.INFO:
    91  				Flashout.info(m);
    92  				break;
    93  			case AbstractLogLevel.WARNING:
    94  				Flashout.warning(m);
    95  				break;
    96  			case AbstractLogLevel.ERROR:
    97  				Flashout.error(m);
    98  				break;
    99  			case AbstractLogLevel.FATAL:
   100  				// old Flashout did not provide fatal level
   101  				// wanted support to be compatible with both versions
   102  				if (Flashout["fatal"]) {
   103  					Flashout["fatal"](m);
   104  				} else {
   105  					Flashout.error(m);
   106  				}
   107  				break;
   108  			default:
   109  				Flashout.log(m);
   110  				break;
   111  		}
   112  	}
   113  	
   114  }