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.env.except.IllegalArgumentException; 18 import org.as2lib.env.log.LogHandler; 19 import org.as2lib.env.log.LogLevel; 20 import org.as2lib.env.log.LogMessage; 21 import org.as2lib.env.log.level.AbstractLogLevel; 22 23 /** 24 * {@code LevelFilterHandler} filters log messages depending on their level. 25 * 26 * @author Simon Wacker 27 */ 28 class org.as2lib.env.log.handler.LevelFilterHandler implements LogHandler { 29 30 /** All log messages get logged. */ 31 public static var ALL:LogLevel = AbstractLogLevel.ALL; 32 33 /** All log messages that are at a lower log level than debug get logged. */ 34 public static var DEBUG:LogLevel = AbstractLogLevel.DEBUG; 35 36 /** All log messages that are at a lower log level than info get logged. */ 37 public static var INFO:LogLevel = AbstractLogLevel.INFO; 38 39 /** All log messages that are at a lower log level than warning get logged. */ 40 public static var WARNING:LogLevel = AbstractLogLevel.WARNING; 41 42 /** All log messages that are at a lower log level than error get logged. */ 43 public static var ERROR:LogLevel = AbstractLogLevel.ERROR; 44 45 /** All log messages that are at a lower log level than fatal get logged. */ 46 public static var FATAL:LogLevel = AbstractLogLevel.FATAL; 47 48 /** No log messages get logged. */ 49 public static var NONE:LogLevel = AbstractLogLevel.NONE; 50 51 /** The wrapped handler to forward not-filtered log messages to. */ 52 private var handler:LogHandler; 53 54 /** The lowest level that is not filtered. */ 55 private var level:LogLevel; 56 57 /** The lowest level as number that is not filtered. */ 58 private var levelAsNumber:Number; 59 60 /** 61 * Constructs a new {@code LevelFilterHandler} instance. 62 * 63 * <p>All log messages with a lower level than the passed-in {@code level} get 64 * filtered out. 65 * 66 * <p>If {@code level} is not passed-in or is {@code null} or {@code undefined} all 67 * levels will be allowed, this means it will be set to 68 * {@link AbstractLogLevel#ALL}. 69 * 70 * @param handler the handler to forward not-filtered log messages to 71 * @param (optional) level the log level determining which log messages to filter 72 * @throws IllegalArgumentException if the passed-in {@code handler} is 73 * {@code null} or {@code undefined} 74 */ 75 public function LevelFilterHandler(handler:LogHandler, level:LogLevel) { 76 if (!handler) throw new IllegalArgumentException("Argument 'handler' [" + handler + "] must not be 'null' nor 'undefined'.", this, arguments); 77 this.handler = handler; 78 this.level = level ? level : AbstractLogLevel.ALL; 79 this.levelAsNumber = this.level.toNumber(); 80 } 81 82 /** 83 * Returns the wrapped handler not-filtered log messages are fowarded to. 84 * 85 * @return the wrapped handler 86 */ 87 public function getHandler(Void):LogHandler { 88 return this.handler; 89 } 90 91 /** 92 * Returns the lowest level messages can have that are not filtered. All messages 93 * at a lower level than the returned one are filtered. 94 * 95 * @return the lowest level of messages that are not filtered 96 */ 97 public function getLevel(Void):LogLevel { 98 return this.level; 99 } 100 101 /** 102 * Forwards that passed-in {@code message} to the wrapped handler if the message 103 * has a higher or the same level than the specified one. 104 * 105 * @param message the message to forward to the wrapped handler 106 */ 107 public function write(message:LogMessage):Void { 108 if (this.levelAsNumber >= message.getLevel().toNumber()) { 109 this.handler.write(message); 110 } 111 } 112 113 }