]>
Commit | Line | Data |
---|---|---|
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 | |
36 | //\r | |
37 | //Err file\r | |
38 | //\r | |
39 | private static File fleErrFile = null;\r | |
40 | \r | |
41 | //\r | |
42 | //Log file name\r | |
43 | //\r | |
44 | static String strLogFileName = "Log.log";\r | |
45 | \r | |
46 | //\r | |
47 | //Err file name\r | |
48 | //\r | |
49 | static String strErrFileName = "Err.log";\r | |
50 | \r | |
51 | /**\r | |
52 | Main class, used for test\r | |
53 | \r | |
54 | @param args\r | |
55 | \r | |
56 | **/\r | |
57 | public static void main(String[] args) {\r | |
58 | try {\r | |
59 | Log.log("Test", "test");\r | |
60 | Log.err("Test1", "test1");\r | |
61 | Log.err("sdfsdfsd fsdfsdfsdfsdfj dsfksdjflsdjf sdkfjsdklfjsdkf dskfsjdkfjks dskfjsdklfjsdkf sdkfjsdlf sdkfjsdk kdfjskdf sdkfjsdkf ksdjfksdfjskdf sdkfsjdfksd fskdfjsdf", "dfsdf sdfksdf sd sdfksd fsdf");\r | |
62 | } catch (Exception e) {\r | |
63 | e.printStackTrace();\r | |
64 | }\r | |
65 | }\r | |
66 | \r | |
67 | /**\r | |
68 | This is the default constructor\r | |
69 | Do nothing\r | |
70 | \r | |
71 | **/\r | |
72 | public Log() {\r | |
73 | }\r | |
74 | \r | |
75 | /**\r | |
76 | Call writeToLogFile to save log item and log information to log file\r | |
77 | \r | |
78 | @param strItem The log item\r | |
79 | @param strLog The log information\r | |
80 | \r | |
81 | **/\r | |
82 | public static void log(String strItem, String strLog) {\r | |
83 | try {\r | |
84 | writeToLogFile(strItem + ":" + strLog);\r | |
85 | } catch (IOException e) {\r | |
86 | e.printStackTrace();\r | |
87 | }\r | |
88 | }\r | |
89 | \r | |
90 | /**\r | |
91 | Call writeToLogFile to save log information to log file\r | |
92 | \r | |
93 | @param strLog The log information\r | |
94 | \r | |
95 | **/\r | |
96 | public static void log(String strLog) {\r | |
97 | try {\r | |
98 | writeToLogFile(strLog);\r | |
99 | } catch (IOException e) {\r | |
100 | e.printStackTrace();\r | |
101 | }\r | |
102 | }\r | |
103 | \r | |
104 | /**\r | |
105 | Call writeToErrFile to save err item and err information to err file\r | |
106 | \r | |
107 | @param strItem The err item\r | |
108 | @param strLog The err information\r | |
109 | \r | |
110 | **/\r | |
111 | public static void err(String strItem, String strErr) {\r | |
112 | try {\r | |
113 | writeToErrFile("Error when " + strItem + "::" + strErr);\r | |
114 | showErrMessage("Error when " + strItem + "::" + strErr);\r | |
115 | } catch (IOException e) {\r | |
116 | e.printStackTrace();\r | |
117 | }\r | |
118 | }\r | |
119 | \r | |
120 | /**\r | |
121 | Call writeToErrFile to save err information to err file\r | |
122 | \r | |
123 | @param strLog The err information\r | |
124 | \r | |
125 | **/\r | |
126 | public static void err(String strErr) {\r | |
127 | try {\r | |
128 | writeToErrFile("Error::" + strErr);\r | |
129 | showErrMessage("Error::" + strErr);\r | |
130 | } catch (IOException e) {\r | |
131 | e.printStackTrace();\r | |
132 | }\r | |
133 | }\r | |
134 | \r | |
135 | /**\r | |
136 | Brings up a dialog to show err message\r | |
137 | When the message's length > defined max length, wrap the text to the next line.\r | |
138 | \r | |
139 | @param strErr The input data of err message\r | |
140 | \r | |
141 | **/\r | |
142 | private static void showErrMessage(String strErr) {\r | |
143 | int intMaxLength = 40;\r | |
144 | String strReturn = "";\r | |
145 | String strTemp = "";\r | |
146 | while (strErr.length() > 0) {\r | |
147 | if (strErr.length() > intMaxLength) {\r | |
148 | strTemp = strErr.substring(0, intMaxLength);\r | |
149 | strErr = strErr.substring(strTemp.length());\r | |
150 | strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;\r | |
151 | \r | |
152 | } else if (strErr.length() <= intMaxLength) {\r | |
153 | strReturn = strReturn + strErr;\r | |
154 | strErr = "";\r | |
155 | }\r | |
156 | }\r | |
157 | JOptionPane.showConfirmDialog(null, strReturn, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);\r | |
158 | }\r | |
159 | \r | |
160 | /**\r | |
161 | Open log file and write log information\r | |
162 | \r | |
163 | @param strLog The log information\r | |
164 | @throws IOException\r | |
165 | \r | |
166 | **/\r | |
167 | private static void writeToLogFile(String strLog) throws IOException {\r | |
168 | try {\r | |
169 | if (fleLogFile == null) {\r | |
170 | fleLogFile = new File(strLogFileName);\r | |
171 | fleLogFile.createNewFile();\r | |
172 | }\r | |
173 | FileOutputStream fos = new FileOutputStream(fleLogFile, true);\r | |
174 | fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r | |
175 | fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r | |
176 | fos.flush();\r | |
177 | fos.close();\r | |
178 | } catch (FileNotFoundException e) {\r | |
179 | e.printStackTrace();\r | |
180 | } catch (IOException e) {\r | |
181 | e.printStackTrace();\r | |
182 | }\r | |
183 | }\r | |
184 | \r | |
185 | /**\r | |
186 | Open err file and write err information\r | |
187 | \r | |
188 | @param strLog The log information\r | |
189 | @throws IOException\r | |
190 | \r | |
191 | **/\r | |
192 | private static void writeToErrFile(String strLog) throws IOException {\r | |
193 | try {\r | |
194 | if (fleErrFile == null) {\r | |
195 | fleErrFile = new File(strErrFileName);\r | |
196 | fleErrFile.createNewFile();\r | |
197 | }\r | |
198 | FileOutputStream fos = new FileOutputStream(fleErrFile, true);\r | |
199 | fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());\r | |
200 | fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());\r | |
201 | fos.flush();\r | |
202 | fos.close();\r | |
203 | } catch (FileNotFoundException e) {\r | |
204 | e.printStackTrace();\r | |
205 | } catch (IOException e) {\r | |
206 | e.printStackTrace();\r | |
207 | }\r | |
208 | }\r | |
209 | }\r |