]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/CreateMdkPkg/src/org/tianocore/common/Log.java
Initial import.
[mirror_edk2.git] / Tools / Source / CreateMdkPkg / 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 CreateMdkPkg 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 } catch (Exception e) {
64 e.printStackTrace();
65 }
66 }
67
68 /**
69 This is the default constructor
70 Do nothing
71
72 **/
73 public Log() {
74 }
75
76 /**
77 Call writeToLogFile to save log item and log information to log file
78
79 @param strItem The log item
80 @param strLog The log information
81
82 **/
83 public static void log(String strItem, String strLog) {
84 try {
85 writeToLogFile(strItem + ":" + strLog);
86 } catch (IOException e) {
87 e.printStackTrace();
88 }
89 }
90
91 /**
92 Call writeToLogFile to save log information to log file
93
94 @param strLog The log information
95
96 **/
97 public static void log(String strLog) {
98 try {
99 writeToLogFile(strLog);
100 } catch (IOException e) {
101 e.printStackTrace();
102 }
103 }
104
105 /**
106 Call writeToErrFile to save err item and err information to err file
107
108 @param strItem The err item
109 @param strLog The err information
110
111 **/
112 public static void err(String strItem, String strErr) {
113 try {
114 writeToErrFile("Error when " + strItem + ":" + strErr);
115 JOptionPane.showConfirmDialog(null, "Error when " + strItem + "::" + strErr, "Error",
116 JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
117
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121 }
122
123 /**
124 Call writeToErrFile to save err information to err file
125
126 @param strLog The err information
127
128 **/
129 public static void err(String strErr) {
130 try {
131 writeToErrFile("Error::" + strErr);
132 JOptionPane.showConfirmDialog(null, "Error::" + strErr, "Error", JOptionPane.DEFAULT_OPTION,
133 JOptionPane.ERROR_MESSAGE);
134 } catch (IOException e) {
135 e.printStackTrace();
136 }
137 }
138
139 /**
140 Open log file and write log information
141
142 @param strLog The log information
143 @throws IOException
144
145 **/
146 private static void writeToLogFile(String strLog) throws IOException {
147 try {
148 if (fleLogFile == null) {
149 fleLogFile = new File(strLogFileName);
150 fleLogFile.createNewFile();
151 }
152 FileOutputStream fos = new FileOutputStream(fleLogFile, true);
153 fos.write((Tools.getCurrentDateTime() + "\r\n").getBytes());
154 fos.write((strLog + "\r\n").getBytes());
155 fos.flush();
156 fos.close();
157 } catch (FileNotFoundException e) {
158 e.printStackTrace();
159 } catch (IOException e) {
160 e.printStackTrace();
161 }
162 }
163
164 /**
165 Open err file and write err information
166
167 @param strLog The log information
168 @throws IOException
169
170 **/
171 private static void writeToErrFile(String strLog) throws IOException {
172 try {
173 if (fleErrFile == null) {
174 fleErrFile = new File(strErrFileName);
175 fleErrFile.createNewFile();
176 }
177 FileOutputStream fos = new FileOutputStream(fleErrFile, true);
178 fos.write((Tools.getCurrentDateTime() + "\r\n").getBytes());
179 fos.write((strLog + "\r\n").getBytes());
180 fos.flush();
181 fos.close();
182 } catch (FileNotFoundException e) {
183 e.printStackTrace();
184 } catch (IOException e) {
185 e.printStackTrace();
186 }
187 }
188 }