[izpack-changes] r1550 - izpack-src/trunk/src/lib/com/izforge/izpack/util

noreply at berlios.de noreply at berlios.de
Sun Aug 27 18:47:26 CEST 2006


Author: eppelman
Date: 2006-08-27 18:47:05 +0200 (Sun, 27 Aug 2006)
New Revision: 1550

Modified:
   izpack-src/trunk/src/lib/com/izforge/izpack/util/Debug.java
Log:
Expanded Debug.log()'s possibility to log with Timestamp and or Date()
Default is now with Both; See following Sample Entry
1156690989720; Sun Aug 27 17:03:09 CEST 2006; automationHelperInstance.runAutomated :com.izforge.izpack.panels.ShortcutPanelAutomationHelper successfully done.




Modified: izpack-src/trunk/src/lib/com/izforge/izpack/util/Debug.java
===================================================================
--- izpack-src/trunk/src/lib/com/izforge/izpack/util/Debug.java	2006-08-27 14:10:46 UTC (rev 1549)
+++ izpack-src/trunk/src/lib/com/izforge/izpack/util/Debug.java	2006-08-27 16:47:05 UTC (rev 1550)
@@ -20,432 +20,481 @@
  */
 package com.izforge.izpack.util;
 
-import com.izforge.izpack.installer.Installer;
-
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
-
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.Properties;
 
