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.core.BasicClass;
    18  import org.as2lib.env.log.LogHandler;
    19  import org.as2lib.env.log.LogMessage;
    20  
    21  /**
    22   * {@code Bit101Handler} logs messages to the Bit-101 Debug Panel.
    23   *
    24   * <p>The {@code Debug} class is needed.
    25   *
    26   * @author Simon Wacker
    27   * @see org.as2lib.env.log.logger.Bit101Logger
    28   * @see <a href="www.bit-101.com/DebugPanel">Flash Debug Panel Source</a>
    29   * @see <a href="http://www.bit-101.com/blog/archives/000119.html">Flash Debug Panel Article</a>
    30   */
    31  class org.as2lib.env.log.handler.Bit101Handler extends BasicClass implements LogHandler {
    32  	
    33  	/** Holds a bit-101 handler. */
    34  	private static var bit101Handler:Bit101Handler;
    35  	
    36  	/**
    37  	 * Returns an instance of this class.
    38  	 *
    39  	 * <p>This method always returns the same instance.
    40  	 *
    41  	 * <p>Note that the arguments are only recognized on first call of this method and
    42  	 * are in this case used for construction of the {@code Bit101Handler} instance.
    43  	 *
    44  	 * @param decorateMessage (optional) determines whether to log the string returned
    45  	 * by the {@code LogMessage.toString} method or just the message returned by
    46  	 * {@code LogMessage.getMessage}
    47  	 * @param recursionDepth (optional) determines the count of recursions for
    48  	 * recursively traced objects
    49  	 * @param indentation (optional) determines the indentation number for recursively
    50  	 * traced objects
    51  	 * @return a bit-101 handler
    52  	 */
    53  	public static function getInstance(decorateMessage:Boolean, recursionDepth:Number, indentation:Number):Bit101Handler {
    54  		if (!bit101Handler) bit101Handler = new Bit101Handler(decorateMessage, recursionDepth, indentation);
    55  		return bit101Handler;
    56  	}
    57  	
    58  	/** Determines whether to decorate messages. */
    59  	private var decorateMessage:Boolean;
    60  	
    61  	/** The number of recursions when tracing an object recursively. */
    62  	private var recursionDepth:Number;
    63  	
    64  	/** The indentation number for recursively traced objects. */
    65  	private var indentation:Number;
    66  	
    67  	/**	
    68  	 * Constructs a new {@code Bit101Handler} instance.
    69  	 *
    70  	 * <p>You can use one and the same instance for multiple loggers. So think about
    71  	 * using the handler returned by the static {@link #getInstance} method. Using
    72  	 * this instance prevents the instantiation of unnecessary bit-101 handlers and
    73  	 * saves storage.
    74  	 *
    75  	 * <p>{@code decorateMessage} is by default {@code true}. Refer to the
    76  	 * {@code Debug} class for information on the default {@code recursionDepth} and
    77  	 * {@code indentation}.
    78  	 *
    79  	 * <p>Note that messages are only logged recursively if {@code decorateMessage} is
    80  	 * set to {@code false}.
    81  	 *
    82  	 * @param decorateMessage (optional) determines whether to log the string returned
    83  	 * by the {@code LogMessage.toString} method or just the message returned by
    84  	 * {@code LogMessage.getMessage}
    85  	 * @param recursionDepth (optional) determines the count of recursions for
    86  	 * recursively traced objects
    87  	 * @param indentation (optional) determines the indentation number for recursively
    88  	 * traced objects
    89  	 */
    90  	public function Bit101Handler(decorateMessage:Boolean, recursionDepth:Number, indentation:Number) {
    91  		this.decorateMessage = decorateMessage == null ? true : decorateMessage;
    92  		this.recursionDepth = recursionDepth;
    93  		this.indentation = indentation;
    94  	}
    95  	
    96  	/**
    97  	 * Writes log messages to the Bit-101 Debug Panel.
    98  	 *
    99  	 * <p>Uses the {@link LogMessage#toString} method to obtain the string that is
   100  	 * logged if {@code decorateMessage} is turned on. Otherwise the string returned
   101  	 * by the original message's {@code toString} method is logged if it is of type
   102  	 * {@code 'string'}, {@code 'number'}, {@code 'boolean'}, {@code 'undefined'} or
   103  	 * {@code 'null'} or the original method is logged recursively if it is not of one
   104  	 * of the above types.
   105  	 *
   106  	 * @param message the message to log
   107  	 */
   108  	public function write(message:LogMessage):Void {
   109  		if (this.decorateMessage) {
   110  			Debug.trace(message.toString());
   111  		} else {
   112  			var type:String = typeof(message.getMessage());
   113  			if (type == "string" || type == "number" || type == "boolean" || type == "undefined" || type == "null") {
   114  				Debug.trace(message.getMessage().toString());
   115  			} else {
   116  				Debug.traceObject(message.getMessage(), this.recursionDepth, this.indentation);
   117  			}
   118  		}
   119  	}
   120  	
   121  }