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.LogLevel;
    21  import org.as2lib.env.log.level.AbstractLogLevel;
    22  import org.as2lib.env.log.handler.AbstractLogHandler;
    23  
    24  import org.actionstep.FDTDebugger;
    25  
    26  /**
    27   * {@code FlashDebugToolHandler} the {@code org.actionstep.FDTDebugger.trace} method to log
    28   * messages.
    29   * 
    30   * @author Simon Wacker
    31   * @see org.as2lib.env.log.logger.FlashDebugToolLogger
    32   * @see <a href="http://sourceforge.net/projects/flashdebugtool">Flash Debug Tool</a>
    33   */
    34  class org.as2lib.env.log.handler.FlashDebugToolHandler extends AbstractLogHandler implements LogHandler {
    35  	
    36  	/** Holds a flash debug tool handler instance. */
    37  	private static var flashDebugToolHandler:FlashDebugToolHandler;
    38  	
    39  	/**
    40  	 * Returns an instance of this class.
    41  	 *
    42  	 * <p>This method always returns the same instance.
    43  	 *
    44  	 * <p>The {@code messageStringifier} argument is only recognized on first
    45  	 * invocation of this method.
    46  	 * 
    47  	 * @param messageStringifier (optional) the log message stringifier to be used by
    48  	 * the returned handler
    49  	 * @return a flash debug tool handler
    50  	 */
    51  	public static function getInstance(messageStringifier:Stringifier):FlashDebugToolHandler {
    52  		if (!flashDebugToolHandler) flashDebugToolHandler = new FlashDebugToolHandler(messageStringifier);
    53  		return flashDebugToolHandler;
    54  	}
    55  	
    56  	/**	
    57  	 * Constructs a new {@code FlashDebugToolHandler} instance.
    58  	 *
    59  	 * <p>You can use one and the same instance for multiple loggers. So think about
    60  	 * using the handler returned by the static {@link #getInstance} method. Using this
    61  	 * instance prevents the instantiation of unnecessary trace handlers and saves
    62  	 * storage.
    63  	 * 
    64  	 * @param messageStringifier (optional) the log message stringifier to use
    65  	 */
    66  	public function FlashDebugToolHandler(messageStringifier:Stringifier) {
    67  		super (messageStringifier);
    68  	}
    69  	
    70  	/**
    71  	 * Writes the passed-in {@code message} using the {@code org.actionstep.FDTDebugger.trace}
    72  	 * method.
    73  	 *
    74  	 * <p>The string representation of the {@code message} to log is obtained via
    75  	 * the {@code convertMessage} method.
    76  	 * 
    77  	 * @param message the message to log
    78  	 */
    79  	public function write(message:LogMessage):Void {
    80  		FDTDebugger.trace(convertMessage(message), convertLevel(message.getLevel()));
    81  	}
    82  	
    83  	/**
    84  	 * Converts the As2lib {@code LogLevel} into a Flash Debug Tool level number.
    85  	 * 
    86  	 * @param level the As2lib log level to convert
    87  	 * @return the equivalent Flash Debug Tool level
    88  	 */
    89  	private function convertLevel(level:LogLevel):Number {
    90  		switch (level) {
    91  			case AbstractLogLevel.DEBUG:
    92  				return FDTDebugger.DEBUG;
    93  			case AbstractLogLevel.INFO:
    94  				return FDTDebugger.INFO;
    95  			case AbstractLogLevel.WARNING:
    96  				return FDTDebugger.WARNING;
    97  			case AbstractLogLevel.ERROR:
    98  				return FDTDebugger.ERROR;
    99  			case AbstractLogLevel.FATAL:
   100  				return FDTDebugger.FATAL;
   101  			default:
   102  				return null;
   103  		}
   104  	}
   105  	
   106  }