File Source: Logging.java

         /* 
    P/P   *  Method: com.dmdirc.parser.irc.Logging__static_init
          */
     1  /*
     2   * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
     3   *
     4   * Permission is hereby granted, free of charge, to any person obtaining a copy
     5   * of this software and associated documentation files (the "Software"), to deal
     6   * in the Software without restriction, including without limitation the rights
     7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   * copies of the Software, and to permit persons to whom the Software is
     9   * furnished to do so, subject to the following conditions:
    10   *
    11   * The above copyright notice and this permission notice shall be included in
    12   * all copies or substantial portions of the Software.
    13   *
    14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   * SOFTWARE.
    21   */
    22  
    23  package com.dmdirc.parser.irc;
    24  
    25  import java.lang.reflect.Method;
    26  import java.lang.reflect.InvocationTargetException;
    27  
    28  /**
    29   * Logging using log4j if available.
    30   */
    31  public class Logging {
    32  	/** Available Log Levels */
        	 /* 
    P/P 	  *  Method: Logging$LogLevel valueOf(String)
        	  * 
        	  *  Postconditions:
        	  *    init'ed(return_value)
        	  */
    33  	public enum LogLevel {
        		 /* 
    P/P 		  *  Method: com.dmdirc.parser.irc.Logging$LogLevel__static_init
        		  * 
        		  *  Postconditions:
        		  *    $VALUES == &new Logging$LogLevel[](Logging$LogLevel__static_init#7)
        		  *    DEBUG == &new Logging$LogLevel(Logging$LogLevel__static_init#2)
        		  *    $VALUES[1] == &new Logging$LogLevel(Logging$LogLevel__static_init#2)
        		  *    ERROR == &new Logging$LogLevel(Logging$LogLevel__static_init#5)
        		  *    $VALUES[4] == &new Logging$LogLevel(Logging$LogLevel__static_init#5)
        		  *    FATAL == &new Logging$LogLevel(Logging$LogLevel__static_init#6)
        		  *    $VALUES[5] == &new Logging$LogLevel(Logging$LogLevel__static_init#6)
        		  *    INFO == &new Logging$LogLevel(Logging$LogLevel__static_init#3)
        		  *    $VALUES[2] == &new Logging$LogLevel(Logging$LogLevel__static_init#3)
        		  *    TRACE == &new Logging$LogLevel(Logging$LogLevel__static_init#1)
        		  *    ...
        		  */
    34  		TRACE("trace", "isTraceEnabled"),
    35  		DEBUG("debug", "isDebugEnabled"),
    36  		INFO("info", "isInfoEnabled"),
    37  		WARN("warn", "isWarnEnabled"),
    38  		ERROR("error", "isErrorEnabled"),
    39  		FATAL("fatal", "isFatalEnabled");
    40  		
    41  		/** Method name */
    42  		private final String methodName;
    43  		
    44  		/** Check Method name */
    45  		private final String checkMethodName;
    46  		
    47  		/**
    48  		 * Create a new LogLevel
    49  		 *
    50  		 * @param methodName Name of method in log4j to log to
    51  		 * @param checkMethodName Name of method in log4j to sue to check logging
    52  		 */
        		 /* 
    P/P 		  *  Method: void com.dmdirc.parser.irc.Logging$LogLevel(String, int, String, String)
        		  * 
        		  *  Postconditions:
        		  *    this.checkMethodName == checkMethodName
        		  *    init'ed(this.checkMethodName)
        		  *    this.methodName == methodName
        		  *    init'ed(this.methodName)
        		  */
    53  		private LogLevel(final String methodName, final String checkMethodName) {
    54  			this.methodName = methodName;
    55  			this.checkMethodName = checkMethodName;
    56  		}
    57  		
    58  		/**
    59  		 * Get the Name of method in log4j to log to
    60  		 *
    61  		 * @return Name of method in log4j to log to
    62  		 */
        		 /* 
    P/P 		  *  Method: String getMethodName()
        		  * 
        		  *  Postconditions:
        		  *    return_value == this.methodName
        		  *    init'ed(return_value)
        		  */
    63  		public String getMethodName() { return methodName; }
    64  		
    65  		/**
    66  		 * Get the Name of the check method in log4j
    67  		 *
    68  		 * @return Name of check method in log4j
    69  		 */
        		 /* 
    P/P 		  *  Method: String getCheckMethodName()
        		  * 
        		  *  Postconditions:
        		  *    return_value == this.checkMethodName
        		  *    init'ed(return_value)
        		  */
    70  		public String getCheckMethodName() { return checkMethodName; }
    71  	};
    72  	
    73  	/** Singleton Instance of Logging. */
    74  	private static Logging me;
    75  	
    76  	/** Is log4j available. */
    77  	private final boolean isAvailable;
    78  	
    79  	/** "Log" object if available. */
    80  	private Object log = null;
    81  	
    82  	/**
    83  	 * Get an instance of Logging.
    84  	 *
    85  	 * @return The instance of Logging
    86  	 */
    87  	public static Logging getLogging() {
        		 /* 
    P/P 		  *  Method: Logging getLogging()
        		  * 
        		  *  Preconditions:
        		  *    init'ed(me)
        		  * 
        		  *  Postconditions:
        		  *    me == One-of{old me, &new Logging(getLogging#1)}
        		  *    me != null
        		  *    return_value == me
        		  *    new Logging(getLogging#1) num objects <= 1
        		  *    init'ed(new Logging(getLogging#1).isAvailable)
        		  *    init'ed(new Logging(getLogging#1).log)
        		  * 
        		  *  Test Vectors:
        		  *    me: Inverse{null}, Addr_Set{null}
        		  */
    88  		if (me == null) { me = new Logging(); }
    89  		return me;
    90  	}
    91  	
    92  	/** Create a new Logging. */
    93  	@SuppressWarnings("unchecked")
        	 /* 
    P/P 	  *  Method: void com.dmdirc.parser.irc.Logging()
        	  * 
        	  *  Presumptions:
        	  *    java.lang.Class:getMethod(...)@104 != null
        	  * 
        	  *  Postconditions:
        	  *    init'ed(this.isAvailable)
        	  *    init'ed(this.log)
        	  * 
        	  *  Test Vectors:
        	  *    java.lang.Class:forName(...)@100: Addr_Set{null}, Inverse{null}
        	  */
    94  	private Logging() {
    95  		boolean classExists = false;
    96  		try {
    97  			Class factory = null;
    98  			// Check for classes
    99  			Class.forName("org.apache.commons.logging.Log");
   100  			factory = Class.forName("org.apache.commons.logging.LogFactory");
   101  		
   102  			classExists = (factory != null);
   103  			if (classExists) {
   104  				final Method getLog = factory.getMethod("getLog", new Class[]{Class.class});
   105  				log = getLog.invoke(null, this.getClass());
   106  			}
   107  		} catch (ClassNotFoundException cnfe) {
   108  		} catch (NoSuchMethodException nsme) {
   109  		} catch (IllegalAccessException iae) {
   110  		} catch (InvocationTargetException ite) {
   111  		}
   112  		
   113  		isAvailable = (classExists && log != null);
   114  	}
   115  	
   116  	/**
   117  	 * Check is a log level is available.
   118  	 *
   119  	 * @param level Level to check
   120  	 */
   121  	public boolean levelEnabled(final LogLevel level) {
        		 /* 
    P/P 		  *  Method: bool levelEnabled(Logging$LogLevel)
        		  * 
        		  *  Preconditions:
        		  *    (soft) level != null
        		  *    (soft) this.log != null
        		  * 
        		  *  Presumptions:
        		  *    java.lang.Class:getMethod(...)@124 != null
        		  *    java.lang.Object:getClass(...)@124 != null
        		  *    java.lang.reflect.Method:invoke(...)@125 != null
        		  * 
        		  *  Postconditions:
        		  *    init'ed(return_value)
        		  * 
        		  *  Test Vectors:
        		  *    this.isAvailable: {0}, {1}
        		  */
   122  		if (isAvailable) {
   123  			try {
   124  				final Method check = log.getClass().getMethod(level.getCheckMethodName(), new Class[0]);
   125  				return (Boolean)check.invoke(log, new Object[0]);
   126  			} catch (NoSuchMethodException nsme) {
   127  			} catch (IllegalAccessException iae) {
   128  			} catch (InvocationTargetException ite) {
   129  			}
   130  		}
   131  		
   132  		return false;
   133  	}
   134  	
   135  	/**
   136  	 * Log a message if log4j is available.
   137  	 *
   138  	 * @param level Level to log at
   139  	 * @param message Message to log
   140  	 */
   141  	public void log(final LogLevel level, final String message) {
        		 /* 
    P/P 		  *  Method: void log(Logging$LogLevel, String)
        		  * 
        		  *  Preconditions:
        		  *    (soft) level != null
        		  *    (soft) this.log != null
        		  */
   142  		log(level, message, null);
   143  	}
   144  	
   145  	/**
   146  	 * Log a message if log4j is available.
   147  	 *
   148  	 * @param level Level to log at
   149  	 * @param message Message to log
   150  	 * @param throwable Throwable to log alongside message
   151  	 */
   152  	public void log(final LogLevel level, final String message, final Throwable throwable) {
        		 /* 
    P/P 		  *  Method: void log(Logging$LogLevel, String, Throwable)
        		  * 
        		  *  Preconditions:
        		  *    (soft) level != null
        		  *    (soft) this.log != null
        		  * 
        		  *  Presumptions:
        		  *    java.lang.Class:getMethod(...)@156 != null
        		  *    java.lang.Class:getMethod(...)@159 != null
        		  *    java.lang.Object:getClass(...)@156 != null
        		  *    java.lang.Object:getClass(...)@159 != null
        		  * 
        		  *  Test Vectors:
        		  *    this.isAvailable: {1}, {0}
        		  *    throwable: Inverse{null}, Addr_Set{null}
        		  */
   153  		if (!isAvailable) { return; }
   154  		try {
   155  			if (throwable == null) {
   156  				final Method method = log.getClass().getMethod(level.getMethodName(), new Class[]{String.class});
   157  				method.invoke(log, new Object[]{message});
   158  			} else {
   159  				final Method method = log.getClass().getMethod(level.getMethodName(), new Class[]{String.class, Throwable.class});
   160  				method.invoke(log, new Object[]{message, throwable});
   161  			}
   162  		} catch (NoSuchMethodException nsme) {
   163  		} catch (IllegalAccessException iae) {
   164  		} catch (InvocationTargetException ite) {
   165  		}
   166  	}
   167  }








SofCheck Inspector Build Version : 2.17854
Logging.java 2009-Jun-25 01:54:24
Logging.class 2009-Sep-02 17:04:16
Logging$LogLevel.class 2009-Sep-02 17:04:16