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