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 |
16 | package org.tianocore.frameworkwizard.common;\r |
17 | \r |
18 | import java.io.File;\r |
19 | import java.io.FileNotFoundException;\r |
20 | import java.io.FileOutputStream;\r |
21 | import java.io.IOException;\r |
22 | \r |
23 | import javax.swing.JOptionPane;\r |
24 | \r |
25 | /**\r |
26 | The class is used to provides static interfaces to save log and error information\r |
27 | \r |
28 | **/\r |
29 | public class Log {\r |
30 | \r |
31 | //\r |
32 | //Log file\r |
33 | //\r |
34 | private static File fleLogFile = null;\r |
35 | \r |
ed1665f2 |
36 | //\r |
37 | //Wrn file\r |
38 | //\r |
39 | private static File fleWrnFile = null;\r |
40 | \r |
a13899c5 |
41 | //\r |
42 | //Err file\r |
43 | //\r |
44 | private static File fleErrFile = null;\r |
45 | \r |
46 | //\r |
47 | //Log file name\r |
48 | //\r |
49 | static String strLogFileName = "Log.log";\r |
50 | \r |
ed1665f2 |
51 | //\r |
52 | //Wrn file name\r |
53 | //\r |
54 | static String strWrnFileName = "Wrn.log";\r |
55 | \r |
a13899c5 |
56 | //\r |
57 | //Err file name\r |
58 | //\r |
59 | static String strErrFileName = "Err.log";\r |
60 | \r |
61 | /**\r |
62 | Main class, used for test\r |
63 | \r |
64 | @param args\r |
65 | \r |
66 | **/\r |
67 | public static void main(String[] args) {\r |
68 | try {\r |
ed1665f2 |
69 | //Log.log("Test", "test");\r |
70 | //Log.err("Test1", "test1");\r |
ed1665f2 |
71 | Log\r |
d933fc5f |
72 | .wrn("aaa bbbbbb cccccccccccc ddddddddddd eeeeeeeeee fffffffffff gggggggggggggggggg hhhhhhhhhhhhhhhhhhhhhhhhhhhhh iiiii jjjj kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk lll mmm nn poooooooooooooooooooooooooooooooooooooooooooop");\r |
73 | Log.wrn("Incorrect data type for ModuleEntryPoint");\r |
a13899c5 |
74 | } catch (Exception e) {\r |
75 | e.printStackTrace();\r |
76 | }\r |
77 | }\r |
78 | \r |
a13899c5 |
79 | /**\r |
80 | Call writeToLogFile to save log item and log information to log file\r |
81 | \r |
82 | @param strItem The log item\r |
83 | @param strLog The log information\r |
84 | \r |
85 | **/\r |
86 | public static void log(String strItem, String strLog) {\r |
87 | try {\r |
88 | writeToLogFile(strItem + ":" + strLog);\r |
89 | } catch (IOException e) {\r |
90 | e.printStackTrace();\r |
91 | }\r |
92 | }\r |
93 | \r |
94 | /**\r |
95 | Call writeToLogFile to save log information to log file\r |
96 | \r |
97 | @param strLog The log information\r |
98 | \r |
99 | **/\r |
100 | public static void log(String strLog) {\r |
101 | try {\r |
102 | writeToLogFile(strLog);\r |
103 | } catch (IOException e) {\r |
104 | e.printStackTrace();\r |
105 | }\r |
106 | }\r |
107 | \r |
ed1665f2 |
108 | /**\r |
109 | Call writeToWrnFile to save wrn item and wrn information to wrn file\r |
110 | \r |
111 | @param strItem The wrn item\r |
112 | @param strLog The wrn information\r |
113 | \r |
114 | **/\r |
115 | public static void wrn(String strItem, String strWrn) {\r |
116 | try {\r |
117 | writeToWrnFile("Warning when " + strItem + "::" + strWrn);\r |
118 | showWrnMessage(strWrn);\r |
119 | } catch (IOException e) {\r |
120 | e.printStackTrace();\r |
121 | }\r |
122 | }\r |
123 | \r |
124 | /**\r |
125 | Call writeToWrnFile to save wrn information to wrn file\r |
126 | \r |
127 | @param strLog The wrn information\r |
128 | \r |
129 | **/\r |
130 | public static void wrn(String strWrn) {\r |
131 | try {\r |
132 | writeToWrnFile("Warning::" + strWrn);\r |
133 | showWrnMessage("Warning::" + strWrn);\r |
134 | } catch (IOException e) {\r |
135 | e.printStackTrace();\r |
136 | }\r |
137 | }\r |
138 | \r |
a13899c5 |
139 | /**\r |
140 | Call writeToErrFile to save err item and err information to err file\r |
141 | \r |
142 | @param strItem The err item\r |
143 | @param strLog The err information\r |
144 | \r |
145 | **/\r |
146 | public static void err(String strItem, String strErr) {\r |
147 | try {\r |
148 | writeToErrFile("Error when " + strItem + "::" + strErr);\r |
a13899c5 |
149 | } catch (IOException e) {\r |
150 | e.printStackTrace();\r |
151 | }\r |
152 | }\r |
153 | \r |
154 | /**\r |
155 | Call writeToErrFile to save err information to err file\r |
156 | \r |
157 | @param strLog The err information\r |
158 | \r |
159 | **/\r |
160 | public static void err(String strErr) {\r |
161 | try {\r |
162 | writeToErrFile("Error::" + strErr);\r |
a13899c5 |
163 | } catch (IOException e) {\r |
164 | e.printStackTrace();\r |
165 | }\r |
166 | }\r |
167 | \r |
168 | /**\r |
169 | Brings up a dialog to show err message\r |
170 | When the message's length > defined max length, wrap the text to the next line.\r |
171 | \r |
172 | @param strErr The input data of err message\r |
173 | \r |
174 | **/\r |
ed1665f2 |
175 | private static void showWrnMessage(String strErr) {\r |
176 | String strReturn = Tools.wrapStringByWord(strErr);\r |
d933fc5f |
177 | JOptionPane\r |
178 | .showConfirmDialog(null, strReturn, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);\r |
a13899c5 |
179 | }\r |
180 | \r |
181 | /**\r |
182 | Open log file and write log information\r |
183 | \r |
184 | @param strLog The log information\r |
185 | @throws IOException\r |
186 | \r |
187 | **/\r |
188 | private static void writeToLogFile(String strLog) throws IOException {\r |
189 | try {\r |
190 | if (fleLogFile == null) {\r |
191 | fleLogFile = new File(strLogFileName);\r |
192 | fleLogFile.createNewFile();\r |
193 | }\r |
194 | FileOutputStream fos = new FileOutputStream(fleLogFile, true);\r |
195 | fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r |
196 | fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r |
197 | fos.flush();\r |
198 | fos.close();\r |
199 | } catch (FileNotFoundException e) {\r |
ed1665f2 |
200 | e.printStackTrace();\r |
201 | } catch (IOException e) {\r |
202 | e.printStackTrace();\r |
203 | }\r |
204 | }\r |
205 | \r |
206 | /**\r |
207 | Open wrn file and write wrn information\r |
208 | \r |
209 | @param strLog The log information\r |
210 | @throws IOException\r |
211 | \r |
212 | **/\r |
213 | private static void writeToWrnFile(String strLog) throws IOException {\r |
214 | try {\r |
215 | if (fleWrnFile == null) {\r |
216 | fleWrnFile = new File(strWrnFileName);\r |
217 | fleWrnFile.createNewFile();\r |
218 | }\r |
219 | FileOutputStream fos = new FileOutputStream(fleWrnFile, true);\r |
220 | fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r |
221 | fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r |
222 | fos.flush();\r |
223 | fos.close();\r |
224 | } catch (FileNotFoundException e) {\r |
a13899c5 |
225 | e.printStackTrace();\r |
226 | } catch (IOException e) {\r |
227 | e.printStackTrace();\r |
228 | }\r |
229 | }\r |
230 | \r |
231 | /**\r |
232 | Open err file and write err information\r |
233 | \r |
234 | @param strLog The log information\r |
235 | @throws IOException\r |
236 | \r |
237 | **/\r |
238 | private static void writeToErrFile(String strLog) throws IOException {\r |
239 | try {\r |
240 | if (fleErrFile == null) {\r |
241 | fleErrFile = new File(strErrFileName);\r |
242 | fleErrFile.createNewFile();\r |
243 | }\r |
244 | FileOutputStream fos = new FileOutputStream(fleErrFile, true);\r |
245 | fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r |
246 | fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r |
247 | fos.flush();\r |
248 | fos.close();\r |
249 | } catch (FileNotFoundException e) {\r |
250 | e.printStackTrace();\r |
251 | } catch (IOException e) {\r |
252 | e.printStackTrace();\r |
253 | }\r |
254 | }\r |
255 | }\r |