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.util.Stringifier;
    18  import org.as2lib.env.log.LogHandler;
    19  import org.as2lib.env.log.LogMessage;
    20  import org.as2lib.env.log.LogLevel;
    21  import org.as2lib.env.log.level.AbstractLogLevel;
    22  import org.as2lib.env.log.handler.AbstractLogHandler;
    23  
    24  /**
    25   * {@code ZtorLog4fHandler} uses the {@code Log4f.log} method of the ZTOR Log4f
    26   * project to log messages.
    27   * 
    28   * @author Simon Wacker
    29   * @see org.as2lib.env.log.logger.ZtorLog4fLogger
    30   * @see <a href="http://www.ztor.com/index.php4?ln=&g=comp&d=log4f">ZTOR Log4f</a>
    31   */
    32  class org.as2lib.env.log.handler.ZtorLog4fHandler extends AbstractLogHandler implements LogHandler {
    33  	
    34  	/** Holds a log4f handler instance. */
    35  	private static var ztorLog4fHandler:ZtorLog4fHandler;
    36  	
    37  	/**
    38  	 * Returns an instance of this class.
    39  	 *
    40  	 * <p>This method always returns the same instance.
    41  	 *
    42  	 * <p>The {@code messageStringifier} argument is only recognized on first
    43  	 * invocation of this method.
    44  	 * 
    45  	 * @param messageStringifier (optional) the log message stringifier to be used by
    46  	 * the returned handler
    47  	 * @return a ztor log4f handler
    48  	 */
    49  	public static function getInstance(messageStringifier:Stringifier):ZtorLog4fHandler {
    50  		if (!ztorLog4fHandler) ztorLog4fHandler = new ZtorLog4fHandler(messageStringifier);
    51  		return ztorLog4fHandler;
    52  	}
    53  	
    54  	/**	
    55  	 * Constructs a new {@code ZtorLog4fHandler} instance.
    56  	 *
    57  	 * <p>You can use one and the same instance for multiple loggers. So think about
    58  	 * using the handler returned by the static {@link #getInstance} method. Using this
    59  	 * instance prevents the instantiation of unnecessary trace handlers and saves
    60  	 * storage.
    61  	 * 
    62  	 * @param messageStringifier (optional) the log message stringifier to use
    63  	 */
    64  	public function ZtorLog4fHandler(messageStringifier:Stringifier) {
    65  		super (messageStringifier);
    66  	}
    67  	
    68  	/**
    69  	 * Writes the passed-in {@code message} using the {@code Log4f.log} method.
    70  	 *
    71  	 * <p>The string representation of the {@code message} to log is obtained via
    72  	 * the {@code convertMessage} method and passed as header to the {@code Log4f.log}
    73  	 * method.
    74  	 * 
    75  	 * @param message the message to log
    76  	 */
    77  	public function write(message:LogMessage):Void {
    78  		Log4f.log(convertLevel(message.getLevel()), convertMessage(message), "");
    79  	}
    80  	
    81  	/**
    82  	 * Converts the As2lib {@code LogLevel} into a ZTOR Log4f level number.
    83  	 * 
    84  	 * <p>The default level is {@code Log4f.LOG4F}. It is used if no match is found.
    85  	 * 
    86  	 * @param level the As2lib log level to convert
    87  	 * @return the equivalent ZTOR Log4f level
    88  	 */
    89  	private function convertLevel(level:LogLevel):Number {
    90  		switch (level) {
    91  			case AbstractLogLevel.DEBUG:
    92  				return Log4f.DEBUG;
    93  			case AbstractLogLevel.INFO:
    94  				return Log4f.INFO;
    95  			case AbstractLogLevel.WARNING:
    96  				return Log4f.WARN;
    97  			case AbstractLogLevel.ERROR:
    98  				return Log4f.ERROR;
    99  			case AbstractLogLevel.FATAL:
   100  				return Log4f.FATAL;
   101  			default:
   102  				return Log4f.LOG4F;
   103  		}
   104  	}
   105  	
   106  }