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