]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/global/GenBuildLogger.java
Remove TianoToolsException.java. Add code to stop all threads once one corrupts.
[mirror_edk2.git] / Tools / 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.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Vector;
31
32 import org.apache.tools.ant.BuildEvent;
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.DefaultLogger;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.Task;
37 import org.apache.tools.ant.util.StringUtils;
38
39 import org.tianocore.build.id.Identification;
40 import org.tianocore.common.logger.EdkLog;
41 import org.tianocore.common.logger.LogMethod;
42
43 public class GenBuildLogger extends DefaultLogger implements LogMethod {
44
45 private Project project = null;
46
47 ///
48 /// flag to present whether cache all msg or not
49 /// true means to cache.
50 ///
51 private static boolean flag = false;
52
53 private static boolean enableFlag = true;
54
55 private static Map<Identification, List<String>> map = new HashMap<Identification, List<String> >(256);
56
57 private Identification id = null;
58
59 public GenBuildLogger () {
60
61 }
62
63 public GenBuildLogger (Project project) {
64 this.project = project;
65 }
66
67 public GenBuildLogger (Project project, Identification id) {
68 this.project = project;
69 this.id = id;
70 }
71
72 /**
73 Rules: flag = false: means no cache Action: Print it to console
74
75 flag = true: mean cache all msg exception some special Action: loglevel
76 is EDK_ALWAYS -- Print but no cache loglevel is EDK_ERROR -- Print and
77 cache the msg others -- No print and cache the msg
78 **/
79 public synchronized void putMessage(Object msgSource, int msgLevel, String msg) {
80 if (this.project == null) {
81 return;
82 }
83
84 //
85 // If msgLevel is always print, then print it
86 //
87 switch (msgLevel) {
88 case EdkLog.EDK_ALWAYS:
89 //
90 // Do some special
91 //
92 log(msgSource, msg, Project.MSG_ERR);
93 break;
94 case EdkLog.EDK_ERROR:
95 log(msgSource, msg, Project.MSG_ERR);
96 break;
97 case EdkLog.EDK_WARNING:
98 log(msgSource, msg, Project.MSG_WARN);
99 break;
100 case EdkLog.EDK_INFO:
101 log(msgSource, msg, Project.MSG_INFO);
102 break;
103 case EdkLog.EDK_VERBOSE:
104 log(msgSource, msg, Project.MSG_VERBOSE);
105 break;
106 case EdkLog.EDK_DEBUG:
107 log(msgSource, msg, Project.MSG_DEBUG);
108 break;
109 }
110 }
111
112 public void flushToFile(File file) {
113 //
114 // Put all messages in map to file
115 //
116 String msg = "Writing log to file [" + file.getPath() + "]";
117 log("Logging", msg, Project.MSG_INFO);
118 try {
119 BufferedWriter bw = new BufferedWriter(new FileWriter(file));
120 List<String> allMessages = map.get(null);
121 for(int i = 0; i < allMessages.size(); i++) {
122 bw.write(allMessages.get(i));
123 bw.newLine();
124 }
125 bw.flush();
126 bw.close();
127 } catch (IOException e) {
128 new BuildException("Writing log error. " + e.getMessage());
129 }
130
131 }
132
133 private void log(Object msgSource, String msg, int level) {
134 if (msgSource instanceof Task) {
135 this.project.log((Task)msgSource, msg, level);
136 } else if (msgSource instanceof String){
137 //
138 // Pad 12 space to keep message in unify format
139 //
140 msg = msg.replaceAll("\n", "\n ");
141 this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);
142 } else {
143 this.project.log(msg, level);
144 }
145 }
146 public void targetStarted(BuildEvent event) {
147 if (!flag) {
148 super.targetStarted(event);
149 }
150 }
151
152 public void messageLogged(BuildEvent event) {
153 if (!enableFlag) {
154 return ;
155 }
156 int currentLevel = event.getPriority();
157 //
158 // If current level is upper than Ant Level, skip it
159 //
160 if (currentLevel <= this.msgOutputLevel) {
161 String originalMessage = event.getMessage();
162
163 StringBuffer message = new StringBuffer();
164 if (!emacsMode && event.getTask() != null) {
165 String label = String.format("%12s", "[" + event.getTask().getTaskName() + "] ");
166 //
167 // Append label first
168 //
169 message.append(label);
170
171 //
172 // Format all output message's line separator
173 //
174 try {
175 BufferedReader r = new BufferedReader(new StringReader(originalMessage));
176 boolean ifFirstLine = true;
177 String line = null;
178 while ((line = r.readLine()) != null) {
179 if (!ifFirstLine) {
180 message.append(StringUtils.LINE_SEP);
181 }
182 ifFirstLine = false;
183 message.append(line);
184 }
185 } catch (IOException e) {
186 message.append(originalMessage);
187 }
188 } else {
189 message.append(originalMessage);
190 }
191
192 String msg = message.toString();
193 if (currentLevel == Project.MSG_ERR) {
194 printMessage(msg, err, currentLevel);
195 } else if(currentLevel == Project.MSG_WARN) {
196 printMessage(msg, out, currentLevel);
197 } else if(!flag) {
198 printMessage(msg, out, currentLevel);
199 }
200
201 log(msg);
202 }
203 }
204
205 public static void setCacheEnable(boolean enable) {
206 flag = enable;
207 }
208
209 public static void maskAllLog(boolean enable) {
210 enableFlag = !enable;
211 }
212
213 protected synchronized void log(String message) {
214 //
215 // cache log
216 //
217 if (map.containsKey(this.id)) {
218 map.get(this.id).add(message);
219 } else {
220 List<String> list = new Vector<String>(1024);
221 list.add(message);
222 map.put(this.id, list);
223 }
224 }
225 }