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.overload.Overload;
    19  import org.as2lib.env.log.Logger;
    20  import org.as2lib.env.log.LogManager;
    21  
    22  /**
    23   * {@code MtascUtil} offers support for MTASCs extraordinary trace functionality that
    24   * does not only allow for multiple arguments but also passes information like the
    25   * class name, the file name and even the line number.
    26   * 
    27   * <p>Usage:
    28   * <pre>
    29   *   mtasc -trace org.as2lib.env.log.MtascUtil.log Test.as (...)
    30   * </pre>
    31   * 
    32   * @author Simon Wacker
    33   * @see <a href="http://www.mtasc.org/#trace">MTASC - Tracing facilities</a>
    34   */
    35  class org.as2lib.env.log.MtascUtil extends BasicClass {
    36  	
    37  	/** Debug level output. */
    38  	public static var DEBUG:Number = 2;
    39  	
    40  	/** Info level output. */
    41  	public static var INFO:Number = 3;
    42  	
    43  	/** Warning level output. */
    44  	public static var WARNING:Number = 4;
    45  	
    46  	/** Error level output. */
    47  	public static var ERROR:Number = 5;
    48  	
    49  	/** Fatal level output. */
    50  	public static var FATAL:Number = 6;
    51  	
    52  	/**
    53  	 * @overload #logByDefaultLevel
    54  	 * @overload #logByLevel
    55  	 */
    56  	public static function log():Void {
    57  		var o:Overload = new Overload(eval("th" + "is"));
    58  		o.addHandler([Object, String, String, Number], logByDefaultLevel);
    59  		o.addHandler([Object, Number, String, String, Number], logByLevel);
    60  		o.forward(arguments);
    61  	}
    62  	
    63  	/**
    64  	 * Logs the {@code message} at default level {@link #INFO}.
    65  	 * 
    66  	 * @param message the message to log
    67  	 * @param className the name of the class that logs the {@code message}
    68  	 * @param fileName the name of the file that declares the class
    69  	 * @param lineNumber the line number at which the logging call stands
    70  	 * @see #logByLevel
    71  	 */
    72  	public static function logByDefaultLevel(message:Object, className:String, fileName:String, lineNumber:Number):Void {
    73  		logByLevel(message, null, className, fileName, lineNumber);
    74  	}
    75  	
    76  	/**
    77  	 * Logs the {@code message} at the specified {@code level}.
    78  	 * 
    79  	 * <p>If this level is none of the declared ones, {@code #INFO} is used. This is
    80  	 * also the case if {@code level} is {@code null} or {@code undefined}.
    81  	 * 
    82  	 * <p>The {@code message} is logged using a logger returned by the
    83  	 * {@link LogManager#getLogger} method passing-in the given {@code className}. The
    84  	 * extra information is passed to the specific log methods as further arguments.
    85  	 * 
    86  	 * @param message the message to log
    87  	 * @param className the name of the class that logs the {@code message}
    88  	 * @param fileName the name of the file that declares the class
    89  	 * @param lineNumber the line number at which the logging call stands
    90  	 */
    91  	public static function logByLevel(message:Object, level:Number, className:String, fileName:String, lineNumber:Number):Void {
    92  		var logger:Logger = LogManager.getLogger(className);
    93  		switch (level) {
    94  			case DEBUG:
    95  				logger.debug(message, className, fileName, lineNumber);
    96  				break;
    97  			case WARNING:
    98  				logger.warning(message, className, fileName, lineNumber);
    99  				break;
   100  			case ERROR:
   101  				logger.error(message, className, fileName, lineNumber);
   102  				break;
   103  			case FATAL:
   104  				logger.fatal(message, className, fileName, lineNumber);
   105  				break;
   106  			default:
   107  				logger.info(message, className, fileName, lineNumber);
   108  				break;
   109  		}
   110  	}
   111  	
   112  	/**
   113  	 * Private constructor.
   114  	 */
   115  	private function MtascUtil(Void) {
   116  	}
   117  	
   118  }