Change some format and remove some unused codes.
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / global / GenBuildLogger.java
CommitLineData
b9546cc8 1/*++\r
2\r
c8df018e 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
b9546cc8 8\r
c8df018e 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
b9546cc8 11\r
c8df018e 12 Module Name:\r
13 GenBuildLogger.java\r
b9546cc8 14\r
c8df018e 15 Abstract:\r
b9546cc8 16\r
c8df018e 17 --*/\r
b9546cc8 18\r
19package org.tianocore.build.global;\r
ff225cbb 20\r
2eb7d78d 21import java.io.BufferedReader;\r
22import java.io.BufferedWriter;\r
c8df018e 23import java.io.File;\r
2eb7d78d 24import java.io.FileWriter;\r
25import java.io.IOException;\r
26import java.io.StringReader;\r
498e9021 27import java.util.Iterator;\r
28import java.util.LinkedHashMap;\r
c8df018e 29import java.util.List;\r
2eb7d78d 30import java.util.Map;\r
c8df018e 31import java.util.Vector;\r
32\r
2eb7d78d 33import org.apache.tools.ant.BuildEvent;\r
34import org.apache.tools.ant.BuildException;\r
35import org.apache.tools.ant.DefaultLogger;\r
b9546cc8 36import org.apache.tools.ant.Project;\r
91f7d582 37import org.apache.tools.ant.Task;\r
2eb7d78d 38import org.apache.tools.ant.util.StringUtils;\r
ff225cbb 39\r
498e9021 40import org.tianocore.build.id.FpdModuleIdentification;\r
c8df018e 41import org.tianocore.common.logger.EdkLog;\r
ff225cbb 42import org.tianocore.common.logger.LogMethod;\r
b9546cc8 43\r
2eb7d78d 44public class GenBuildLogger extends DefaultLogger implements LogMethod {\r
45 \r
498e9021 46 Project project = null;\r
188fdd0a 47 \r
48 ///\r
a06a5417 49 /// Time of the start of the build \r
188fdd0a 50 ///\r
a06a5417 51 private long startTime = System.currentTimeMillis();\r
188fdd0a 52 \r
c8df018e 53 ///\r
54 /// flag to present whether cache all msg or not\r
2eb7d78d 55 /// true means to cache.\r
c8df018e 56 ///\r
2eb7d78d 57 private static boolean flag = false;\r
c8df018e 58\r
498e9021 59 private static Map<FpdModuleIdentification, List<String>> map = new LinkedHashMap<FpdModuleIdentification, List<String> >(256);\r
2eb7d78d 60 \r
498e9021 61 private FpdModuleIdentification id = null;\r
2eb7d78d 62 \r
63 public GenBuildLogger () {\r
64 \r
65 }\r
c8df018e 66\r
67 public GenBuildLogger (Project project) {\r
68 this.project = project;\r
69 }\r
70\r
c8df018e 71 /**\r
2eb7d78d 72 Rules: flag = false: means no cache Action: Print it to console\r
c8df018e 73 \r
2eb7d78d 74 flag = true: mean cache all msg exception some special Action: loglevel\r
c8df018e 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
2eb7d78d 78 public synchronized void putMessage(Object msgSource, int msgLevel, String msg) {\r
c8df018e 79 if (this.project == null) {\r
80 return;\r
81 }\r
188fdd0a 82\r
c8df018e 83 //\r
84 // If msgLevel is always print, then print it\r
85 //\r
86 switch (msgLevel) {\r
87 case EdkLog.EDK_ALWAYS:\r
2eb7d78d 88 //\r
89 // Do some special\r
90 //\r
91 log(msgSource, msg, Project.MSG_ERR);\r
c8df018e 92 break;\r
93 case EdkLog.EDK_ERROR:\r
2eb7d78d 94 log(msgSource, msg, Project.MSG_ERR);\r
c8df018e 95 break;\r
96 case EdkLog.EDK_WARNING:\r
2eb7d78d 97 log(msgSource, msg, Project.MSG_WARN);\r
c8df018e 98 break;\r
99 case EdkLog.EDK_INFO:\r
2eb7d78d 100 log(msgSource, msg, Project.MSG_INFO);\r
c8df018e 101 break;\r
188fdd0a 102 case EdkLog.EDK_VERBOSE:\r
2eb7d78d 103 log(msgSource, msg, Project.MSG_VERBOSE);\r
c8df018e 104 break;\r
105 case EdkLog.EDK_DEBUG:\r
2eb7d78d 106 log(msgSource, msg, Project.MSG_DEBUG);\r
c8df018e 107 break;\r
b9546cc8 108 }\r
c8df018e 109 }\r
498e9021 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
c8df018e 120\r
121 public void flushToFile(File file) {\r
122 //\r
2eb7d78d 123 // Put all messages in map to file\r
c8df018e 124 //\r
2eb7d78d 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
498e9021 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
2eb7d78d 151 bw.newLine();\r
498e9021 152 for(int i = 0; i < mainLogs.size(); i++) {\r
153 bw.write(mainLogs.get(i));\r
154 bw.newLine();\r
155 }\r
2eb7d78d 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
b9546cc8 163 }\r
91f7d582 164 \r
165 private void log(Object msgSource, String msg, int level) {\r
166 if (msgSource instanceof Task) {\r
d965d1f6 167 ((Task)msgSource).getProject().log((Task)msgSource, msg, level);\r
2619585a 168 } else if (msgSource instanceof String){\r
2619585a 169 //\r
2eb7d78d 170 // Pad 12 space to keep message in unify format\r
2619585a 171 //\r
172 msg = msg.replaceAll("\n", "\n ");\r
173 this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);\r
91f7d582 174 } else {\r
175 this.project.log(msg, level);\r
176 }\r
177 }\r
2eb7d78d 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
188fdd0a 185 \r
186 int currentLevel = event.getPriority();\r
2eb7d78d 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
2eb7d78d 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
498e9021 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
a06a5417 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
b9546cc8 307}