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