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.Logger;
    19  
    20  import org.actionstep.ASDebugger;
    21  
    22  /**
    23   * {@code ActionStepLogger} provides support for the ActionStep Debugger.
    24   * 
    25   * <p>The actual logging is always made using the {@code org.actionstep.ASDebugger.trace}
    26   * method. No other output devices are supported. Use the {@link SimpleLogger} to be
    27   * able to add log handlers as you please which allows you to log to every device you
    28   * want.
    29   * 
    30   * <p>Configure the ActionStep Debugger API as usually and just use this class in
    31   * your application to log messages or objects. This enables you to switch between
    32   * almost every available logging API without having to change the logs in your
    33   * application but just the underlying configuration on startup.
    34   * 
    35   * @author Simon Wacker
    36   * @see org.as2lib.env.log.handler.ActionStepHandler
    37   * @see <a href="http://actionstep.sourceforge.net">ActionStep</a>
    38   */
    39  class org.as2lib.env.log.logger.ActionStepLogger extends BasicClass implements Logger {
    40  	
    41  	/** All level. */
    42  	public static var ALL:Number = 5;
    43  	
    44  	/** ActionStep debug level. */
    45  	public static var DEBUG:Number = 4;
    46  	
    47  	/** ActionStep info level. */
    48  	public static var INFO:Number = 3;
    49  	
    50  	/** ActionStep warning level. */
    51  	public static var WARNING:Number = 2;
    52  	
    53  	/** ActionStep error level. */
    54  	public static var ERROR:Number = 1;
    55  	
    56  	/** ActionStep fatal level. */
    57  	public static var FATAL:Number = 0;
    58  	
    59  	/** None level. */
    60  	public static var NONE:Number = -1;
    61  	
    62  	/** The name of this logger. */
    63  	private var name:String;
    64  	
    65  	/** The set level as number. */
    66  	private var level:Number;
    67  	
    68  	/** {@code ASDebugger} class reference for fast access. */
    69  	private var asDebugger:Function;
    70  	
    71  	/** ActionStep debug level. */
    72  	private var debugLevel:Number;
    73  	
    74  	/** ActionStep info level. */
    75  	private var infoLevel:Number;
    76  	
    77  	/** ActionStep warn level. */
    78  	private var warningLevel:Number;
    79  	
    80  	/** ActionStep error level. */
    81  	private var errorLevel:Number;
    82  	
    83  	/** ActionStep fatal level. */
    84  	private var fatalLevel:Number;
    85  	
    86  	/**
    87  	 * Constructs a new {@code ActionStepLogger} instance.
    88  	 *
    89  	 * <p>The default log level is {@code ALL}. This means all messages regardless of
    90  	 * their level are logged.
    91  	 *
    92  	 * <p>The name is used as class name for the {@code ASDebugger.trace} method, if
    93  	 * the passed class name is {@code null} or {@code undefined}.
    94  	 *
    95  	 * @param name (optional) the name of this logger
    96  	 */
    97  	public function ActionStepLogger(name:String) {
    98  		this.name = name;
    99  		this.level = ALL;
   100  		this.asDebugger = ASDebugger;
   101  		this.debugLevel = DEBUG;
   102  		this.infoLevel = INFO;
   103  		this.warningLevel = WARNING;
   104  		this.errorLevel = ERROR;
   105  		this.fatalLevel = FATAL;
   106  	}
   107  	
   108  	/**
   109  	 * Returns the name of this logger.
   110  	 *
   111  	 * <p>This method returns {@code null} if no name has been set via the
   112  	 * {@link #setName} method nor on construction.
   113  	 *
   114  	 * @return the name of this logger
   115  	 */
   116  	public function getName(Void):String {
   117  		return name;
   118  	}
   119  	
   120  	/**
   121  	 * Sets the name of this logger.
   122  	 *
   123  	 * <p>The name is used as class name for the {@code ASDebugger.trace} method, if
   124  	 * the passed class name is {@code null} or {@code undefined}.
   125  	 *
   126  	 * @param name the new name of this logger
   127  	 */
   128  	public function setName(name:String):Void {
   129  		this.name = name;
   130  	}
   131  	
   132  	/**
   133  	 * Sets the log level.
   134  	 *
   135  	 * <p>The log level determines which messages are logged and which are not.
   136  	 *
   137  	 * <p>A level of value {@code null} or {@code undefined} is interpreted as level
   138  	 * {@code ALL} which is also the default level.
   139  	 *
   140  	 * @param level the new log level
   141  	 */
   142  	public function setLevel(level:Number):Void {
   143  		if (level == null) {
   144  			this.level = ALL;
   145  		} else {
   146  			this.level = level;
   147  		}
   148  	}
   149  	
   150  	/**
   151  	 * Returns the set level.
   152  	 *
   153  	 * @return the set level
   154  	 */
   155  	public function getLevel(Void):Number {
   156  		return this.level;
   157  	}
   158  	
   159  	/**
   160  	 * Checks whether this logger is enabled for the passed-in {@code level}.
   161  	 *
   162  	 * <p>{@code false} will be returned if:
   163  	 * <ul>
   164  	 *   <li>This logger is not enabled for the passed-in {@code level}.</li>
   165  	 *   <li>The passed-in {@code level} is {@code null} or {@code undefined}.</li>
   166  	 * </ul>
   167  	 *
   168  	 * @param level the level to make the check upon
   169  	 * @return {@code true} if this logger is enabled for the given {@code level} else
   170  	 * {@code false}
   171  	 * @see #log
   172  	 */
   173  	public function isEnabled(level:Number):Boolean {
   174  		if (!level) return false;
   175  		return (this.level >= level);
   176  	}
   177  	
   178  	/**
   179  	 * Checks if this logger is enabled for debug level log messages.
   180  	 *
   181  	 * @return {@code true} if debug messages are logged
   182  	 * @see #debug
   183  	 */
   184  	public function isDebugEnabled(Void):Boolean {
   185  		return (this.level >= this.debugLevel);
   186  	}
   187  	
   188  	/**
   189  	 * Checks if this logger is enabled for info level log messages.
   190  	 *
   191  	 * @return {@code true} if info messages are logged
   192  	 * @see #info
   193  	 */
   194  	public function isInfoEnabled(Void):Boolean {
   195  		return (this.level >= this.infoLevel);
   196  	}
   197  	
   198  	/**
   199  	 * Checks if this logger is enabled for warning level log messages.
   200  	 * 
   201  	 * @return {@code true} if warning messages are logged
   202  	 * @see #warning
   203  	 */
   204  	public function isWarningEnabled(Void):Boolean {
   205  		return (this.level >= this.warningLevel);
   206  	}
   207  	
   208  	/**
   209  	 * Checks if this logger is enabled for error level log messages.
   210  	 * 
   211  	 * @return {@code true} if error messages are logged
   212  	 * @see #error
   213  	 */
   214  	public function isErrorEnabled(Void):Boolean {
   215  		return (this.level >= this.errorLevel);
   216  	}
   217  	
   218  	/**
   219  	 * Checks if this logger is enabled for fatal level log messages.
   220  	 * 
   221  	 * @return {@code true} if fatal messages are logged
   222  	 * @see #fatal
   223  	 */
   224  	public function isFatalEnabled(Void):Boolean {
   225  		return (this.level >= this.fatalLevel);
   226  	}
   227  	
   228  	/**
   229  	 * Logs the passed-in {@code message} at the given {@code level}.
   230  	 *
   231  	 * <p>The {@code message} is only logged when this logger is enabled for the
   232  	 * passed-in {@code level}.
   233  	 *
   234  	 * <p>The {@code message} is always logged using {@code ASDebugger.trace} passing
   235  	 * at least the arguments {@code message} and {@code level} and {@code className},
   236  	 * {@code fileName} and {@code lineNumber} if specified.
   237  	 * 
   238  	 * <p>If {@code className} is {@code null} or {@code undefined}, the name of this
   239  	 * logger is used instead.
   240  	 *
   241  	 * @param message the message object to log
   242  	 * @param level the specific level at which the {@code message} shall be logged
   243  	 * @param className (optional) the name of the class that logs the {@code message}
   244  	 * @param fileName (optional) the name of the file that declares the class
   245  	 * @param lineNumber (optional) the line number at which the logging call stands
   246  	 * @see #isEnabled
   247  	 */
   248  	public function log(message, level:Number, className:String, fileName:String, lineNumber:Number):Void {
   249  		if (isEnabled(level)) {
   250  			if (className == null) className = this.name;
   251  			asDebugger.trace(message, level, className, fileName, lineNumber);
   252  		}
   253  	}
   254  	
   255  	/**
   256  	 * Logs the passed-in {@code message} at debug level.
   257  	 *
   258  	 * <p>The {@code message} is only logged when the level is set to {@code DEBUG} or
   259  	 * a level above.
   260  	 *
   261  	 * <p>The {@code message} is always logged using {@code ASDebugger.trace} passing
   262  	 * at least the arguments {@code message}, the debug level and {@code className},
   263  	 * {@code fileName} and {@code lineNumber} if specified.
   264  	 * 
   265  	 * <p>If {@code className} is {@code null} or {@code undefined}, the name of this
   266  	 * logger is used instead.
   267  	 *
   268  	 * @param message the message object to log
   269  	 * @param className (optional) the name of the class that logs the {@code message}
   270  	 * @param fileName (optional) the name of the file that declares the class
   271  	 * @param lineNumber (optional) the line number at which the logging call stands
   272  	 * @see #isDebugEnabled
   273  	 */
   274  	public function debug(message, className:String, fileName:String, lineNumber:Number):Void {
   275  		if (isDebugEnabled()) {
   276  			if (className == null) className = this.name;
   277  			asDebugger.trace(message, this.debugLevel, className, fileName, lineNumber);
   278  		}
   279  	}
   280  	
   281  	/**
   282  	 * Logs the passed-in {@code message} at info level.
   283  	 *
   284  	 * <p>The {@code message} is only logged when the level is set to {@code INFO} or
   285  	 * a level above.
   286  	 *
   287  	 * <p>The {@code message} is always logged using {@code ASDebugger.trace} passing
   288  	 * at least the arguments {@code message}, the info level and {@code className},
   289  	 * {@code fileName} and {@code lineNumber} if specified.
   290  	 * 
   291  	 * <p>If {@code className} is {@code null} or {@code undefined}, the name of this
   292  	 * logger is used instead.
   293  	 *
   294  	 * @param message the message object to log
   295  	 * @param className (optional) the name of the class that logs the {@code message}
   296  	 * @param fileName (optional) the name of the file that declares the class
   297  	 * @param lineNumber (optional) the line number at which the logging call stands
   298  	 * @see #isInfoEnabled
   299  	 */
   300  	public function info(message, className:String, fileName:String, lineNumber:Number):Void {
   301  		if (isInfoEnabled()) {
   302  			if (className == null) className = this.name;
   303  			asDebugger.trace(message, this.infoLevel, className, fileName, lineNumber);
   304  		}
   305  	}
   306  	
   307  	/**
   308  	 * Logs the passed-in {@code message} at warning level.
   309  	 *
   310  	 * <p>The {@code message} is only logged when the level is set to {@code WARNING}
   311  	 * or a level above.
   312  	 *
   313  	 * <p>The {@code message} is always logged using {@code ASDebugger.trace} passing
   314  	 * at least the arguments {@code message}, the warning level and {@code className},
   315  	 * {@code fileName} and {@code lineNumber} if specified.
   316  	 * 
   317  	 * <p>If {@code className} is {@code null} or {@code undefined}, the name of this
   318  	 * logger is used instead.
   319  	 *
   320  	 * @param message the message object to log
   321  	 * @param className (optional) the name of the class that logs the {@code message}
   322  	 * @param fileName (optional) the name of the file that declares the class
   323  	 * @param lineNumber (optional) the line number at which the logging call stands
   324  	 * @see #isWarningEnabled
   325  	 */
   326  	public function warning(message, className:String, fileName:String, lineNumber:Number):Void {
   327  		if (isWarningEnabled()) {
   328  			if (className == null) className = this.name;
   329  			asDebugger.trace(message, this.warningLevel, className, fileName, lineNumber);
   330  		}
   331  	}
   332  	
   333  	/**
   334  	 * Logs the passed-in {@code message} at error level.
   335  	 *
   336  	 * <p>The {@code message} is only logged when the level is set to {@code ERROR} or
   337  	 * a level above.
   338  	 *
   339  	 * <p>The {@code message} is always logged using {@code ASDebugger.trace} passing
   340  	 * at least the arguments {@code message}, the error level and {@code className},
   341  	 * {@code fileName} and {@code lineNumber} if specified.
   342  	 * 
   343  	 * <p>If {@code className} is {@code null} or {@code undefined}, the name of this
   344  	 * logger is used instead.
   345  	 *
   346  	 * @param message the message object to log
   347  	 * @param className (optional) the name of the class that logs the {@code message}
   348  	 * @param fileName (optional) the name of the file that declares the class
   349  	 * @param lineNumber (optional) the line number at which the logging call stands
   350  	 * @see #isErrorEnabled
   351  	 */
   352  	public function error(message, className:String, fileName:String, lineNumber:Number):Void {
   353  		if (isErrorEnabled()) {
   354  			if (className == null) className = this.name;
   355  			asDebugger.trace(message, this.errorLevel, className, fileName, lineNumber);
   356  		}
   357  	}
   358  	
   359  	/**
   360  	 * Logs the passed-in {@code message} at fatal level.
   361  	 * 
   362  	 * <p>The {@code message} is only logged when the level is set to {@code FATAL} or
   363  	 * a level above.
   364  	 * 
   365  	 * <p>The {@code message} is always logged using {@code ASDebugger.trace} passing
   366  	 * at least the arguments {@code message}, the fatal level and {@code className},
   367  	 * {@code fileName} and {@code lineNumber} if specified.
   368  	 * 
   369  	 * <p>If {@code className} is {@code null} or {@code undefined}, the name of this
   370  	 * logger is used instead.
   371  	 * 
   372  	 * @param message the message object to log
   373  	 * @param className (optional) the name of the class that logs the {@code message}
   374  	 * @param fileName (optional) the name of the file that declares the class
   375  	 * @param lineNumber (optional) the line number at which the logging call stands
   376  	 * @see #isFatalEnabled
   377  	 */
   378  	public function fatal(message, className:String, fileName:String, lineNumber:Number):Void {
   379  		if (isFatalEnabled()) {
   380  			if (className == null) className = this.name;
   381  			asDebugger.trace(message, this.fatalLevel, className, fileName, lineNumber);
   382  		}
   383  	}
   384  	
   385  }