]> git.proxmox.com Git - mirror_edk2.git/blame - 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
CommitLineData
a13899c5 1/** @file\r
2 \r
3 The file is used to provides static interfaces to save log and error information \r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15\r
16package org.tianocore.frameworkwizard.common;\r
17\r
18import java.io.File;\r
19import java.io.FileNotFoundException;\r
20import java.io.FileOutputStream;\r
21import java.io.IOException;\r
22\r
23import javax.swing.JOptionPane;\r
24\r
82484861 25import org.tianocore.frameworkwizard.FrameworkWizardUI;\r
9e011eac 26import org.tianocore.frameworkwizard.workspace.Workspace;\r
82484861 27\r
a13899c5 28/**\r
29 The class is used to provides static interfaces to save log and error information\r
30 \r
31 **/\r
32public class Log {\r
9e011eac 33 //\r
34 //Log file directory path\r
35 //\r
36 private static String strLogDir = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR + "Tools"\r
37 + DataType.FILE_SEPARATOR + "Logs";\r
a13899c5 38\r
39 //\r
40 //Log file\r
41 //\r
42 private static File fleLogFile = null;\r
43\r
ed1665f2 44 //\r
45 //Wrn file\r
46 //\r
47 private static File fleWrnFile = null;\r
48\r
a13899c5 49 //\r
50 //Err file\r
51 //\r
52 private static File fleErrFile = null;\r
53\r
54 //\r
55 //Log file name\r
56 //\r
9e011eac 57 private static String strLogFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.log";\r
a13899c5 58\r
ed1665f2 59 //\r
60 //Wrn file name\r
61 //\r
9e011eac 62 private static String strWrnFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.wrn";\r
ed1665f2 63\r
a13899c5 64 //\r
65 //Err file name\r
66 //\r
9e011eac 67 private static String strErrFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.err";\r
68\r
69 //\r
70 //Flag for create log or not\r
71 //\r
72 private static boolean isSaveLog = false;\r
a13899c5 73\r
74 /**\r
75 Main class, used for test\r
76 \r
77 @param args\r
78 \r
79 **/\r
80 public static void main(String[] args) {\r
81 try {\r
ed1665f2 82 //Log.log("Test", "test");\r
83 //Log.err("Test1", "test1");\r
ed1665f2 84 Log\r
d933fc5f 85 .wrn("aaa bbbbbb cccccccccccc ddddddddddd eeeeeeeeee fffffffffff gggggggggggggggggg hhhhhhhhhhhhhhhhhhhhhhhhhhhhh iiiii jjjj kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk lll mmm nn poooooooooooooooooooooooooooooooooooooooooooop");\r
86 Log.wrn("Incorrect data type for ModuleEntryPoint");\r
a13899c5 87 } catch (Exception e) {\r
88 e.printStackTrace();\r
89 }\r
90 }\r
91\r
a13899c5 92 /**\r
93 Call writeToLogFile to save log item and log information to log file\r
94 \r
95 @param strItem The log item\r
96 @param strLog The log information\r
97 \r
98 **/\r
99 public static void log(String strItem, String strLog) {\r
100 try {\r
101 writeToLogFile(strItem + ":" + strLog);\r
9e011eac 102 } catch (Exception e) {\r
a13899c5 103 e.printStackTrace();\r
104 }\r
105 }\r
106\r
107 /**\r
108 Call writeToLogFile to save log information to log file\r
109 \r
110 @param strLog The log information\r
111 \r
112 **/\r
113 public static void log(String strLog) {\r
114 try {\r
115 writeToLogFile(strLog);\r
9e011eac 116 } catch (Exception e) {\r
a13899c5 117 e.printStackTrace();\r
118 }\r
119 }\r
120\r
ed1665f2 121 /**\r
122 Call writeToWrnFile to save wrn item and wrn information to wrn file\r
123 \r
124 @param strItem The wrn item\r
125 @param strLog The wrn information\r
126 \r
127 **/\r
128 public static void wrn(String strItem, String strWrn) {\r
129 try {\r
130 writeToWrnFile("Warning when " + strItem + "::" + strWrn);\r
131 showWrnMessage(strWrn);\r
9e011eac 132 } catch (Exception e) {\r
ed1665f2 133 e.printStackTrace();\r
134 }\r
135 }\r
136\r
137 /**\r
138 Call writeToWrnFile to save wrn information to wrn file\r
139 \r
140 @param strLog The wrn information\r
141 \r
142 **/\r
143 public static void wrn(String strWrn) {\r
144 try {\r
145 writeToWrnFile("Warning::" + strWrn);\r
146 showWrnMessage("Warning::" + strWrn);\r
9e011eac 147 } catch (Exception e) {\r
ed1665f2 148 e.printStackTrace();\r
149 }\r
150 }\r
151\r
a13899c5 152 /**\r
153 Call writeToErrFile to save err item and err information to err file\r
154 \r
155 @param strItem The err item\r
156 @param strLog The err information\r
157 \r
158 **/\r
159 public static void err(String strItem, String strErr) {\r
160 try {\r
161 writeToErrFile("Error when " + strItem + "::" + strErr);\r
9e011eac 162 } catch (Exception e) {\r
a13899c5 163 e.printStackTrace();\r
164 }\r
165 }\r
166\r
167 /**\r
168 Call writeToErrFile to save err information to err file\r
169 \r
170 @param strLog The err information\r
171 \r
172 **/\r
173 public static void err(String strErr) {\r
174 try {\r
175 writeToErrFile("Error::" + strErr);\r
9e011eac 176 } catch (Exception e) {\r
a13899c5 177 e.printStackTrace();\r
178 }\r
179 }\r
180\r
181 /**\r
182 Brings up a dialog to show err message\r
183 When the message's length > defined max length, wrap the text to the next line.\r
184 \r
185 @param strErr The input data of err message\r
186 \r
187 **/\r
ed1665f2 188 private static void showWrnMessage(String strErr) {\r
189 String strReturn = Tools.wrapStringByWord(strErr);\r
9e011eac 190 JOptionPane.showConfirmDialog(FrameworkWizardUI.getInstance(), strReturn, "Warning",\r
191 JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);\r
a13899c5 192 }\r
193\r
194 /**\r
195 Open log file and write log information\r
196 \r
197 @param strLog The log information\r
198 @throws IOException\r
199 \r
200 **/\r
9e011eac 201 private static void writeToLogFile(String strLog) throws Exception {\r
202 if (isSaveLog) {\r
203 try {\r
204 createLogDir();\r
205 if (fleLogFile == null) {\r
206 fleLogFile = new File(strLogFileName);\r
207 fleLogFile.delete();\r
208 fleLogFile.createNewFile();\r
209 }\r
210 FileOutputStream fos = new FileOutputStream(fleLogFile, true);\r
211 fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
212 fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
213 fos.flush();\r
214 fos.close();\r
215 } catch (FileNotFoundException e) {\r
216 e.printStackTrace();\r
217 } catch (IOException e) {\r
218 e.printStackTrace();\r
a13899c5 219 }\r
ed1665f2 220 }\r
221 }\r
222\r
223 /**\r
224 Open wrn file and write wrn information\r
225 \r
226 @param strLog The log information\r
9e011eac 227 * @throws Exception \r
ed1665f2 228 \r
229 **/\r
9e011eac 230 private static void writeToWrnFile(String strLog) throws Exception {\r
231 if (isSaveLog) {\r
232 try {\r
233 createLogDir();\r
234 if (fleWrnFile == null) {\r
235 fleWrnFile = new File(strWrnFileName);\r
236 fleWrnFile.delete();\r
237 fleWrnFile.createNewFile();\r
238 }\r
239 FileOutputStream fos = new FileOutputStream(fleWrnFile, true);\r
240 fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
241 fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
242 fos.flush();\r
243 fos.close();\r
244 } catch (FileNotFoundException e) {\r
245 e.printStackTrace();\r
246 } catch (IOException e) {\r
247 e.printStackTrace();\r
ed1665f2 248 }\r
a13899c5 249 }\r
250 }\r
251\r
252 /**\r
253 Open err file and write err information\r
254 \r
255 @param strLog The log information\r
256 @throws IOException\r
257 \r
258 **/\r
9e011eac 259 private static void writeToErrFile(String strLog) throws Exception {\r
260 if (isSaveLog) {\r
261 try {\r
262 createLogDir();\r
263 if (fleErrFile == null) {\r
264 fleErrFile = new File(strErrFileName);\r
265 fleErrFile.delete();\r
266 fleErrFile.createNewFile();\r
267 }\r
268 FileOutputStream fos = new FileOutputStream(fleErrFile, true);\r
269 fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r
270 fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r
271 fos.flush();\r
272 fos.close();\r
273 } catch (FileNotFoundException e) {\r
274 e.printStackTrace();\r
275 } catch (IOException e) {\r
276 e.printStackTrace();\r
a13899c5 277 }\r
a13899c5 278 }\r
279 }\r
9e011eac 280\r
281 /**\r
282 Check if directory for Logs exists or not\r
283 Create the directory if it doesn't exist \r
284 * @throws Exception \r
285 \r
286 **/\r
287 private static void createLogDir() throws Exception {\r
288 File f = new File(strLogDir);\r
289 if (!f.exists()) {\r
290 FileOperation.newFolder(strLogDir);\r
291 }\r
292 }\r
293\r
294 public static boolean isSaveLog() {\r
295 return isSaveLog;\r
296 }\r
297\r
298 public static void setSaveLog(boolean isSaveLog) {\r
299 Log.isSaveLog = isSaveLog;\r
300 }\r
a13899c5 301}\r