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