From 9e011eacdbc44b753abac2cb77998f84412a7d10 Mon Sep 17 00:00:00 2001
From: hche10x <hche10x@6f19259b-4bc3-4df7-8a09-765794883524>
Date: Wed, 31 Jan 2007 08:20:54 +0000
Subject: 1. Fix EDKT508: FW should't build Log.log file in current directory

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@2341 6f19259b-4bc3-4df7-8a09-765794883524
---
 .../frameworkwizard/FrameworkWizardUI.java         |  36 ++++-
 .../org/tianocore/frameworkwizard/common/Log.java  | 152 +++++++++++++--------
 2 files changed, 127 insertions(+), 61 deletions(-)

(limited to 'Tools/Java/Source/FrameworkWizard/src')

diff --git a/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/FrameworkWizardUI.java b/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/FrameworkWizardUI.java
index c3452d7b4c..69d47423a5 100644
--- a/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/FrameworkWizardUI.java
+++ b/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/FrameworkWizardUI.java
@@ -324,12 +324,22 @@ public class FrameworkWizardUI extends IFrame implements KeyListener, MouseListe
      @return FrameworkWizardUI The instance of this class
      
      **/
-    public static FrameworkWizardUI getInstance() {
+    public static FrameworkWizardUI getInstance(String[] args) {
         if (fwui == null) {
-            fwui = new FrameworkWizardUI();
+            fwui = new FrameworkWizardUI(args);
         }
         return fwui;
     }
+    
+    /**
+    If the class hasn't an instnace, new one.
+    
+    @return FrameworkWizardUI The instance of this class
+    
+    **/
+   public static FrameworkWizardUI getInstance() {
+       return fwui;
+   }
 
     /**
      This method initializes jMenuBar 
@@ -1794,8 +1804,11 @@ public class FrameworkWizardUI extends IFrame implements KeyListener, MouseListe
      * Main class, start the GUI
      * 
      */
-    public static void main(String[] args) {
-        FrameworkWizardUI module = FrameworkWizardUI.getInstance();
+    public static void main(String[] args) {      
+        //
+        // Start Main UI
+        //
+        FrameworkWizardUI module = FrameworkWizardUI.getInstance(args);
         module.setVisible(true);
     }
 
@@ -1803,9 +1816,9 @@ public class FrameworkWizardUI extends IFrame implements KeyListener, MouseListe
      This is the default constructor
      
      **/
-    public FrameworkWizardUI() {
+    public FrameworkWizardUI(String[] args) {
         super();
-        init();
+        init(args);
     }
 
     /**
@@ -1813,7 +1826,7 @@ public class FrameworkWizardUI extends IFrame implements KeyListener, MouseListe
      
      
      **/
-    private void init() {
+    private void init(String[] args) {
         //
         // Set current workspace and check
         // Check if exists WORKSPACE
@@ -1826,6 +1839,15 @@ public class FrameworkWizardUI extends IFrame implements KeyListener, MouseListe
         //
         SplashScreen ss = new SplashScreen();
         ss.setVisible(true);
+        
+        //
+        // Go through args to check if enable log
+        //
+        for (int index = 0; index < args.length; index++) {
+            if (args[index].equals("--log") || args[index].equals("-l")) {
+                Log.setSaveLog(true);
+            }
+        }
 
         //
         // Init Global Data
diff --git a/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Log.java b/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Log.java
index cab190946b..49a7a3615b 100644
--- a/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Log.java
+++ b/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Log.java
@@ -23,12 +23,18 @@ import java.io.IOException;
 import javax.swing.JOptionPane;
 
 import org.tianocore.frameworkwizard.FrameworkWizardUI;
+import org.tianocore.frameworkwizard.workspace.Workspace;
 
 /**
  The class is used to provides static interfaces to save log and error information
  
  **/
 public class Log {
+    //
+    //Log file directory path
+    //
+    private static String strLogDir = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR + "Tools"
+                                      + DataType.FILE_SEPARATOR + "Logs";
 
     //
     //Log file
@@ -48,17 +54,22 @@ public class Log {
     //
     //Log file name
     //
-    static String strLogFileName = "Log.log";
+    private static String strLogFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.log";
 
     //
     //Wrn file name
     //
-    static String strWrnFileName = "Wrn.log";
+    private static String strWrnFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.wrn";
 
     //
     //Err file name
     //
-    static String strErrFileName = "Err.log";
+    private static String strErrFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.err";
+
+    //
+    //Flag for create log or not
+    //
+    private static boolean isSaveLog = false;
 
     /**
      Main class, used for test
@@ -88,7 +99,7 @@ public class Log {
     public static void log(String strItem, String strLog) {
         try {
             writeToLogFile(strItem + ":" + strLog);
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -102,7 +113,7 @@ public class Log {
     public static void log(String strLog) {
         try {
             writeToLogFile(strLog);
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -118,7 +129,7 @@ public class Log {
         try {
             writeToWrnFile("Warning when " + strItem + "::" + strWrn);
             showWrnMessage(strWrn);
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -133,7 +144,7 @@ public class Log {
         try {
             writeToWrnFile("Warning::" + strWrn);
             showWrnMessage("Warning::" + strWrn);
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -148,7 +159,7 @@ public class Log {
     public static void err(String strItem, String strErr) {
         try {
             writeToErrFile("Error when " + strItem + "::" + strErr);
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -162,7 +173,7 @@ public class Log {
     public static void err(String strErr) {
         try {
             writeToErrFile("Error::" + strErr);
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
     }
@@ -176,8 +187,8 @@ public class Log {
      **/
     private static void showWrnMessage(String strErr) {
         String strReturn = Tools.wrapStringByWord(strErr);
-        JOptionPane
-                   .showConfirmDialog(FrameworkWizardUI.getInstance(), strReturn, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
+        JOptionPane.showConfirmDialog(FrameworkWizardUI.getInstance(), strReturn, "Warning",
+                                      JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
     }
 
     /**
@@ -187,21 +198,25 @@ public class Log {
      @throws IOException
      
      **/
-    private static void writeToLogFile(String strLog) throws IOException {
-        try {
-            if (fleLogFile == null) {
-                fleLogFile = new File(strLogFileName);
-                fleLogFile.createNewFile();
+    private static void writeToLogFile(String strLog) throws Exception {
+        if (isSaveLog) {
+            try {
+                createLogDir();
+                if (fleLogFile == null) {
+                    fleLogFile = new File(strLogFileName);
+                    fleLogFile.delete();
+                    fleLogFile.createNewFile();
+                }
+                FileOutputStream fos = new FileOutputStream(fleLogFile, true);
+                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
+                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
+                fos.flush();
+                fos.close();
+            } catch (FileNotFoundException e) {
+                e.printStackTrace();
+            } catch (IOException e) {
+                e.printStackTrace();
             }
-            FileOutputStream fos = new FileOutputStream(fleLogFile, true);
-            fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
-            fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
-            fos.flush();
-            fos.close();
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
         }
     }
 
@@ -209,24 +224,28 @@ public class Log {
      Open wrn file and write wrn information
      
      @param strLog The log information
-     @throws IOException
+     * @throws Exception 
      
      **/
-    private static void writeToWrnFile(String strLog) throws IOException {
-        try {
-            if (fleWrnFile == null) {
-                fleWrnFile = new File(strWrnFileName);
-                fleWrnFile.createNewFile();
+    private static void writeToWrnFile(String strLog) throws Exception {
+        if (isSaveLog) {
+            try {
+                createLogDir();
+                if (fleWrnFile == null) {
+                    fleWrnFile = new File(strWrnFileName);
+                    fleWrnFile.delete();
+                    fleWrnFile.createNewFile();
+                }
+                FileOutputStream fos = new FileOutputStream(fleWrnFile, true);
+                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
+                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
+                fos.flush();
+                fos.close();
+            } catch (FileNotFoundException e) {
+                e.printStackTrace();
+            } catch (IOException e) {
+                e.printStackTrace();
             }
-            FileOutputStream fos = new FileOutputStream(fleWrnFile, true);
-            fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
-            fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
-            fos.flush();
-            fos.close();
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
         }
     }
 
@@ -237,21 +256,46 @@ public class Log {
      @throws IOException
      
      **/
-    private static void writeToErrFile(String strLog) throws IOException {
-        try {
-            if (fleErrFile == null) {
-                fleErrFile = new File(strErrFileName);
-                fleErrFile.createNewFile();
+    private static void writeToErrFile(String strLog) throws Exception {
+        if (isSaveLog) {
+            try {
+                createLogDir();
+                if (fleErrFile == null) {
+                    fleErrFile = new File(strErrFileName);
+                    fleErrFile.delete();
+                    fleErrFile.createNewFile();
+                }
+                FileOutputStream fos = new FileOutputStream(fleErrFile, true);
+                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
+                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
+                fos.flush();
+                fos.close();
+            } catch (FileNotFoundException e) {
+                e.printStackTrace();
+            } catch (IOException e) {
+                e.printStackTrace();
             }
-            FileOutputStream fos = new FileOutputStream(fleErrFile, true);
-            fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
-            fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
-            fos.flush();
-            fos.close();
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
         }
     }
+
+    /**
+     Check if directory for Logs exists or not
+     Create the directory if it doesn't exist  
+     * @throws Exception 
+     
+     **/
+    private static void createLogDir() throws Exception {
+        File f = new File(strLogDir);
+        if (!f.exists()) {
+            FileOperation.newFolder(strLogDir);
+        }
+    }
+
+    public static boolean isSaveLog() {
+        return isSaveLog;
+    }
+
+    public static void setSaveLog(boolean isSaveLog) {
+        Log.isSaveLog = isSaveLog;
+    }
 }
-- 
cgit v1.2.3