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  import de.richinternet.utils.Dumper;
    24  
    25  /**
    26   * {@code RichInternetHandler} logs messages to Dirk Eisman's Flex Trace Panel.
    27   * 
    28   * <p>The {@code de.richinternet.utils.Dumper} class is needed.
    29   * 
    30   * @author Simon Wacker
    31   * @see org.as2lib.env.log.logger.RichInternetLogger
    32   * @see <a href="http://www.richinternet.de/blog/index.cfm?entry=EB3BA9D6-A212-C5FA-A9B1B5DB4BB7F555">Flex Trace Panel</a>
    33   */
    34  class org.as2lib.env.log.handler.RichInternetHandler extends AbstractLogHandler implements LogHandler {
    35  	
    36  	/** Holds a rich internet handler. */
    37  	private static var richInternetHandler:RichInternetHandler;
    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 rich internet handler
    50  	 */
    51  	public static function getInstance(messageStringifier:Stringifier):RichInternetHandler {
    52  		if (!richInternetHandler) richInternetHandler = new RichInternetHandler(messageStringifier);
    53  		return richInternetHandler;
    54  	}
    55  	
    56  	/**	
    57  	 * Constructs a new {@code RichInternetHandler} 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 rich internet handlers and
    62  	 * saves storage.
    63  	 * 
    64  	 * @param messageStringifier (optional) the log message stringifier to use
    65  	 */
    66  	public function RichInternetHandler(messageStringifier:Stringifier) {
    67  		super (messageStringifier);
    68  	}
    69  	
    70  	/**
    71  	 * Writes log messages to Dirk Eisman's Flex Trace Panel.
    72  	 *
    73  	 * <p>The string representation of the {@code message} to log is obtained via
    74  	 * the {@code convertMessage} method.
    75  	 * 
    76  	 * <p>The default level is {@code INFO}. If the {@code message}'s level is {@code FATAL}
    77  	 * it will be logged at {@code ERROR} level because Dirk Eisman's Flex Trace Panel
    78  	 * does not support the {@code FATAL} level.
    79  	 * 
    80  	 * @param message the message to log
    81  	 */
    82  	public function write(message:LogMessage):Void {
    83  		var m:String = convertMessage(message);
    84  		switch (message.getLevel()) {
    85  			case AbstractLogLevel.DEBUG:
    86  				Dumper.trace(m);
    87  				break;
    88  			case AbstractLogLevel.INFO:
    89  				Dumper.info(m);
    90  				break;
    91  			case AbstractLogLevel.WARNING:
    92  				Dumper.warn(m);
    93  				break;
    94  			case AbstractLogLevel.ERROR:
    95  				Dumper.error(m);
    96  				break;
    97  			case AbstractLogLevel.FATAL:
    98  				Dumper.error(m);
    99  				break;
   100  			default:
   101  				Dumper.dump(m);
   102  				break;
   103  		}
   104  	}
   105  	
   106  }