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