]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Log.java
1. Fix EDKT508: FW should't build Log.log file in current directory
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / Log.java
index cab190946b2f66359e4f3e921826d8e9be334a54..49a7a3615bc8e14b0804935d41286825176f0ea3 100644 (file)
@@ -23,12 +23,18 @@ import java.io.IOException;
 import javax.swing.JOptionPane;\r
 \r
 import org.tianocore.frameworkwizard.FrameworkWizardUI;\r
+import org.tianocore.frameworkwizard.workspace.Workspace;\r
 \r
 /**\r
  The class is used to provides static interfaces to save log and error information\r
  \r
  **/\r
 public class Log {\r
+    //\r
+    //Log file directory path\r
+    //\r
+    private static String strLogDir = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR + "Tools"\r
+                                      + DataType.FILE_SEPARATOR + "Logs";\r
 \r
     //\r
     //Log file\r
@@ -48,17 +54,22 @@ public class Log {
     //\r
     //Log file name\r
     //\r
-    static String strLogFileName = "Log.log";\r
+    private static String strLogFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.log";\r
 \r
     //\r
     //Wrn file name\r
     //\r
-    static String strWrnFileName = "Wrn.log";\r
+    private static String strWrnFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.wrn";\r
 \r
     //\r
     //Err file name\r
     //\r
-    static String strErrFileName = "Err.log";\r
+    private static String strErrFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.err";\r
+\r
+    //\r
+    //Flag for create log or not\r
+    //\r
+    private static boolean isSaveLog = false;\r
 \r
     /**\r
      Main class, used for test\r
@@ -88,7 +99,7 @@ public class Log {
     public static void log(String strItem, String strLog) {\r
         try {\r
             writeToLogFile(strItem + ":" + strLog);\r
-        } catch (IOException e) {\r
+        } catch (Exception e) {\r
             e.printStackTrace();\r
         }\r
     }\r
@@ -102,7 +113,7 @@ public class Log {
     public static void log(String strLog) {\r
         try {\r
             writeToLogFile(strLog);\r
-        } catch (IOException e) {\r
+        } catch (Exception e) {\r
             e.printStackTrace();\r
         }\r
     }\r
@@ -118,7 +129,7 @@ public class Log {
         try {\r
             writeToWrnFile("Warning when " + strItem + "::" + strWrn);\r
             showWrnMessage(strWrn);\r
-        } catch (IOException e) {\r
+        } catch (Exception e) {\r
             e.printStackTrace();\r
         }\r
     }\r
@@ -133,7 +144,7 @@ public class Log {
         try {\r
             writeToWrnFile("Warning::" + strWrn);\r
             showWrnMessage("Warning::" + strWrn);\r
-        } catch (IOException e) {\r
+        } catch (Exception e) {\r
             e.printStackTrace();\r
         }\r
     }\r
@@ -148,7 +159,7 @@ public class Log {
     public static void err(String strItem, String strErr) {\r
         try {\r
             writeToErrFile("Error when " + strItem + "::" + strErr);\r
-        } catch (IOException e) {\r
+        } catch (Exception e) {\r
             e.printStackTrace();\r
         }\r
     }\r
@@ -162,7 +173,7 @@ public class Log {
     public static void err(String strErr) {\r
         try {\r
             writeToErrFile("Error::" + strErr);\r
-        } catch (IOException e) {\r
+        } catch (Exception e) {\r
             e.printStackTrace();\r
         }\r
     }\r
@@ -176,8 +187,8 @@ public class Log {
      **/\r
     private static void showWrnMessage(String strErr) {\r
         String strReturn = Tools.wrapStringByWord(strErr);\r
-        JOptionPane\r
-                   .showConfirmDialog(FrameworkWizardUI.getInstance(), strReturn, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);\r
+        JOptionPane.showConfirmDialog(FrameworkWizardUI.getInstance(), strReturn, "Warning",\r
+                                      JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);\r
     }\r
 \r
     /**\r
@@ -187,21 +198,25 @@ public class Log {
      @throws IOException\r
      \r
      **/\r
-    private static void writeToLogFile(String strLog) throws IOException {\r
-        try {\r
-            if (fleLogFile == null) {\r
-                fleLogFile = new File(strLogFileName);\r
-                fleLogFile.createNewFile();\r
+    private static void writeToLogFile(String strLog) throws Exception {\r
+        if (isSaveLog) {\r
+            try {\r
+                createLogDir();\r
+                if (fleLogFile == null) {\r
+                    fleLogFile = new File(strLogFileName);\r
+                    fleLogFile.delete();\r
+                    fleLogFile.createNewFile();\r
+                }\r
+                FileOutputStream fos = new FileOutputStream(fleLogFile, true);\r
+                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
+                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
+                fos.flush();\r
+                fos.close();\r
+            } catch (FileNotFoundException e) {\r
+                e.printStackTrace();\r
+            } catch (IOException e) {\r
+                e.printStackTrace();\r
             }\r
-            FileOutputStream fos = new FileOutputStream(fleLogFile, true);\r
-            fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
-            fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
-            fos.flush();\r
-            fos.close();\r
-        } catch (FileNotFoundException e) {\r
-            e.printStackTrace();\r
-        } catch (IOException e) {\r
-            e.printStackTrace();\r
         }\r
     }\r
 \r
@@ -209,24 +224,28 @@ public class Log {
      Open wrn file and write wrn information\r
      \r
      @param strLog The log information\r
-     @throws IOException\r
+     * @throws Exception \r
      \r
      **/\r
-    private static void writeToWrnFile(String strLog) throws IOException {\r
-        try {\r
-            if (fleWrnFile == null) {\r
-                fleWrnFile = new File(strWrnFileName);\r
-                fleWrnFile.createNewFile();\r
+    private static void writeToWrnFile(String strLog) throws Exception {\r
+        if (isSaveLog) {\r
+            try {\r
+                createLogDir();\r
+                if (fleWrnFile == null) {\r
+                    fleWrnFile = new File(strWrnFileName);\r
+                    fleWrnFile.delete();\r
+                    fleWrnFile.createNewFile();\r
+                }\r
+                FileOutputStream fos = new FileOutputStream(fleWrnFile, true);\r
+                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
+                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
+                fos.flush();\r
+                fos.close();\r
+            } catch (FileNotFoundException e) {\r
+                e.printStackTrace();\r
+            } catch (IOException e) {\r
+                e.printStackTrace();\r
             }\r
-            FileOutputStream fos = new FileOutputStream(fleWrnFile, true);\r
-            fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
-            fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
-            fos.flush();\r
-            fos.close();\r
-        } catch (FileNotFoundException e) {\r
-            e.printStackTrace();\r
-        } catch (IOException e) {\r
-            e.printStackTrace();\r
         }\r
     }\r
 \r
@@ -237,21 +256,46 @@ public class Log {
      @throws IOException\r
      \r
      **/\r
-    private static void writeToErrFile(String strLog) throws IOException {\r
-        try {\r
-            if (fleErrFile == null) {\r
-                fleErrFile = new File(strErrFileName);\r
-                fleErrFile.createNewFile();\r
+    private static void writeToErrFile(String strLog) throws Exception {\r
+        if (isSaveLog) {\r
+            try {\r
+                createLogDir();\r
+                if (fleErrFile == null) {\r
+                    fleErrFile = new File(strErrFileName);\r
+                    fleErrFile.delete();\r
+                    fleErrFile.createNewFile();\r
+                }\r
+                FileOutputStream fos = new FileOutputStream(fleErrFile, true);\r
+                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
+                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
+                fos.flush();\r
+                fos.close();\r
+            } catch (FileNotFoundException e) {\r
+                e.printStackTrace();\r
+            } catch (IOException e) {\r
+                e.printStackTrace();\r
             }\r
-            FileOutputStream fos = new FileOutputStream(fleErrFile, true);\r
-            fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
-            fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
-            fos.flush();\r
-            fos.close();\r
-        } catch (FileNotFoundException e) {\r
-            e.printStackTrace();\r
-        } catch (IOException e) {\r
-            e.printStackTrace();\r
         }\r
     }\r
+\r
+    /**\r
+     Check if directory for Logs exists or not\r
+     Create the directory if it doesn't exist  \r
+     * @throws Exception \r
+     \r
+     **/\r
+    private static void createLogDir() throws Exception {\r
+        File f = new File(strLogDir);\r
+        if (!f.exists()) {\r
+            FileOperation.newFolder(strLogDir);\r
+        }\r
+    }\r
+\r
+    public static boolean isSaveLog() {\r
+        return isSaveLog;\r
+    }\r
+\r
+    public static void setSaveLog(boolean isSaveLog) {\r
+        Log.isSaveLog = isSaveLog;\r
+    }\r
 }\r