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