+import com.izforge.izpack.installer.Installer;
 
 /**
- * This class is for debug purposes. It is highly recommended to use it on critical or
- * experimental code places. To enable the debug mode of IzPack, just start the
- * installer  with the java parameter -DTRACE=true or -DSTACKTRACE=true to enable
- * extendend output of the internal status of critical objects. <br>
+ * This class is for debug purposes. It is highly recommended to use it on critical or experimental
+ * code places. To enable the debug mode of IzPack, just start the installer with the java parameter
+ * -DTRACE=true or -DSTACKTRACE=true to enable extendend output of the internal status of critical
+ * objects. <br>
  * How to use it as IzPack Setup Developer: <br>
  * Just import this class and use one of the methods:
  * 
  * <dl>
- * <dt>
- * Debug.trace( aCriticalObject )
- * </dt>
- * <dd>
- * - to print the status on console
- * </dd>
- * <dt>
- * Debug.error( aCriticalObject )
- * </dt>
- * <dd>
- * - to print the status on console and<br>
- * print the stacktrace of a supressed Exception.
- * </dd>
- * <dt>
- * Additionally:
- * </dt>
- * <dd>
- * if -DLOG is given the output will be written in the File see #LOGFILENAME in the users
- * Home directory.
- * </dd>
+ * <dt> Debug.trace( aCriticalObject ) </dt>
+ * <dd> - to print the status on console </dd>
+ * <dt> Debug.error( aCriticalObject ) </dt>
+ * <dd> - to print the status on console and<br>
+ * print the stacktrace of a supressed Exception. </dd>
+ * <dt> Additionally: </dt>
+ * <dd> if -DLOG is given the output will be written in the File see #LOGFILENAME in the users Home
+ * directory. </dd>
  * </dl>
  * 
- *
+ * 
  * @author Julien Ponge, Klaus Bartz, Marc Eppelmann
  * @version $Revision$ ($Id$)
  */
 public class Debug
 {
-  //~ Static fields/initializers *********************************************************
 
-  /**
-   * Parameter for public javacall "java -jar izpack.jar -DLOG" (Class.internal.variable:
-   * (DLOG = "LOG"))
-   */
-  public static final String DLOG = "LOG";
+    // ~ Static fields/initializers *********************************************************
 
-  /**
-   * Parameter for public javacall "java -jar izpack.jar -DSTACKTRACE"
-   * (Class.internal.variable: (DSTACKTRACE = "STACKTRACE"))
-   */
-  public static final String DSTACKTRACE = "STACKTRACE";
+    /**
+     * Parameter for public javacall "java -jar izpack.jar -DLOG" (Class.internal.variable: (DLOG =
+     * "LOG"))
+     */
+    public static final String DLOG = "LOG";
 
-  /**
-   * Parameter for public javacall "java -jar izpack.jar -DTRACE"
-   * (Class.internal.variable: (DTRACE = "TRACE"))
-   */
-  public static final String DTRACE = "TRACE";
+    /**
+     * Parameter for public javacall "java -jar izpack.jar -DSTACKTRACE" (Class.internal.variable:
+     * (DSTACKTRACE = "STACKTRACE"))
+     */
+    public static final String DSTACKTRACE = "STACKTRACE";
 
-  /** System.Property Key: IZPACK_LOGFILE = "izpack.logfile" */
-  public static final String IZPACK_LOGFILE = "izpack.logfile";
+    /**
+     * Parameter for public javacall "java -jar izpack.jar -DTRACE" (Class.internal.variable:
+     * (DTRACE = "TRACE"))
+     */
+    public static final String DTRACE = "TRACE";
 
-  /** internally initial unintialized TRACE-flag */
-  private static boolean TRACE;
+    /** System.Property Key: IZPACK_LOGFILE = "izpack.logfile" */
+    public static final String IZPACK_LOGFILE = "izpack.logfile";
 
-  /** internal initial unintialized STACKTRACE-flag */
-  private static boolean STACKTRACE;
+    /** WITHOUT_DATE = 0 */
+    public static final int LOG_WITHOUT_DATE = 0;
 
-  /** internal initial unintialized LOG-flag */
-  private static boolean LOG;
+    /** WITH_DATE = 1 */
+    public static final int LOG_WITH_DATE = 1;
 
-  /** LOGFILE_PREFIX = "IzPack_Logfile_at_" */
-  public static String LOGFILE_PREFIX = "IzPack_Logfile_at_";
+    /** WITH_TIME_STAMP = 2 */
+    public static final int LOG_WITH_TIME_STAMP = 2;
 
-  /** LOGFILE_EXTENSION = ".txt" */
-  public static String LOGFILE_EXTENSION = ".txt";
+    /** LOG_WITH_DATE | LOG_WITH_TIME_STAMP = 3 */
+    public static final int LOG_WITH_TIME_AND_DATE = LOG_WITH_DATE | LOG_WITH_TIME_STAMP;
 
-  /** LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis() + LOGFILE_EXTENSION */
-  public static String LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis(  ) +
-                                     LOGFILE_EXTENSION;
+    /** internally initial unintialized TRACE-flag */
+    private static boolean TRACE;
 
+    /** internal initial unintialized STACKTRACE-flag */
+    private static boolean STACKTRACE;
+
+    /** internal initial unintialized LOG-flag */
+    private static boolean LOG;
+
+    /** LOGFILE_PREFIX = "IzPack_Logfile_at_" */
+    public static String LOGFILE_PREFIX = "IzPack_Logfile_at_";
+
+    /** LOGFILE_EXTENSION = ".txt" */
+    public static String LOGFILE_EXTENSION = ".txt";
+
+    /** LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis() + LOGFILE_EXTENSION */
+    public static String LOGFILENAME = LOGFILE_PREFIX + System.currentTimeMillis()
+            + LOGFILE_EXTENSION;
+
     /**
      * The log initializion bloc.
      */
-  static
-  {
-    boolean st = false;
-
-    try
+    static
     {
-      st = Boolean.getBoolean( DSTACKTRACE );
-    }
-    catch( Exception ex )
-    {
-      // ignore
-    }
+        boolean st = false;
 
-    STACKTRACE = st;
+        try
+        {
+            st = Boolean.getBoolean(DSTACKTRACE);
+        }
+        catch (Exception ex)
+        {
+            // ignore
+        }
 
-    boolean log = false;
+        STACKTRACE = st;
 
-    try
-    {
-      log = Boolean.getBoolean( DLOG );
-    }
-    catch( Exception ex )
-    {
-      // ignore
-    }
+        boolean log = false;
 
-    LOG = log;
+        try
+        {
+            log = Boolean.getBoolean(DLOG);
+        }
+        catch (Exception ex)
+        {
+            // ignore
+        }
 
-    boolean t = false;
+        LOG = log;
 
-    try
-    {
-      if( STACKTRACE )
-      {
-        t = true;
-      }
-      else
-      {
-        t = Boolean.getBoolean( DTRACE );
-      }
-    }
-    catch( Exception ex )
-    {
-      // ignore
-    }
+        boolean t = false;
 
-    TRACE = t;
+        try
+        {
+            if (STACKTRACE)
+            {
+                t = true;
+            }
+            else
+            {
+                t = Boolean.getBoolean(DTRACE);
+            }
+        }
+        catch (Exception ex)
+        {
+            // ignore
+        }
 
-    if( LOG )
-    {
-      System.out.println( DLOG + " enabled." );
-      PrintWriter logfile = createLogFile();
+        TRACE = t;
 
-      Debug.log( Installer.class.getName(  ) + " LogFile created at " +
-                 new Date( System.currentTimeMillis(  ) ) );
+        if (LOG)
+        {
+            System.out.println(DLOG + " enabled.");
+            PrintWriter logfile = createLogFile();
 
-      //** write some runtime system properties into the logfile **
-      Debug.log( "System.Properties:" );
+            Debug.log(Installer.class.getName() + " LogFile created at ");
 
-      Properties  sysProps = System.getProperties(  );
+            // ** write some runtime system properties into the logfile **
+            Debug.log("System.Properties:", LOG_WITH_TIME_STAMP);
 
-      Enumeration spe = sysProps.keys(  );
+            Properties sysProps = System.getProperties();
 
-      while( spe.hasMoreElements(  ) )
-      {
-        String aKey = (String) spe.nextElement(  );
-        Debug.log( aKey + "  =  " + sysProps.getProperty( aKey ) );
-      }
-      Debug.log( "\n==========================================\n" );
-      Debug.log( "\n " + Installer.class.getName(  )+ " installs on: \n" );
-      Debug.log( OsVersion.getOsDetails() );
-      Debug.log( "\n==========================================\n" );
-    }
+            Enumeration spe = sysProps.keys();
 
-    if( TRACE )
-    {
-      System.out.println( DTRACE + " enabled." );
-    }
+            while (spe.hasMoreElements())
+            {
+                String aKey = (String) spe.nextElement();
+                Debug.log(aKey + "  =  " + sysProps.getProperty(aKey), LOG_WITHOUT_DATE);
+            }
+            Debug.log("\n==========================================\n", LOG_WITHOUT_DATE);
+            Debug.log("\n " + Installer.class.getName() + " installs on: \n", LOG_WITHOUT_DATE);
+            Debug.log(OsVersion.getOsDetails(), LOG_WITHOUT_DATE);
+            Debug.log("\n==========================================\n", LOG_WITHOUT_DATE);
+        }
 
-    if( STACKTRACE )
-    {
-      System.out.println( DSTACKTRACE + " enabled." );
+        if (TRACE)
+        {
+            System.out.println(DTRACE + " enabled.");
+        }
+
+        if (STACKTRACE)
+        {
+            System.out.println(DSTACKTRACE + " enabled.");
+        }
     }
-  }
 
-  //~ Methods ****************************************************************************
+    // ~ Methods ****************************************************************************
 
-  /** 
-   * Traces the internal status of the given Object
-   *
-   * @param s
-   */
-  public static void trace( Object s )
-  {
-    if( TRACE )
+    /**
+     * Traces the internal status of the given Object
+     * 
+     * @param s
+     */
+    public static void trace(Object s)
     {
-      // console.println(s.toString());
-      System.out.println( s );
+        if (TRACE)
+        {
+            // console.println(s.toString());
+            System.out.println(s);
 
-      if( STACKTRACE && ( s instanceof Throwable ) )
-      {
-        // StringWriter sw = new StringWriter();
-        // PrintWriter pw = new PrintWriter(sw);
-        // ((Throwable)s).printStackTrace(pw);
-        // console.println(sw.toString());
-        ( (Throwable) s ).printStackTrace(  );
-      }
+            if (STACKTRACE && (s instanceof Throwable))
+            {
+                // StringWriter sw = new StringWriter();
+                // PrintWriter pw = new PrintWriter(sw);
+                // ((Throwable)s).printStackTrace(pw);
+                // console.println(sw.toString());
+                ((Throwable) s).printStackTrace();
+            }
 
-      System.out.flush(  );
+            System.out.flush();
+        }
     }
-  }
 
-  /** 
-   * Traces the given object and additional write their status in the LOGFILE.
-   *
-   * @param s
-   */
-  public static void error( Object s )
-  {
-    trace( s );
-    System.err.println( s );
-    System.err.flush(  );
-    log( s );
-  }
+    /**
+     * Traces the given object and additional write their status in the LOGFILE.
+     * 
+     * @param s
+     */
+    public static void error(Object s)
+    {
+        trace(s);
+        System.err.println(s);
+        System.err.flush();
+        log(s);
+    }
 
-  /** 
-   * Logs the given Object in the created Logfile if -DLOG=true was given on commandline
-   * i.e: java -DLOG=true -jar izpack-installer.jar
-   *
-   * @param o The Object to log
-   */
-  public static void log( Object o )
-  {
-    //if LOG was given 
-    if(LOG )
+    /**
+     * Logs the given Object in the created Logfile if -DLOG=true was given on commandline i.e: java
+     * -DLOG=true -jar izpack-installer.jar
+     * 
+     * @param o The Object to log, can be also an exception.
+     */
+    public static void log(Object o)
     {
-        PrintWriter logfile;
-        if( ( logfile = getLogFile(  ) ) == null )
-        {
-          logfile = createLogFile(  );
-        }
+        log(o, LOG_WITH_TIME_AND_DATE);
+    }
 
-      if( logfile != null )
-      {
-        if( o == null )
+    /**
+     * Logs the given Object in the created Logfile if -DLOG=true was given on commandline i.e: java
+     * -DLOG=true -jar izpack-installer.jar
+     * 
+     * @param o The Object to log
+     * @param withDate if the given MASK is greater than 0, Log with Date/Timestamp
+     */
+    public static void log(Object o, int withWhatFormat)
+    {
+        // if LOG was given
+        if (LOG)
         {
-          o = "null";
-        }
+            PrintWriter logfile;
+            if ((logfile = getLogFile()) == null)
+            {
+                logfile = createLogFile();
+            }
 
-        logfile.println( o );
+            if (logfile != null)
+            {
+                if (o == null)
+                {
+                    o = "null";
+                }
 
-        if( o instanceof Throwable )
-        {
-          ( (Throwable) o ).printStackTrace( logfile );
-        }
+                StringBuffer entry = new StringBuffer();
+                if (logWithTimeStamp(withWhatFormat))
+                {
+                    entry.append(System.currentTimeMillis());
+                    entry.append(';');
+                    entry.append(' ');
+                }
+                if (logWithDate(withWhatFormat))
+                {
+                    entry.append(new Date());
+                    entry.append(';');
+                    entry.append(' ');
+                }
 
-        logfile.flush(  );
+                entry.append(o);
 
-        //logfile.close();
-        //logFile = null;
-      }
-      else
-      {
-        System.err.println( "Cannot write into logfile: (" + logfile + ") <- '" + o +
-                            "'" );
-      }
-    }
-  }
+                logfile.println(entry.toString());
 
-  /** 
-   * Creates the logfile to write log-infos into.
-   *
-   * @return The writer object instance
-   */
-  private static PrintWriter createLogFile(  )
-  {
-    String tempDir = System.getProperty( "java.io.tmpdir" );
+                if (o instanceof Throwable)
+                {
+                    ((Throwable) o).printStackTrace(logfile);
+                }
 
-    File   tempDirFile = new File( tempDir );
+                logfile.flush();
 
-    try
+                // logfile.close();
+                // logFile = null;
+            }
+            else
+            {
+                System.err.println("Cannot write into logfile: (" + logfile + ") <- '" + o + "'");
+            }
+        }
+    }
+
+    /**
+     * Indicates that to log with Date.
+     * 
+     * @param withWhat The whished Format
+     * @return true if to log with Date
+     */
+    private static boolean logWithDate(int withWhatFormat)
     {
-      tempDirFile.mkdirs(  );
+        // TODO Auto-generated method stub
+        return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE;
     }
