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