+++ /dev/null
-/*++\r
-\r
- Copyright (c) 2006, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
- Module Name:\r
- GenBuildLogger.java\r
-\r
- Abstract:\r
-\r
- --*/\r
-\r
-package org.tianocore.build.global;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.BufferedWriter;\r
-import java.io.File;\r
-import java.io.FileWriter;\r
-import java.io.IOException;\r
-import java.io.StringReader;\r
-import java.util.Iterator;\r
-import java.util.LinkedHashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Vector;\r
-\r
-import org.apache.tools.ant.BuildEvent;\r
-import org.apache.tools.ant.BuildException;\r
-import org.apache.tools.ant.DefaultLogger;\r
-import org.apache.tools.ant.Project;\r
-import org.apache.tools.ant.Task;\r
-import org.apache.tools.ant.util.StringUtils;\r
-\r
-import org.tianocore.build.id.FpdModuleIdentification;\r
-import org.tianocore.common.logger.EdkLog;\r
-import org.tianocore.common.logger.LogMethod;\r
-\r
-public class GenBuildLogger extends DefaultLogger implements LogMethod {\r
- \r
- Project project = null;\r
- \r
- ///\r
- /// flag to present whether cache all msg or not\r
- /// true means to cache.\r
- ///\r
- private static boolean flag = false;\r
-\r
- private static Map<FpdModuleIdentification, List<String>> map = new LinkedHashMap<FpdModuleIdentification, List<String> >(256);\r
- \r
- private FpdModuleIdentification id = null;\r
- //\r
- // semaroph for multi thread\r
- // \r
- public static Object semaphore = new Object();\r
- \r
- public GenBuildLogger () {\r
- \r
- }\r
-\r
- public GenBuildLogger (Project project) {\r
- this.project = project;\r
- }\r
-\r
- /**\r
- Rules: flag = false: means no cache Action: Print it to console\r
- \r
- flag = true: mean cache all msg exception some special Action: loglevel\r
- is EDK_ALWAYS -- Print but no cache loglevel is EDK_ERROR -- Print and\r
- cache the msg others -- No print and cache the msg\r
- **/\r
- public synchronized void putMessage(Object msgSource, int msgLevel, String msg) {\r
- if (this.project == null) {\r
- return;\r
- }\r
-\r
- //\r
- // If msgLevel is always print, then print it\r
- //\r
- switch (msgLevel) {\r
- case EdkLog.EDK_ALWAYS:\r
- //\r
- // Do some special\r
- //\r
- log(msgSource, msg, Project.MSG_ERR);\r
- break;\r
- case EdkLog.EDK_ERROR:\r
- log(msgSource, msg, Project.MSG_ERR);\r
- break;\r
- case EdkLog.EDK_WARNING:\r
- log(msgSource, msg, Project.MSG_WARN);\r
- break;\r
- case EdkLog.EDK_INFO:\r
- log(msgSource, msg, Project.MSG_INFO);\r
- break;\r
- case EdkLog.EDK_VERBOSE:\r
- log(msgSource, msg, Project.MSG_VERBOSE);\r
- break;\r
- case EdkLog.EDK_DEBUG:\r
- log(msgSource, msg, Project.MSG_DEBUG);\r
- break;\r
- }\r
- }\r
- \r
- public static void flushErrorModuleLog(FpdModuleIdentification errorModuleId) {\r
- List<String> errorLogs = map.get(errorModuleId);\r
- if (errorLogs != null) {\r
- EdkLog.log("ErrorLog", EdkLog.EDK_ERROR, errorModuleId + " error logs: ");\r
- for(int i = 0; i < errorLogs.size(); i++) {\r
- EdkLog.log(EdkLog.EDK_ERROR, errorLogs.get(i));\r
- }\r
- }\r
- }\r
-\r
- public void flushToFile(File file) {\r
- //\r
- // Put all messages in map to file\r
- //\r
- String msg = "Writing log to file [" + file.getPath() + "]";\r
- log("Logging", msg, Project.MSG_INFO);\r
- try {\r
- BufferedWriter bw = new BufferedWriter(new FileWriter(file));\r
- Iterator<FpdModuleIdentification> iter = map.keySet().iterator();\r
- List<String> mainLogs = null;\r
- while (iter.hasNext()) {\r
- FpdModuleIdentification item = iter.next();\r
- if(item == null) {\r
- mainLogs = map.get(item);\r
- continue ;\r
- }\r
- bw.write(">>>>>>>>>>>>>");\r
- bw.write(" " + item + " Build Log ");\r
- bw.write(">>>>>>>>>>>>>");\r
- bw.newLine();\r
- List<String> allMessages = map.get(item);\r
- for(int i = 0; i < allMessages.size(); i++) {\r
- bw.write(allMessages.get(i));\r
- bw.newLine();\r
- }\r
- }\r
- if (mainLogs != null) {\r
- bw.write(">>>>>>>>>>>>>");\r
- bw.write(" Main Logs (already print to command) ");\r
- bw.write(">>>>>>>>>>>>>");\r
- bw.newLine();\r
- for(int i = 0; i < mainLogs.size(); i++) {\r
- bw.write(mainLogs.get(i));\r
- bw.newLine();\r
- }\r
- }\r
- bw.flush();\r
- bw.close();\r
- } catch (IOException e) {\r
- new BuildException("Writing log error. " + e.getMessage());\r
- }\r
- \r
- }\r
- \r
- private void log(Object msgSource, String msg, int level) {\r
- if (msgSource instanceof Task) {\r
- ((Task)msgSource).getProject().log((Task)msgSource, msg, level);\r
- } else if (msgSource instanceof String){\r
- //\r
- // Pad 12 space to keep message in unify format\r
- //\r
- msg = msg.replaceAll("\n", "\n ");\r
- this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);\r
- } else {\r
- this.project.log(msg, level);\r
- }\r
- }\r
- public void targetStarted(BuildEvent event) {\r
- if (!flag) {\r
- super.targetStarted(event);\r
- }\r
- }\r
- \r
- public void messageLogged(BuildEvent event) {\r
- \r
- int currentLevel = event.getPriority();\r
- //\r
- // If current level is upper than Ant Level, skip it\r
- //\r
- if (currentLevel <= this.msgOutputLevel) {\r
- String originalMessage = event.getMessage();\r
-\r
- StringBuffer message = new StringBuffer();\r
- if (!emacsMode && event.getTask() != null) {\r
- String label = String.format("%12s", "[" + event.getTask().getTaskName() + "] ");\r
- //\r
- // Append label first\r
- //\r
- message.append(label);\r
- \r
- //\r
- // Format all output message's line separator\r
- //\r
- try {\r
- BufferedReader r = new BufferedReader(new StringReader(originalMessage));\r
- boolean ifFirstLine = true;\r
- String line = null;\r
- while ((line = r.readLine()) != null) {\r
- if (!ifFirstLine) {\r
- message.append(StringUtils.LINE_SEP);\r
- }\r
- ifFirstLine = false;\r
- message.append(line);\r
- }\r
- } catch (IOException e) {\r
- message.append(originalMessage);\r
- }\r
- } else {\r
- message.append(originalMessage);\r
- }\r
-\r
- String msg = message.toString();\r
- if (currentLevel == Project.MSG_ERR) {\r
- printMessage(msg, err, currentLevel);\r
- } else if(currentLevel == Project.MSG_WARN) {\r
- printMessage(msg, out, currentLevel);\r
- } else if(!flag) {\r
- printMessage(msg, out, currentLevel);\r
- } \r
- log(msg);\r
- }\r
- }\r
- \r
- public static void setCacheEnable(boolean enable) {\r
- flag = enable;\r
- }\r
- \r
- protected synchronized void log(String message) {\r
- //\r
- // cache log\r
- //\r
- if (map.containsKey(this.id)) {\r
- map.get(this.id).add(message);\r
- } else {\r
- List<String> list = new Vector<String>(1024);\r
- list.add(message);\r
- map.put(this.id, list);\r
- }\r
- }\r
- \r
- public Object clone() {\r
- GenBuildLogger newLogger = new GenBuildLogger();\r
- //\r
- // Transfer emacs mode, out, err, level to new Logger\r
- //\r
- newLogger.setEmacsMode(this.emacsMode);\r
- newLogger.setOutputPrintStream(this.out);\r
- newLogger.setErrorPrintStream(this.err);\r
- newLogger.setMessageOutputLevel(this.msgOutputLevel);\r
- \r
- //\r
- // Transfer project\r
- //\r
- newLogger.project = this.project;\r
- return newLogger;\r
- }\r
-\r
- public void setId(FpdModuleIdentification id) {\r
- this.id = id;\r
- }\r
-\r
- public void buildFinished(BuildEvent event) {\r
- if (this.msgOutputLevel >= Project.MSG_VERBOSE) {\r
- int level = this.msgOutputLevel;\r
- synchronized(semaphore){\r
- this.msgOutputLevel = this.msgOutputLevel - 1;\r
- super.buildFinished(event);\r
- this.msgOutputLevel = level;\r
- }\r
- } else {\r
- super.buildFinished(event);\r
- }\r
- }\r
-}
\ No newline at end of file