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.LogHandler;
    19  import org.as2lib.env.log.LogMessage;
    20  import org.as2lib.env.log.LogLevel;
    21  import org.as2lib.env.log.level.AbstractLogLevel;
    22  
    23  import LuminicBox.Log.ConsolePublisher;
    24  import LuminicBox.Log.LogEvent;
    25  import LuminicBox.Log.Level;
    26  
    27  /**
    28   * {@code LuminicBoxHandler} is a wrapper of the {@code ConsolePublisher} class
    29   * from the LuminicBox Logging API. It can be used to write log messages to the
    30   * LuminicBox console.
    31   * 
    32   * @author Simon Wacker
    33   * @author Christoph Atteneder
    34   * @see <a href="http://www.luminicbox.com/dev/flash/log">LuminicBox Logging API</a>
    35   */
    36  class org.as2lib.env.log.handler.LuminicBoxHandler extends BasicClass implements LogHandler {
    37  	
    38  	/** Holds a luminic box handler. */
    39  	private static var luminicBoxHandler:LuminicBoxHandler;
    40  	
    41  	/**
    42  	 * Returns an instance of this class.
    43  	 *
    44  	 * <p>This method always returns the same instance.
    45  	 *
    46  	 * <p>The {@code maximalInspectionDepth} is only recognized when this method is
    47  	 * invoked the first time.
    48  	 *
    49  	 * @param maximalInspectionDepth (optional) the maximal depth of object inspection
    50  	 * @return a luminic box handler
    51  	 */
    52  	public static function getInstance(maximalInspectionDepth:Number):LuminicBoxHandler {
    53  		if (!luminicBoxHandler) luminicBoxHandler = new LuminicBoxHandler(maximalInspectionDepth);
    54  		return luminicBoxHandler;
    55  	}
    56  	
    57  	/** The wrapped console publisher. */
    58  	private var consolePublisher:ConsolePublisher;
    59  	
    60  	/**	
    61  	 * Constructs a new {@code LuminicBoxHandler} instance.
    62  	 *
    63  	 * <p>You can use one and the same instance for multiple loggers. So think about
    64  	 * using the handler returned by the static {@link #getInstance} method. Using
    65  	 * this instance prevents the instantiation of unnecessary luminic box handlers
    66  	 * and saves storage.
    67  	 *
    68  	 * @param maximalInspectionDepth (optional) the maximal depth of object inspection
    69  	 */
    70  	public function LuminicBoxHandler(maximalInspectionDepth:Number) {
    71  		consolePublisher = new ConsolePublisher();
    72  		if (maximalInspectionDepth != null) {
    73  			consolePublisher.maxDepth = maximalInspectionDepth;
    74  		}
    75  	}
    76  	
    77  	/**
    78  	 * Writes directly to the LuminicBox console.
    79  	 *
    80  	 * <p>The {@code ConsolePublisher} determines how the string representation is
    81  	 * constructed.
    82  	 *
    83  	 * @param message the log message to write out
    84  	 */
    85  	public function write(message:LogMessage):Void {
    86  		var event:LogEvent = new LogEvent(message.getLoggerName(), message.getMessage(), convertLevel(message.getLevel()));
    87  		event.time = new Date(message.getTimeStamp());
    88  		consolePublisher.publish(event);
    89  	}
    90  	
    91  	/**
    92  	 * Converts the As2lib {@code LogLevel} into a LuminicBox {@code Level}.
    93  	 *
    94  	 * @param level the As2lib log level to convert
    95  	 * @return the equivalent LuminicBox level
    96  	 */
    97  	private function convertLevel(level:LogLevel):Level {
    98  		switch (level) {
    99  			case AbstractLogLevel.ALL:
   100  				return Level.ALL;
   101  			case AbstractLogLevel.DEBUG:
   102  				return Level.DEBUG;
   103  			case AbstractLogLevel.INFO:
   104  				return Level.INFO;
   105  			case AbstractLogLevel.WARNING:
   106  				return Level.WARN;
   107  			case AbstractLogLevel.ERROR:
   108  				return Level.ERROR;
   109  			case AbstractLogLevel.FATAL:
   110  				return Level.FATAL;
   111  			case AbstractLogLevel.NONE:
   112  				return Level.NONE;
   113  			default:
   114  				return null;
   115  		}
   116  	}
   117  	
   118  }