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