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