]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Java/Source/GenBuild/org/tianocore/build/global/GenBuildLogger.java
Change some format and remove some unused codes.
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / global / GenBuildLogger.java
... / ...
CommitLineData
1/*++\r
2\r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12 Module Name:\r
13 GenBuildLogger.java\r
14\r
15 Abstract:\r
16\r
17 --*/\r
18\r
19package org.tianocore.build.global;\r
20\r
21import java.io.BufferedReader;\r
22import java.io.BufferedWriter;\r
23import java.io.File;\r
24import java.io.FileWriter;\r
25import java.io.IOException;\r
26import java.io.StringReader;\r
27import java.util.Iterator;\r
28import java.util.LinkedHashMap;\r
29import java.util.List;\r
30import java.util.Map;\r
31import java.util.Vector;\r
32\r
33import org.apache.tools.ant.BuildEvent;\r
34import org.apache.tools.ant.BuildException;\r
35import org.apache.tools.ant.DefaultLogger;\r
36import org.apache.tools.ant.Project;\r
37import org.apache.tools.ant.Task;\r
38import org.apache.tools.ant.util.StringUtils;\r
39\r
40import org.tianocore.build.id.FpdModuleIdentification;\r
41import org.tianocore.common.logger.EdkLog;\r
42import org.tianocore.common.logger.LogMethod;\r
43\r
44public class GenBuildLogger extends DefaultLogger implements LogMethod {\r
45 \r
46 Project project = null;\r
47 \r
48 ///\r
49 /// Time of the start of the build \r
50 ///\r
51 private long startTime = System.currentTimeMillis();\r
52 \r
53 ///\r
54 /// flag to present whether cache all msg or not\r
55 /// true means to cache.\r
56 ///\r
57 private static boolean flag = false;\r
58\r
59 private static Map<FpdModuleIdentification, List<String>> map = new LinkedHashMap<FpdModuleIdentification, List<String> >(256);\r
60 \r
61 private FpdModuleIdentification id = null;\r
62 \r
63 public GenBuildLogger () {\r
64 \r
65 }\r
66\r
67 public GenBuildLogger (Project project) {\r
68 this.project = project;\r
69 }\r
70\r
71 /**\r
72 Rules: flag = false: means no cache Action: Print it to console\r
73 \r
74 flag = true: mean cache all msg exception some special Action: loglevel\r
75 is EDK_ALWAYS -- Print but no cache loglevel is EDK_ERROR -- Print and\r
76 cache the msg others -- No print and cache the msg\r
77 **/\r
78 public synchronized void putMessage(Object msgSource, int msgLevel, String msg) {\r
79 if (this.project == null) {\r
80 return;\r
81 }\r
82\r
83 //\r
84 // If msgLevel is always print, then print it\r
85 //\r
86 switch (msgLevel) {\r
87 case EdkLog.EDK_ALWAYS:\r
88 //\r
89 // Do some special\r
90 //\r
91 log(msgSource, msg, Project.MSG_ERR);\r
92 break;\r
93 case EdkLog.EDK_ERROR:\r
94 log(msgSource, msg, Project.MSG_ERR);\r
95 break;\r
96 case EdkLog.EDK_WARNING:\r
97 log(msgSource, msg, Project.MSG_WARN);\r
98 break;\r
99 case EdkLog.EDK_INFO:\r
100 log(msgSource, msg, Project.MSG_INFO);\r
101 break;\r
102 case EdkLog.EDK_VERBOSE:\r
103 log(msgSource, msg, Project.MSG_VERBOSE);\r
104 break;\r
105 case EdkLog.EDK_DEBUG:\r
106 log(msgSource, msg, Project.MSG_DEBUG);\r
107 break;\r
108 }\r
109 }\r
110 \r
111 public static void flushErrorModuleLog(FpdModuleIdentification errorModuleId) {\r
112 List<String> errorLogs = map.get(errorModuleId);\r
113 if (errorLogs != null) {\r
114 EdkLog.log("ErrorLog", EdkLog.EDK_ERROR, errorModuleId + " error logs: ");\r
115 for(int i = 0; i < errorLogs.size(); i++) {\r
116 EdkLog.log(EdkLog.EDK_ERROR, errorLogs.get(i));\r
117 }\r
118 }\r
119 }\r
120\r
121 public void flushToFile(File file) {\r
122 //\r
123 // Put all messages in map to file\r
124 //\r
125 String msg = "Writing log to file [" + file.getPath() + "]";\r
126 log("Logging", msg, Project.MSG_INFO);\r
127 try {\r
128 BufferedWriter bw = new BufferedWriter(new FileWriter(file));\r
129 Iterator<FpdModuleIdentification> iter = map.keySet().iterator();\r
130 List<String> mainLogs = null;\r
131 while (iter.hasNext()) {\r
132 FpdModuleIdentification item = iter.next();\r
133 if(item == null) {\r
134 mainLogs = map.get(item);\r
135 continue ;\r
136 }\r
137 bw.write(">>>>>>>>>>>>>");\r
138 bw.write(" " + item + " Build Log ");\r
139 bw.write(">>>>>>>>>>>>>");\r
140 bw.newLine();\r
141 List<String> allMessages = map.get(item);\r
142 for(int i = 0; i < allMessages.size(); i++) {\r
143 bw.write(allMessages.get(i));\r
144 bw.newLine();\r
145 }\r
146 }\r
147 if (mainLogs != null) {\r
148 bw.write(">>>>>>>>>>>>>");\r
149 bw.write(" Main Logs (already print to command) ");\r
150 bw.write(">>>>>>>>>>>>>");\r
151 bw.newLine();\r
152 for(int i = 0; i < mainLogs.size(); i++) {\r
153 bw.write(mainLogs.get(i));\r
154 bw.newLine();\r
155 }\r
156 }\r
157 bw.flush();\r
158 bw.close();\r
159 } catch (IOException e) {\r
160 new BuildException("Writing log error. " + e.getMessage());\r
161 }\r
162 \r
163 }\r
164 \r
165 private void log(Object msgSource, String msg, int level) {\r
166 if (msgSource instanceof Task) {\r
167 ((Task)msgSource).getProject().log((Task)msgSource, msg, level);\r
168 } else if (msgSource instanceof String){\r
169 //\r
170 // Pad 12 space to keep message in unify format\r
171 //\r
172 msg = msg.replaceAll("\n", "\n ");\r
173 this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);\r
174 } else {\r
175 this.project.log(msg, level);\r
176 }\r
177 }\r
178 public void targetStarted(BuildEvent event) {\r
179 if (!flag) {\r
180 super.targetStarted(event);\r
181 }\r
182 }\r
183 \r
184 public void messageLogged(BuildEvent event) {\r
185 \r
186 int currentLevel = event.getPriority();\r
187 //\r
188 // If current level is upper than Ant Level, skip it\r
189 //\r
190 if (currentLevel <= this.msgOutputLevel) {\r
191 String originalMessage = event.getMessage();\r
192\r
193 StringBuffer message = new StringBuffer();\r
194 if (!emacsMode && event.getTask() != null) {\r
195 String label = String.format("%12s", "[" + event.getTask().getTaskName() + "] ");\r
196 //\r
197 // Append label first\r
198 //\r
199 message.append(label);\r
200 \r
201 //\r
202 // Format all output message's line separator\r
203 //\r
204 try {\r
205 BufferedReader r = new BufferedReader(new StringReader(originalMessage));\r
206 boolean ifFirstLine = true;\r
207 String line = null;\r
208 while ((line = r.readLine()) != null) {\r
209 if (!ifFirstLine) {\r
210 message.append(StringUtils.LINE_SEP);\r
211 }\r
212 ifFirstLine = false;\r
213 message.append(line);\r
214 }\r
215 } catch (IOException e) {\r
216 message.append(originalMessage);\r
217 }\r
218 } else {\r
219 message.append(originalMessage);\r
220 }\r
221\r
222 String msg = message.toString();\r
223 if (currentLevel == Project.MSG_ERR) {\r
224 printMessage(msg, err, currentLevel);\r
225 } else if(currentLevel == Project.MSG_WARN) {\r
226 printMessage(msg, out, currentLevel);\r
227 } else if(!flag) {\r
228 printMessage(msg, out, currentLevel);\r
229 } \r
230 log(msg);\r
231 }\r
232 }\r
233 \r
234 public static void setCacheEnable(boolean enable) {\r
235 flag = enable;\r
236 }\r
237 \r
238 protected synchronized void log(String message) {\r
239 //\r
240 // cache log\r
241 //\r
242 if (map.containsKey(this.id)) {\r
243 map.get(this.id).add(message);\r
244 } else {\r
245 List<String> list = new Vector<String>(1024);\r
246 list.add(message);\r
247 map.put(this.id, list);\r
248 }\r
249 }\r
250 \r
251 public Object clone() {\r
252 GenBuildLogger newLogger = new GenBuildLogger();\r
253 //\r
254 // Transfer emacs mode, out, err, level to new Logger\r
255 //\r
256 newLogger.setEmacsMode(this.emacsMode);\r
257 newLogger.setOutputPrintStream(this.out);\r
258 newLogger.setErrorPrintStream(this.err);\r
259 newLogger.setMessageOutputLevel(this.msgOutputLevel);\r
260 \r
261 //\r
262 // Transfer project\r
263 //\r
264 newLogger.project = this.project;\r
265 return newLogger;\r
266 }\r
267\r
268 public void setId(FpdModuleIdentification id) {\r
269 this.id = id;\r
270 }\r
271\r
272 public void buildFinished(BuildEvent event) {\r
273 Throwable error = event.getException();\r
274 StringBuffer message = new StringBuffer();\r
275\r
276 if (error == null) {\r
277 message.append(StringUtils.LINE_SEP);\r
278 message.append("BUILD SUCCESSFUL");\r
279 } else {\r
280 message.append(StringUtils.LINE_SEP);\r
281 message.append("BUILD FAILED");\r
282 message.append(StringUtils.LINE_SEP);\r
283\r
284 if (Project.MSG_DEBUG <= msgOutputLevel\r
285 || !(error instanceof BuildException)) {\r
286 message.append(StringUtils.getStackTrace(error));\r
287 } else {\r
288 if (error instanceof BuildException) {\r
289 message.append(error.toString()).append(lSep);\r
290 } else {\r
291 message.append(error.getMessage()).append(lSep);\r
292 }\r
293 }\r
294 }\r
295 message.append(StringUtils.LINE_SEP);\r
296 message.append("Total time: ");\r
297 message.append(formatTime(System.currentTimeMillis() - startTime));\r
298\r
299 String msg = message.toString();\r
300 if (error == null) {\r
301 printMessage(msg, out, Project.MSG_VERBOSE);\r
302 } else {\r
303 printMessage(msg, err, Project.MSG_ERR);\r
304 }\r
305 log(msg);\r
306 }\r
307}