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.env.log.Logger;
    18  import org.as2lib.env.log.LogLevel;
    19  import org.as2lib.env.log.LogMessage;
    20  import org.as2lib.env.log.logger.AbstractLogger;
    21  
    22  /**
    23   * {@code Bit101Logger} delegates all log messages to the {@code Debug.trace}
    24   * method from Keith Peter's Debug Panel.
    25   * 
    26   * <p>Using this class instead of the {@code Debug} class in your application
    27   * directly enables you to switch between almost every available Logging API
    28   * without having to change the logging calls but just the underlying configuration
    29   * on startup.
    30   *
    31   * @author Simon Wacker
    32   * @see org.as2lib.env.log.handler.Bit101Handler
    33   * @see <a href="http://www.bit-101.com/DebugPanel">Flash Debug Panel Source</a>
    34   * @see <a href="http://www.bit-101.com/blog/archives/000119.html">Flash Debug Panel Article</a>
    35   */
    36  class org.as2lib.env.log.logger.Bit101Logger extends AbstractLogger implements Logger {
    37  	
    38  	/** Makes the static variables of the super-class accessible through this class. */
    39  	private static var __proto__:Function = AbstractLogger;
    40  	
    41  	/** The set level. */
    42  	private var level:LogLevel;
    43  	
    44  	/** The set level as number. */
    45  	private var levelAsNumber:Number;
    46  	
    47  	/** Determines whether to trace objects recursively. */
    48  	private var traceObject:Boolean;
    49  	
    50  	/** The number of recursions when tracing an object recursively. */
    51  	private var recursionDepth:Number;
    52  	
    53  	/** The indentation number for recursively traced objects. */
    54  	private var indentation:Number;
    55  	
    56  	/**
    57  	 * Constructs a new {@code Bit101Logger} instance.
    58  	 *
    59  	 * <p>The default log level is {@code ALL}. This means all messages regardless of
    60  	 * their level are logged.
    61  	 *
    62  	 * <p>{@code traceObject} is by default {@code false}. Refer to the {@code Debug}
    63  	 * class for information on the default {@code recursionDepth} and
    64  	 * {@code indentation}.
    65  	 *
    66  	 * @param traceObject (optional) determines whether to trace objects recursively
    67  	 * or to use the result of their {@code toString} method
    68  	 * @param recursionDepth (optional) determines the count of recursions for
    69  	 * recursively traced objects
    70  	 * @param indentation (optional) determines the indentation number for recursively
    71  	 * traced objects
    72  	 */
    73  	public function Bit101Logger(traceObject:Boolean, recursionDepth:Number, indentation:Number) {
    74  		this.traceObject = !traceObject ? false : true;
    75  		this.recursionDepth = recursionDepth;
    76  		this.indentation = indentation;
    77  		level = ALL;
    78  		levelAsNumber = level.toNumber();
    79  	}
    80  	
    81  	/**
    82  	 * Sets the log level.
    83  	 *
    84  	 * <p>The log level determines which messages are logged and which are not.
    85  	 *
    86  	 * <p>A level of value {@code null} or {@code undefined} is interpreted as level
    87  	 * {@code ALL}, which is also the default level.
    88  	 *
    89  	 * @param level the new log level
    90  	 */
    91  	public function setLevel(level:LogLevel):Void {
    92  		if (level) {
    93  			this.level = level;
    94  		} else {
    95  			this.level = ALL;
    96  		}
    97  		this.levelAsNumber = this.level.toNumber();
    98  	}
    99  	
   100  	/**
   101  	 * Returns the set level.
   102  	 *
   103  	 * @return the set level
   104  	 */
   105  	public function getLevel(Void):LogLevel {
   106  		return level;
   107  	}
   108  	
   109  	/**
   110  	 * Checks if this logger is enabled for debug level messages.
   111  	 *
   112  	 * @return {@code true} if debug messages are logged
   113  	 * @see org.as2lib.env.log.level.AbstractLogLevel#DEBUG
   114  	 * @see #debug
   115  	 */
   116  	public function isDebugEnabled(Void):Boolean {
   117  		return (levelAsNumber >= debugLevelAsNumber);
   118  	}
   119  	
   120  	/**
   121  	 * Checks if this logger is enabled for info level messages.
   122  	 *
   123  	 * @return {@code true} if info messages are logged
   124  	 * @see org.as2lib.env.log.level.AbstractLogLevel#INFO
   125  	 * @see #info
   126  	 */
   127  	public function isInfoEnabled(Void):Boolean {
   128  		return (levelAsNumber >= infoLevelAsNumber);
   129  	}
   130  	
   131  	/**
   132  	 * Checks if this logger is enabled for warning level messages.
   133  	 *
   134  	 * @return {@code true} if warning messages are logged
   135  	 * @see org.as2lib.env.log.level.AbstractLogLevel#WARNING
   136  	 * @see #warning
   137  	 */
   138  	public function isWarningEnabled(Void):Boolean {
   139  		return (levelAsNumber >= warningLevelAsNumber);
   140  	}
   141  	
   142  	/**
   143  	 * Checks if this logger is enabled for error level messages.
   144  	 *
   145  	 * @return {@code true} if error messages are logged
   146  	 * @see org.as2lib.env.log.level.AbstractLogLevel#ERROR
   147  	 * @see #error
   148  	 */
   149  	public function isErrorEnabled(Void):Boolean {
   150  		return (levelAsNumber >= errorLevelAsNumber);
   151  	}
   152  	
   153  	/**
   154  	 * Checks if this logger is enabled for fatal level messages.
   155  	 *
   156  	 * @return {@code true} if fatal messages are logged
   157  	 * @see org.as2lib.env.log.level.AbstractLogLevel#FATAL
   158  	 * @see #fatal
   159  	 */
   160  	public function isFatalEnabled(Void):Boolean {
   161  		return (levelAsNumber >= fatalLevelAsNumber);
   162  	}
   163  	
   164  	/**
   165  	 * Logs the {@code message} using the {@code Debug.trace} method if
   166  	 * {@code traceObject} is turned off or if the {@code message} is of type
   167  	 * {@code "string"}, {@code "number"}, {@code "boolean"}, {@code "undefined"} or
   168  	 * {@code "null"} and using the {@code Debug.traceObject} method if neither of the
   169  	 * above cases holds {@code true}.
   170  	 *
   171  	 * @param message the message to log
   172  	 */
   173  	public function log(message):Void {
   174  		if (this.traceObject) {
   175  			var type:String = typeof(message);
   176  			if (type == "string" || type == "number" || type == "boolean" || type == "undefined" || type == "null") {
   177  				Debug.trace(message.toString());
   178  			} else {
   179  				Debug.traceObject(message, this.recursionDepth, this.indentation);
   180  			}
   181  		} else {
   182  			Debug.trace(message.toString());
   183  		}
   184  	}
   185  	
   186  	/**
   187  	 * Logs the passed-in {@code message} at debug level.
   188  	 *
   189  	 * <p>The {@code message} is only logged when the level is set to {@code DEBUG} or
   190  	 * a level above.
   191  	 *
   192  	 * @param message the message object to log
   193  	 * @see #isDebugEnabled
   194  	 */
   195  	public function debug(message):Void {
   196  		if (isDebugEnabled()) {
   197  			log(message);
   198  		}
   199  	}
   200  	
   201  	/**
   202  	 * Logs the passed-in {@code message} object at info level.
   203  	 *
   204  	 * <p>The {@code message} is only logged when the level is set to {@code INFO} or
   205  	 * a level above.
   206  	 *
   207  	 * @param message the message object to log
   208  	 * @see #isInfoEnabled
   209  	 */
   210  	public function info(message):Void {
   211  		if (isInfoEnabled()) {
   212  			log(message);
   213  		}
   214  	}
   215  	
   216  	/**
   217  	 * Logs the passed-in {@code message} object at warning level.
   218  	 *
   219  	 * <p>The {@code message} is only logged when the level is set to {@code WARNING}
   220  	 * or a level above.
   221  	 *
   222  	 * @param message the message object to log
   223  	 * @see #isWarningEnabled
   224  	 */
   225  	public function warning(message):Void {
   226  		if (isWarningEnabled()) {
   227  			log(message);
   228  		}
   229  	}
   230  	
   231  	/**
   232  	 * Logs the passed-in {@code message} object at error level.
   233  	 *
   234  	 * <p>The {@code message} is only logged when the level is set to {@code ERROR} or a
   235  	 * level above.
   236  	 *
   237  	 * @param message the message object to log
   238  	 * @see #isErrorEnabled
   239  	 */
   240  	public function error(message):Void {
   241  		if (isErrorEnabled()) {
   242  			log(message);
   243  		}
   244  	}
   245  	
   246  	/**
   247  	 * Logs the passed-in {@code message} object at fatal level.
   248  	 *
   249  	 * <p>The {@code message} is only logged when the level is set to {@code FATAL} or
   250  	 * a level above.
   251  	 *
   252  	 * @param message the message object to log
   253  	 * @see #isFatalEnabled
   254  	 */
   255  	public function fatal(message):Void {
   256  		if (isFatalEnabled()) {
   257  			log(message);
   258  		}
   259  	}
   260  	
   261  }