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.log.LogHandler;
    20  import org.as2lib.env.log.LogMessage;
    21  import org.as2lib.env.log.level.AbstractLogLevel;
    22  
    23  import net.hiddenresource.util.Debug;
    24  
    25  /**
    26   * {@code AlconHandler} uses the {@code net.hiddenresource.util.Debug} class
    27   * from Sascha Balkau to log messages.
    28   * 
    29   * @author Simon Wacker
    30   * @see org.as2lib.env.log.logger.AlconLogger
    31   * @see <a href="http://hiddenresource.corewatch.net/index.php?itemid=17">Alcon</a>
    32   */
    33  class org.as2lib.env.log.handler.AlconHandler extends BasicClass implements LogHandler {
    34  	
    35  	/** Holds a alcon handler. */
    36  	private static var alconHandler:AlconHandler;
    37  	
    38  	/**
    39  	 * Returns an instance of this class.
    40  	 *
    41  	 * <p>This method always returns the same instance.
    42  	 *
    43  	 * <p>Note that the two arguments {@code decorateMethod} and {@code recursiveTracing}
    44  	 * are only recognized on first call of this method.
    45  	 * 
    46  	 * @param decorateMethod (optional) determines whether to use the string returned
    47  	 * by {@code LogMessage.toString} or only the original message returned by
    48  	 * {@code LogMessage.getMessage} for logging
    49  	 * @param recursiveTracing (optional) determines whether messages shall be traced
    50  	 * recursively or not
    51  	 * @return a alcon handler
    52  	 */
    53  	public static function getInstance(decorateMethod:Boolean, recursiveTracing:Boolean):AlconHandler {
    54  		if (!alconHandler) alconHandler = new AlconHandler(decorateMethod, recursiveTracing);
    55  		return alconHandler;
    56  	}
    57  	
    58  	/** Determines whether to decorate the message. */
    59  	private var decorateMethod:Boolean;
    60  	
    61  	/** Determines whether to trace recursively or not. */
    62  	private var recursiveTracing:Boolean;
    63  	
    64  	/**	
    65  	 * Constructs a new {@code AlconHandler} instance.
    66  	 *
    67  	 * <p>You can use one and the same instance for multiple loggers. So think about
    68  	 * using the handler returned by the static {@link #getInstance} method. Using
    69  	 * this instance prevents the instantiation of unnecessary alcon handlers and
    70  	 * saves storage.
    71  	 *
    72  	 * <p>{@code decorateMethod} is by default {@code true} and {@code recursiveTracing}
    73  	 * {@code false}.
    74  	 *
    75  	 * <p>Note that {@code recursiveTracing} is turned off when {@code decorateMethod}
    76  	 * is turned on.
    77  	 *
    78  	 * @param decorateMethod (optional) determines whether to use the string returned
    79  	 * by {@code LogMessage.toString} or only the original message returned by
    80  	 * {@code LogMessage.getMessage} for logging
    81  	 * @param recursiveTracing (optional) determines whether messages shall be traced
    82  	 * recursively or not
    83  	 */
    84  	public function AlconHandler(decorateMethod:Boolean, recursiveTracing:Boolean) {
    85  		this.decorateMethod = decorateMethod == null ? true : decorateMethod;
    86  		this.recursiveTracing = !recursiveTracing ? false : true;
    87  	}
    88  	
    89  	/**
    90  	 * Uses the {@code AlconHandler} class to log the {@code message}.
    91  	 *
    92  	 * @param message the message to log
    93  	 */
    94  	public function write(message:LogMessage):Void {
    95  		if (this.decorateMethod) {
    96  			Debug.trace(message.toString(), convertLevel(message.getLevel()), this.recursiveTracing);
    97  		} else {
    98  			Debug.trace(message.getMessage(), convertLevel(message.getLevel()), this.recursiveTracing);
    99  		}
   100  	}
   101  	
   102  	/**
   103  	 * Converts the As2lib {@code level} into the corresponding Alcon level number.
   104  	 * 
   105  	 * @param level the As2lib level to convert
   106  	 * @return the corresponding Alcon level number or {@code null}
   107  	 */
   108  	private function convertLevel(level:LogLevel):Number {
   109  		switch (level) {
   110  			case AbstractLogLevel.DEBUG:
   111  				return 0;
   112  			case AbstractLogLevel.INFO:
   113  				return 1;
   114  			case AbstractLogLevel.WARNING:
   115  				return 2;
   116  			case AbstractLogLevel.ERROR:
   117  				return 3;
   118  			case AbstractLogLevel.FATAL:
   119  				return 4;
   120  			default:
   121  				return null;
   122  		}
   123  	}
   124  	
   125  }