topical media & game development

talk show tell print

actionscript-lib-logger-Logger.ax

actionscript-lib-logger-Logger.ax [swf] flex


  package  {
    //import util.Observable;
  
    
A general log class. Use getLog() to create an app-wide instance. Send messages with fatal(), error(), warn(), info(), and debug(). Add views for the log with addObserver() (views must implement Observer).
version: 1.0.0

  
    public class @ax-actionscript-lib-logger-Logger extends actionscript_lib_util_Observable {
      // Static variable. A reference to the log instance (Singleton).
      private static var log:@ax-actionscript-lib-logger-Logger = null;
  
      // The possible log levels for a message.
      public static const FATAL:Number = 0;
      public static const ERROR:Number = 1;
      public static const WARN:Number  = 2;
      public static const INFO:Number  = 3;
      public static const DEBUG:Number = 4;
  
      private var lastMsg:actionscript_lib_logger_LogMessage;
  
      // The human-readable descriptions of the above log levels.
      public static var levelDescriptions:Array = ["FATAL", "ERROR", 
                                             "WARN", "INFO", "DEBUG"];
  
      // The zero-relative filter level for the log. Messages with a level 
      // above logLevel will not be passed on to observers.
      // Default is 3, "INFO" (only DEBUG messages are filtered out).
      private var logLevel:Number;
  
      
@ax-actionscript-lib-logger-Logger Constructor This class is normally a singleton with a private constructor, but private constructors aren't allowed in ActionScript 3.0.

  
      public function @ax-actionscript-lib-logger-Logger () {
        // Show "INFO" level messages by default.
        setLevel(@ax-actionscript-lib-logger-Logger.INFO);   
      }
  
      
Returns a reference to the log instance. If no log instance exists yet, creates one.
returns: A @ax-actionscript-lib-logger-Logger instance.

  
      public static function getLog():@ax-actionscript-lib-logger-Logger {
        if (log == null) {
          log = new @ax-actionscript-lib-logger-Logger();
        }
        return log;  
      }
  
      
Returns a human readable string representing the specified log level.

  
      public static function getLevelDesc(level:Number):String {
        return levelDescriptions[level];
      }
  
      
Sets the message filter level for the log.
parameter: lev The level above which messages are filtered out.

  
      public function setLevel(lev:Number):void {
        // Make sure the supplied level is an integer.
        lev = Math.floor(lev);
        // Set the log level if it's one of the acceptable levels.
        if (lev >= @ax-actionscript-lib-logger-Logger.FATAL && lev <= @ax-actionscript-lib-logger-Logger.DEBUG) {
          logLevel = lev;
          info("Log level set to: " + lev);
          return;
        }
        // If we get this far, the log level isn't valid.
        warn("Invalid log level specified.");
      }
  
      
Returns the message filter level for the log.

  
      public function getLevel():Number {
        return logLevel;
      }
  
      
Returns the most recent message sent to the log.

  
      public function getLastMsg():actionscript_lib_logger_LogMessage {
        return lastMsg;
      }
  
      
Sends a message to the log, with severity "FATAL".

  
      public function fatal(msg:String):void {
        // If the filter level is at least "FATAL", broadcast the message to observers.
        if (logLevel >= @ax-actionscript-lib-logger-Logger.FATAL) {
          // Construct the log message object.
          lastMsg = new actionscript_lib_logger_LogMessage(msg, @ax-actionscript-lib-logger-Logger.FATAL);
  
          // Pass the message on to observers.
          setChanged();
          notifyObservers(lastMsg);
        }
      }
  
      
Sends a message to the log, with severity "ERROR".

  
      public function error(msg:String):void {
        // If the filter level is at least "ERROR", broadcast the message to observers.
        if (logLevel >= @ax-actionscript-lib-logger-Logger.ERROR) {
          lastMsg = new actionscript_lib_logger_LogMessage(msg, @ax-actionscript-lib-logger-Logger.ERROR);
    
          setChanged();
          notifyObservers(lastMsg);
        }
      }
  
      
Sends a message to the log, with severity "WARN".

  
      public function warn(msg:String):void {
        // If the filter level is at least "WARN", broadcast the message to observers.
        if (logLevel >= @ax-actionscript-lib-logger-Logger.WARN) {
          lastMsg = new actionscript_lib_logger_LogMessage(msg, @ax-actionscript-lib-logger-Logger.WARN);
  
          setChanged();
          notifyObservers(lastMsg);
        }
      }
    
      
Sends a message to the log, with severity "INFO".

  
      public function info(msg:String):void {
        // If the filter level is at least "INFO", broadcast the message to observers.
        if (logLevel >= @ax-actionscript-lib-logger-Logger.INFO) {
          lastMsg = new actionscript_lib_logger_LogMessage(msg, @ax-actionscript-lib-logger-Logger.INFO);
  
          setChanged();
          notifyObservers(lastMsg);
        }
      }
  
      
Sends a message to the log, with severity "DEBUG".

  
      public function debug(msg:String):void {
        // If the filter level is at least "DEBUG", broadcast the message to observers.
        if (logLevel >= @ax-actionscript-lib-logger-Logger.DEBUG) {
          lastMsg = new actionscript_lib_logger_LogMessage(msg, @ax-actionscript-lib-logger-Logger.DEBUG);
  
          setChanged();
          notifyObservers(lastMsg);
        }
      }
    }
  }


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.