]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - Tools/Source/GenBuild/org/tianocore/build/global/GenBuildLogger.java
1. Fix EDKT314 Display module source/binary in a radio box
[mirror_edk2.git] / Tools / 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 /// flag to present whether cache all msg or not\r
50 /// true means to cache.\r
51 ///\r
52 private static boolean flag = false;\r
53 \r
54 private static boolean enableFlag = true;\r
55\r
56 private static Map<FpdModuleIdentification, List<String>> map = new LinkedHashMap<FpdModuleIdentification, List<String> >(256);\r
57 \r
58 private FpdModuleIdentification id = null;\r
59 \r
60 public GenBuildLogger () {\r
61 \r
62 }\r
63\r
64 public GenBuildLogger (Project project) {\r
65 this.project = project;\r
66 }\r
67\r
68 /**\r
69 Rules: flag = false: means no cache Action: Print it to console\r
70 \r
71 flag = true: mean cache all msg exception some special Action: loglevel\r
72 is EDK_ALWAYS -- Print but no cache loglevel is EDK_ERROR -- Print and\r
73 cache the msg others -- No print and cache the msg\r
74 **/\r
75 public synchronized void putMessage(Object msgSource, int msgLevel, String msg) {\r
76 if (this.project == null) {\r
77 return;\r
78 }\r
79\r
80 //\r
81 // If msgLevel is always print, then print it\r
82 //\r
83 switch (msgLevel) {\r
84 case EdkLog.EDK_ALWAYS:\r
85 //\r
86 // Do some special\r
87 //\r
88 log(msgSource, msg, Project.MSG_ERR);\r
89 break;\r
90 case EdkLog.EDK_ERROR:\r
91 log(msgSource, msg, Project.MSG_ERR);\r
92 break;\r
93 case EdkLog.EDK_WARNING:\r
94 log(msgSource, msg, Project.MSG_WARN);\r
95 break;\r
96 case EdkLog.EDK_INFO:\r
97 log(msgSource, msg, Project.MSG_INFO);\r
98 break;\r
99 case EdkLog.EDK_VERBOSE:\r
100 log(msgSource, msg, Project.MSG_VERBOSE);\r
101 break;\r
102 case EdkLog.EDK_DEBUG:\r
103 log(msgSource, msg, Project.MSG_DEBUG);\r
104 break;\r
105 }\r
106 }\r
107 \r
108 public static void flushErrorModuleLog(FpdModuleIdentification errorModuleId) {\r
109 List<String> errorLogs = map.get(errorModuleId);\r
110 if (errorLogs != null) {\r
111 EdkLog.log("ErrorLog", EdkLog.EDK_ERROR, errorModuleId + " error logs: ");\r
112 for(int i = 0; i < errorLogs.size(); i++) {\r
113 EdkLog.log(EdkLog.EDK_ERROR, errorLogs.get(i));\r
114 }\r
115 }\r
116 }\r
117\r
118 public void flushToFile(File file) {\r
119 //\r
120 // Put all messages in map to file\r
121 //\r
122 String msg = "Writing log to file [" + file.getPath() + "]";\r
123 log("Logging", msg, Project.MSG_INFO);\r
124 try {\r
125 BufferedWriter bw = new BufferedWriter(new FileWriter(file));\r
126 Iterator<FpdModuleIdentification> iter = map.keySet().iterator();\r
127 List<String> mainLogs = null;\r
128 while (iter.hasNext()) {\r
129 FpdModuleIdentification item = iter.next();\r
130 if(item == null) {\r
131 mainLogs = map.get(item);\r
132 continue ;\r
133 }\r
134 bw.write(">>>>>>>>>>>>>");\r
135 bw.write(" " + item + " Build Log ");\r
136 bw.write(">>>>>>>>>>>>>");\r
137 bw.newLine();\r
138 List<String> allMessages = map.get(item);\r
139 for(int i = 0; i < allMessages.size(); i++) {\r
140 bw.write(allMessages.get(i));\r
141 bw.newLine();\r
142 }\r
143 }\r
144 if (mainLogs != null) {\r
145 bw.write(">>>>>>>>>>>>>");\r
146 bw.write(" Main Logs (already print to command) ");\r
147 bw.write(">>>>>>>>>>>>>");\r
148 bw.newLine();\r
149 for(int i = 0; i < mainLogs.size(); i++) {\r
150 bw.write(mainLogs.get(i));\r
151 bw.newLine();\r
152 }\r
153 }\r
154 bw.flush();\r
155 bw.close();\r
156 } catch (IOException e) {\r
157 new BuildException("Writing log error. " + e.getMessage());\r
158 }\r
159 \r
160 }\r
161 \r
162 private void log(Object msgSource, String msg, int level) {\r
163 if (msgSource instanceof Task) {\r
164 ((Task)msgSource).getProject().log((Task)msgSource, msg, level);\r
165 } else if (msgSource instanceof String){\r
166 //\r
167 // Pad 12 space to keep message in unify format\r
168 //\r
169 msg = msg.replaceAll("\n", "\n ");\r
170 this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);\r
171 } else {\r
172 this.project.log(msg, level);\r
173 }\r
174 }\r
175 public void targetStarted(BuildEvent event) {\r
176 if (!flag) {\r
177 super.targetStarted(event);\r
178 }\r
179 }\r
180 \r
181 public void messageLogged(BuildEvent event) {\r
182 if (!enableFlag) {\r
183 return ;\r
184 }\r
185 int currentLevel = event.getPriority();\r
186 //\r
187 // If current level is upper than Ant Level, skip it\r
188 //\r
189 if (currentLevel <= this.msgOutputLevel) {\r
190 String originalMessage = event.getMessage();\r
191\r
192 StringBuffer message = new StringBuffer();\r
193 if (!emacsMode && event.getTask() != null) {\r
194 String label = String.format("%12s", "[" + event.getTask().getTaskName() + "] ");\r
195 //\r
196 // Append label first\r
197 //\r
198 message.append(label);\r
199 \r
200 //\r
201 // Format all output message's line separator\r
202 //\r
203 try {\r
204 BufferedReader r = new BufferedReader(new StringReader(originalMessage));\r
205 boolean ifFirstLine = true;\r
206 String line = null;\r
207 while ((line = r.readLine()) != null) {\r
208 if (!ifFirstLine) {\r
209 message.append(StringUtils.LINE_SEP);\r
210 }\r
211 ifFirstLine = false;\r
212 message.append(line);\r
213 }\r
214 } catch (IOException e) {\r
215 message.append(originalMessage);\r
216 }\r
217 } else {\r
218 message.append(originalMessage);\r
219 }\r
220\r
221 String msg = message.toString();\r
222 if (currentLevel == Project.MSG_ERR) {\r
223 printMessage(msg, err, currentLevel);\r
224 } else if(currentLevel == Project.MSG_WARN) {\r
225 printMessage(msg, out, currentLevel);\r
226 } else if(!flag) {\r
227 printMessage(msg, out, currentLevel);\r
228 } \r
229 log(msg);\r
230 }\r
231 }\r
232 \r
233 public static void setCacheEnable(boolean enable) {\r
234 flag = enable;\r
235 }\r
236 \r
237 public static void maskAllLog(boolean enable) {\r
238 enableFlag = !enable;\r
239 }\r
240 \r
241 protected synchronized void log(String message) {\r
242 //\r
243 // cache log\r
244 //\r
245 if (map.containsKey(this.id)) {\r
246 map.get(this.id).add(message);\r
247 } else {\r
248 List<String> list = new Vector<String>(1024);\r
249 list.add(message);\r
250 map.put(this.id, list);\r
251 }\r
252 }\r
253 \r
254 public Object clone() {\r
255 GenBuildLogger newLogger = new GenBuildLogger();\r
256 //\r
257 // Transfer emacs mode, out, err, level to new Logger\r
258 //\r
259 newLogger.setEmacsMode(this.emacsMode);\r
260 newLogger.setOutputPrintStream(this.out);\r
261 newLogger.setErrorPrintStream(this.err);\r
262 newLogger.setMessageOutputLevel(this.msgOutputLevel);\r
263 \r
264 //\r
265 // Transfer project\r
266 //\r
267 newLogger.project = this.project;\r
268 return newLogger;\r
269 }\r
270\r
271 public void setId(FpdModuleIdentification id) {\r
272 this.id = id;\r
273 }\r
274}