-    catch( RuntimeException e1 )
+
+    /**
+     * Indicates that to log with Timestamp.
+     * 
+     * @param withWhat The whished Format
+     * @return true if to log with Timestamp
+     */
+    private static boolean logWithTimeStamp(int withWhatFormat)
     {
-      e1.printStackTrace(  );
+
+        return (withWhatFormat & LOG_WITH_DATE) == LOG_WITH_DATE;
     }
 
-    String logfilename = LOGFILENAME;
-    System.out.println( "creating Logfile: '" + logfilename + "' in: '" + tempDir + "'" );
+    /**
+     * Creates the logfile to write log-infos into.
+     * 
+     * @return The writer object instance
+     */
+    private static PrintWriter createLogFile()
+    {
+        String tempDir = System.getProperty("java.io.tmpdir");
 
-    File out = new File( tempDir, logfilename );
+        File tempDirFile = new File(tempDir);
 
-      PrintWriter logfile;
-      if( tempDirFile.canWrite(  ) )
-      {
         try
         {
-            BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out),
-                    "UTF-8"));
-          logfile = setLogFile( new PrintWriter( fw ) );
+            tempDirFile.mkdirs();
         }
-        catch( Exception e )
+        catch (RuntimeException e1)
         {
-          logfile = null;
-          e.printStackTrace(  );
+            e1.printStackTrace();
         }
