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