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  import org.as2lib.env.reflect.ReflectUtil;
    20  import org.as2lib.env.log.LogManager;
    21  
    22  /**
    23   * {@code LogSupport} can be used to easily gain access to loggers.
    24   * 
    25   * <p>You may extend this class to be able to access the logger appropriate to
    26   * your specific class with the getter property {@link logger} or the {@code getLogger}
    27   * method.
    28   * 
    29   * <p>Example:
    30   * <code>
    31   *   class MyClass extends LogSupport {
    32   *     
    33   *     public function test() {
    34   *       logger.info("hi");
    35   *     }
    36   *     
    37   *   }
    38   * </code>
    39   * 
    40   * @author Martin Heidegger
    41   * @version 2.0
    42   */
    43  class org.as2lib.env.log.LogSupport extends BasicClass {
    44  	
    45  	/**
    46  	 * Returns the logger for the given {@code scope}.
    47  	 * 
    48  	 * @param scope the scope to return a logger for
    49  	 * @return the logger corresponding to the given {@code scope}
    50  	 */
    51  	public static function getLoggerByScope(scope):Logger {
    52  		if (typeof scope == "function") {
    53  			return getLoggerByClass(scope);
    54  		} else {
    55  			return getLoggerByInstance(scope);
    56  		}
    57  	}
    58  	
    59  	/**
    60  	 * Return the logger for the given {@code instance}.
    61  	 * 
    62  	 * @param instance the instance to return a logger for
    63  	 * @return the logger corresponding to the given {@code instance}
    64  	 */
    65  	public static function getLoggerByInstance(instance):Logger {
    66  		var p = instance.__proto__;
    67  		if (p.__as2lib__logger !== null && p.__as2lib__logger === p.__proto__.__as2lib__logger) {
    68  			return storeLoggerInPrototype(p, LogManager.getLogger(
    69  				ReflectUtil.getTypeNameForInstance(instance)));
    70  		}
    71  		return p.__as2lib__logger;
    72  	}
    73  	
    74  	/**
    75  	 * Returns the logger for the given {@code clazz}.
    76  	 * 
    77  	 * @param clazz the clazz to return a logger for
    78  	 * @return the logger corresponding to the given {@code clazz}
    79  	 */
    80  	public static function getLoggerByClass(clazz:Function):Logger {
    81  		var	p = clazz["prototype"];
    82  		if (p.__as2lib__logger !== null && p.__as2lib__logger === p.__proto__.__as2lib__logger) {
    83  			return storeLoggerInPrototype(p, LogManager.getLogger(
    84  				ReflectUtil.getTypeNameForType(clazz)));
    85  		}
    86  		return p.__as2lib__logger;
    87  	}
    88  	
    89  	/**
    90  	 * Stores the given {@code logger} in the given {@code prototype}.
    91  	 * 
    92  	 * @param prototype the prototype to store the given {@code logger} in
    93  	 * @param logger the logger to store in the given {@code prototype}
    94  	 * @return the given {@code logger}
    95  	 */
    96  	private static function storeLoggerInPrototype(prototype, logger:Logger):Logger {
    97  		prototype.__as2lib__logger = logger;
    98  		if (!prototype.__as2lib__logger) {
    99  			prototype.__as2lib__logger = null;
   100  		}
   101  		return logger;
   102  	}
   103  	
   104  	/** The logger for your sub-class. */
   105  	public var logger:Logger;
   106  	
   107  	/**
   108  	 * Constructs a new {@code LogSupport} instance.
   109  	 */
   110  	public function LogSupport(Void) {
   111  		// creates property within constructor because of Macromedia scope bug
   112  		addProperty("logger", getLogger, null);
   113  	}
   114  	
   115  	/**
   116  	 * Returns the logger for this instance.
   117  	 * 
   118  	 * @return the logger corresponding to this instance
   119  	 */
   120  	public function getLogger(Void):Logger {
   121  		return getLoggerByInstance(this);
   122  	}
   123  	
   124  }