-      }
-      else
-      {
-        logfile = null;
-        System.err.println( "Fatal: cannot write File: '" + logfilename + "' into: " +
-                            tempDirFile );
-      }
 
-    return logfile;
-  }
+        String logfilename = LOGFILENAME;
+        System.out.println("creating Logfile: '" + logfilename + "' in: '" + tempDir + "'");
 
-  /** 
-   * Indicates if debug is tracing
-   *
-   * @return true if tracing otherwise false
-   */
-  public static boolean tracing(  )
-  {
-    return TRACE;
-  }
+        File out = new File(tempDir, logfilename);
 
-  /** 
-   * Indicates if debug is stacktracing
-   *
-   * @return true if stacktracing otherwise false
-   */
-  public static boolean stackTracing(  )
-  {
-    return STACKTRACE;
-  }
+        PrintWriter logfile;
+        if (tempDirFile.canWrite())
+        {
+            try
+            {
+                BufferedWriter fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
+                        out), "UTF-8"));
+                logfile = setLogFile(new PrintWriter(fw));
+            }
+            catch (Exception e)
+            {
+                logfile = null;
+                e.printStackTrace();
+            }
+        }
+        else
+        {
+            logfile = null;
+            System.err.println("Fatal: cannot write File: '" + logfilename + "' into: "
+                    + tempDirFile);
+        }
 
