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.logger.AbstractLogger;
    20  
    21  /**
    22   * {@code FludgeLogger} delegates all log messages to the appropriate methods on
    23   * the {@code Fludge} class.
    24   * 
    25   * <p>Using this class instead of the {@code Fludge} class in your application
    26   * directly enables you to switch between almost every available Logging API without
    27   * having to change the logging calls, but just the underlying configuration on
    28   * startup.
    29   *
    30   * @author Simon Wacker
    31   * @see org.as2lib.env.log.handler.FludgeHandler
    32   * @see <a href="http://www.osflash.org/doku.php?id=fludge">Fludge</a>
    33   */
    34  class org.as2lib.env.log.logger.FludgeLogger extends AbstractLogger implements Logger {
    35  	
    36  	/** Makes the static variables of the super-class accessible through this class. */
    37  	private static var __proto__:Function = AbstractLogger;
    38  	
    39  	/** The set level. */
    40  	private var level:LogLevel;
    41  	
    42  	/** The set level as number. */
    43  	private var levelAsNumber:Number;
    44  	
    45  	/**
    46  	 * Constructs a new {@code FludgeLogger} instance.
    47  	 *
    48  	 * <p>The default log level is {@code ALL}. This means all messages regardless of
    49  	 * their level are logged.
    50  	 */
    51  	public function FludgeLogger(Void) {
    52  		level = ALL;
    53  		levelAsNumber = level.toNumber();
    54  	}
    55  	
    56  	/**
    57  	 * Sets the log level.
    58  	 *
    59  	 * <p>The log level determines which messages are logged and which are not.
    60  	 *
    61  	 * <p>A level of value {@code null} or {@code undefined} os interpreted as level
    62  	 * {@code ALL} which is also the default level.
    63  	 *
    64  	 * @param level the new log level
    65  	 */
    66  	public function setLevel(level:LogLevel):Void {
    67  		if (level) {
    68  			this.level = level;
    69  			levelAsNumber = level.toNumber();
    70  		} else {
    71  			this.level = ALL;
    72  			levelAsNumber = level.toNumber();
    73  		}
    74  	}
    75  	
    76  	/**
    77  	 * Returns the set level.
    78  	 *
    79  	 * @return the set level
    80  	 */
    81  	public function getLevel(Void):LogLevel {
    82  		return level;
    83  	}
    84  	
    85  	/**
    86  	 * Checks if this logger is enabled for debug level log messages.
    87  	 *
    88  	 * @return {@code true} if debug messages are logged
    89  	 * @see org.as2lib.env.log.level.AbstractLogLevel#DEBUG
    90  	 * @see #debug
    91  	 */
    92  	public function isDebugEnabled(Void):Boolean {
    93  		return (levelAsNumber >= debugLevelAsNumber);
    94  	}
    95  	
    96  	/**
    97  	 * Checks if this logger is enabled for info level log messages.
    98  	 *
    99  	 * @return {@code true} if info messages are logged
   100  	 * @see org.as2lib.env.log.level.AbstractLogLevel#INFO
   101  	 * @see #info
   102  	 */
   103  	public function isInfoEnabled(Void):Boolean {
   104  		return (levelAsNumber >= infoLevelAsNumber);
   105  	}
   106  	
   107  	/**
   108  	 * Checks if this logger is enabled for warning level log messages.
   109  	 *
   110  	 * @return {@code true} if warning messages are logged
   111  	 * @see org.as2lib.env.log.level.AbstractLogLevel#WARNING
   112  	 * @see #warning
   113  	 */
   114  	public function isWarningEnabled(Void):Boolean {
   115  		return (levelAsNumber >= warningLevelAsNumber);
   116  	}
   117  	
   118  	/**
   119  	 * Checks if this logger is enabled for error level log messages.
   120  	 *
   121  	 * @return {@code true} if error messages are logged
   122  	 * @see org.as2lib.env.log.level.AbstractLogLevel#ERROR
   123  	 * @see #error
   124  	 */
   125  	public function isErrorEnabled(Void):Boolean {
   126  		return (levelAsNumber >= errorLevelAsNumber);
   127  	}
   128  	
   129  	/**
   130  	 * Checks if this logger is enabled for fatal level log messages.
   131  	 *
   132  	 * @return {@code true} if fatal messages are logged
   133  	 * @see org.as2lib.env.log.level.AbstractLogLevel#FATAL
   134  	 * @see #fatal
   135  	 */
   136  	public function isFatalEnabled(Void):Boolean {
   137  		return (levelAsNumber >= fatalLevelAsNumber);
   138  	}
   139  	
   140  	/**
   141  	 * Logs the passed-in {@code message} at debug level.
   142  	 *
   143  	 * <p>The {@code message} is only logged when the level is set to {@code DEBUG} or
   144  	 * a level above.
   145  	 * 
   146  	 * <p>Because fludge does not support the debug level the default level info is
   147  	 * used.
   148  	 *
   149  	 * @param message the message object to log
   150  	 * @see #isDebugEnabled
   151  	 */
   152  	public function debug(message):Void {
   153  		if (isDebugEnabled()) {
   154  			Fludge.trace(message.toString(), "debug");
   155  		}
   156  	}
   157  	
   158  	/**
   159  	 * Logs the passed-in {@code message} object at info level.
   160  	 *
   161  	 * <p>The {@code message} is only logged when the level is set to {@code INFO} or
   162  	 * a level above.
   163  	 *
   164  	 * @param message the message object to log
   165  	 * @see #isInfoEnabled
   166  	 */
   167  	public function info(message):Void {
   168  		if (isInfoEnabled()) {
   169  			Fludge.trace(message.toString(), "info");
   170  		}
   171  	}
   172  	
   173  	/**
   174  	 * Logs the passed-in {@code message} object at warning level.
   175  	 *
   176  	 * <p>The {@code message} is only logged when the level is set to {@code WARNING}
   177  	 * or a level above.
   178  	 *
   179  	 * @param message the message object to log
   180  	 * @see #isWarningEnabled
   181  	 */
   182  	public function warning(message):Void {
   183  		if (isWarningEnabled()) {
   184  			Fludge.trace(message.toString(), "warn");
   185  		}
   186  	}
   187  	
   188  	/**
   189  	 * Logs the passed-in {@code message} object at error level.
   190  	 *
   191  	 * <p>The {@code message} is only logged when the level is set to {@code ERROR} or
   192  	 * a level above.
   193  	 *
   194  	 * @param message the message object to log
   195  	 * @see #isErrorEnabled
   196  	 */
   197  	public function error(message):Void {
   198  		if (isErrorEnabled()) {
   199  			Fludge.trace(message.toString(), "error");
   200  		}
   201  	}
   202  	
   203  	/**
   204  	 * Logs the passed-in {@code message} object at fatal level.
   205  	 *
   206  	 * <p>The {@code message} is only logged when the level is set to {@code FATAL} or
   207  	 * a level above.
   208  	 *
   209  	 * <p>The equivalent level for fatal in fludge is exception.
   210  	 *
   211  	 * @param message the message object to log
   212  	 * @see #isFatalEnabled
   213  	 */
   214  	public function fatal(message):Void {
   215  		if (isFatalEnabled()) {
   216  			Fludge.trace(message.toString(), "exception");
   217  		}
   218  	}
   219  	
   220  }