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