-  /** 
-   * Returns the LOG flag.
-   *
-   * @return Returns the LOG flag.
-   */
-  public static boolean isLOG(  )
-  {
-    return LOG;
-  }
+        return logfile;
+    }
 
-  /** 
-   * Sets The LOG like the given value
-   *
-   * @param aFlag The LOG status to set to or not.
-   */
-  public static void setLOG( boolean aFlag )
-  {
-    System.out.println( DLOG + " = " + aFlag );
-    LOG = aFlag;
-  }
+    /**
+     * Indicates if debug is tracing
+     * 
+     * @return true if tracing otherwise false
+     */
+    public static boolean tracing()
+    {
+        return TRACE;
+    }
 
-  /** 
-   * Returns the current STACKTRACE flag
-   *
-   * @return Returns the STACKTRACE.
-   */
-  public static boolean isSTACKTRACE(  )
-  {
-    return STACKTRACE;
-  }
+    /**
+     * Indicates if debug is stacktracing
+     * 
+     * @return true if stacktracing otherwise false
+     */
+    public static boolean stackTracing()
+    {
+        return STACKTRACE;
+    }
 
-  /** 
-   * Sets the STACKTRACE like the given value
-   *
-   * @param aFlag The STACKTRACE to set / unset.
-   */
-  public static void setSTACKTRACE( boolean aFlag )
-  {
-    System.out.println( DSTACKTRACE + " = " + aFlag );
-    STACKTRACE = aFlag;
-  }
+    /**
+     * Returns the LOG flag.
+     * 
+     * @return Returns the LOG flag.
+     */
+    public static boolean isLOG()
+    {
+        return LOG;
+    }
 
