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