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.LogLevel;
    19  import org.as2lib.env.except.IllegalArgumentException;
    20  
    21  /**
    22   * {@code AbstractLogLevel} acts as a basic access point for the pre-defined levels
    23   * {@link #ALL}, {@link #DEBUG}, {@link #INFO}, {@link #WARNING}, {@link #ERROR},
    24   * {@link #FATAL} and {@link #NONE}.
    25   * 
    26   * @author Simon Wacker
    27   */
    28  class org.as2lib.env.log.level.AbstractLogLevel extends BasicClass implements LogLevel {
    29  	
    30  	/** All log messages get logged. */
    31  	public static var ALL:LogLevel = new AbstractLogLevel(60, "ALL");
    32  	
    33  	/** All log messages that are at a higher log level than debug get logged. */
    34  	public static var DEBUG:LogLevel = new AbstractLogLevel(50, "DEBUG");
    35  	
    36  	/** All log messages that are at a higher log level than info get logged. */
    37  	public static var INFO:LogLevel = new AbstractLogLevel(40, "INFO");
    38  	
    39  	/** All log messages that are at a higher log level than warning get logged. */
    40  	public static var WARNING:LogLevel = new AbstractLogLevel(30, "WARNING");
    41  	
    42  	/** All log messages that are at a higher log level than error get logged. */
    43  	public static var ERROR:LogLevel = new AbstractLogLevel(20, "ERROR");
    44  	
    45  	/** All log messages that are at a higher log level than fatal get logged. */
    46  	public static var FATAL:LogLevel = new AbstractLogLevel(10, "FATAL");
    47  	
    48  	/** No log messages get logged. */
    49  	public static var NONE:LogLevel = new AbstractLogLevel(0, "NONE");
    50  	
    51  	/**
    52  	 * Returns the log level for the given {@code name}.
    53  	 * 
    54  	 * <p>If the given {@code name} is not registered to any logger, {@link INFO} is
    55  	 * returned.
    56  	 * 
    57  	 * @param name the name of the log level to return
    58  	 * @return the log level for the given {@code name}
    59  	 */
    60  	public static function forName(name:String):LogLevel {
    61  		switch (name) {
    62  			case "ALL":
    63  				return ALL;
    64  			case "DEBUG":
    65  				return DEBUG;
    66  			case "INFO":
    67  				return INFO;
    68  			case "WARNING":
    69  				return WARNING;
    70  			case "ERROR":
    71  				return ERROR;
    72  			case "FATAL":
    73  				return FATAL;
    74  			case "NONE":
    75  				return NONE;
    76  			default:
    77  				return INFO;
    78  		}
    79  	}
    80  	
    81  	/** Stores the level in form of a number. */
    82  	private var level:Number;
    83  	
    84  	/** The name of the level. */
    85  	private var name:String;
    86  	
    87  	/**
    88  	 * Constructs a new {@code AbstractLogLevel} instance.
    89  	 *
    90  	 * @param level the level represented by a number
    91  	 * @param name the name of the level
    92  	 * @throws IllegalArgumentException if passed-in {@code level} is {@code null} or
    93  	 * {@code undefined}
    94  	 */
    95  	private function AbstractLogLevel(level:Number, name:String) {
    96  		if (level == null) throw new IllegalArgumentException("Level is not allowed to be null or undefined.", this, arguments);
    97  		this.level = level;
    98  		this.name = name;
    99  	}
   100  	
   101  	/**
   102  	 * Compares the number representation of this level with the one of the passed-in
   103  	 * {@code level} using the is greater or equal operator.
   104  	 *
   105  	 * <p>{@code true} will be returned if:
   106  	 * <ul>
   107  	 *   <li>This level is greater or equal than the passed-in {@code level}.</li>
   108  	 *   <li>The passed-in {@code level} is {@code null} or {@code undefined}.</li>
   109  	 * </ul>
   110  	 *
   111  	 * @param level the level to compare this level with
   112  	 * @return {@code true} if this level is greater or equal than the passed-in
   113  	 * {@code level} else {@code false}
   114  	 */
   115  	public function isGreaterOrEqual(level:LogLevel):Boolean {
   116  		return (this.level >= level.toNumber());
   117  	}
   118  	
   119  	/**
   120  	 * Returns the number representation of this level.
   121  	 *
   122  	 * <p>The return value is never {@code null} or {@code undefined}.
   123  	 *
   124  	 * @return the number representation of this level
   125  	 */
   126  	public function toNumber(Void):Number {
   127  		return level;
   128  	}
   129  	
   130  	/**
   131  	 * Returns the string representation of this level.
   132  	 *
   133  	 * @return the string representation of this level
   134  	 */
   135  	public function toString():String {
   136  		return name;
   137  	}
   138  	
   139  }