-  /** 
-   * Gets the current TRACE flag
-   *
-   * @return Returns the TRACE.
-   */
-  public static boolean isTRACE(  )
-  {
-    return TRACE;
-  }
+    /**
+     * Sets The LOG like the given value
+     * 
+     * @param aFlag The LOG status to set to or not.
+     */
+    public static void setLOG(boolean aFlag)
+    {
+        System.out.println(DLOG + " = " + aFlag);
+        LOG = aFlag;
+    }
 
-  /** 
-   * Sets the TRACE flag like the given value
-   *
-   * @param aFlag The TRACE to set / unset.
-   */
-  public static void setTRACE( boolean aFlag )
-  {
-    System.out.println( DTRACE + " = " + aFlag );
-    TRACE = aFlag;
-  }
+    /**
+     * Returns the current STACKTRACE flag
+     * 
+     * @return Returns the STACKTRACE.
+     */
+    public static boolean isSTACKTRACE()
+    {
+        return STACKTRACE;
+    }
 
-  /** 
-   * Get the Logfile
-   *
-   * @return Returns the logFile.
-   */
-  public static PrintWriter getLogFile(  )
-  {
-    PrintWriter logfile = (PrintWriter) System.getProperties().get(IZPACK_LOGFILE);
+    /**
+     * Sets the STACKTRACE like the given value
+     * 
+     * @param aFlag The STACKTRACE to set / unset.
+     */
+    public static void setSTACKTRACE(boolean aFlag)
+    {
+        System.out.println(DSTACKTRACE + " = " + aFlag);
+        STACKTRACE = aFlag;
+    }
 
-    return logfile;
-  }
+    /**
+     * Gets the current TRACE flag
+     * 
+     * @return Returns the TRACE.
+     */
+    public static boolean isTRACE()
+    {
+        return TRACE;
+    }
 
-  /** 
-   * Sets the Logfile
-   *
-   * @param aLogFile The logFile to set.   * 
-   * @return The logfile to write into
-   */
-  public static synchronized PrintWriter setLogFile( PrintWriter aLogFile )
-  {
-    System.getProperties(  ).put( IZPACK_LOGFILE, aLogFile );
+    /**
+     * Sets the TRACE flag like the given value
+     * 
+     * @param aFlag The TRACE to set / unset.
+     */
+    public static void setTRACE(boolean aFlag)
+    {
+        System.out.println(DTRACE + " = " + aFlag);
+        TRACE = aFlag;
+    }
 
-    PrintWriter logfile = (PrintWriter) System.getProperties().get(IZPACK_LOGFILE);
+    /**
+     * Get the Logfile
+     * 
+     * @return Returns the logFile.
+     */
+    public static PrintWriter getLogFile()
+    {
+        PrintWriter logfile = (PrintWriter) System.getProperties().get(IZPACK_LOGFILE);
 
-    if( logfile == null )
+        return logfile;
+    }
+
+    /**
+     * Sets the Logfile
+     * 
+     * @param aLogFile The logFile to set. *
+     * @return The logfile to write into
+     */
+    public static synchronized PrintWriter setLogFile(PrintWriter aLogFile)
     {
-      System.err.println( "Set::logfile == null" );
+        System.getProperties().put(IZPACK_LOGFILE, aLogFile);
+
+        PrintWriter logfile = (PrintWriter) System.getProperties().get(IZPACK_LOGFILE);
+
+        if (logfile == null)
+        {
+            System.err.println("Set::logfile == null");
+        }
+
+        return logfile;
     }
-
-    return logfile;
-  }
 }




More information about the izpack-changes mailing list