buildframework/helium/sf/java/legacy/src/com/nokia/ant/HeliumLogger.java
changeset 645 b8d81fa19e7d
parent 643 27cf35f95864
child 646 a010554f8551
child 648 d5a8d436d33b
equal deleted inserted replaced
643:27cf35f95864 645:b8d81fa19e7d
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.ant;
       
    19 
       
    20 import java.io.File;
       
    21 import java.io.PrintStream;
       
    22 import java.text.SimpleDateFormat;
       
    23 import java.util.Calendar;
       
    24 
       
    25 import org.apache.log4j.Logger;
       
    26 import org.apache.tools.ant.BuildEvent;
       
    27 import org.apache.tools.ant.DefaultLogger;
       
    28 import org.apache.tools.ant.Project;
       
    29 
       
    30 /**
       
    31  * Logger class that can connect to Ant and log information regarding to build
       
    32  * times, number of errors and such. Data is sent to Diamonds server, where it
       
    33  * is processed further.
       
    34  * 
       
    35  * This class is listening all build related events. It catches the build
       
    36  * start-finish, target start-finish events of Ant and gather build start-end
       
    37  * time, errors/warnings and store in BuildData class. Stored data will be
       
    38  * exported to XML and uploaded to Diamonds server after each specific target.
       
    39  * For example after target "create-bom" this class will upload all BOM data to
       
    40  * Diamonds.
       
    41  * 
       
    42  * 
       
    43  */
       
    44 public class HeliumLogger extends DefaultLogger {
       
    45 
       
    46     private static boolean stopLogToConsole;
       
    47     private static final String INTERNALPROPERTY = "internal.";
       
    48 
       
    49     private Project project;
       
    50     private Logger log = Logger.getLogger(this.getClass());
       
    51     
       
    52 
       
    53 
       
    54     /**
       
    55      * Ant call this function when build start.
       
    56      */
       
    57     public void buildStarted(BuildEvent event) {
       
    58         project = event.getProject();
       
    59         super.buildStarted(event);
       
    60     }
       
    61 
       
    62     /**
       
    63      * Triggered when a target starts.
       
    64      */
       
    65     public void targetStarted(BuildEvent event) {
       
    66         /** The "if" condition to test on execution. */
       
    67         String ifCondition = "";
       
    68         /** The "unless" condition to test on execution. */
       
    69         String unlessCondition = "";
       
    70         String targetName = event.getTarget().getName();
       
    71         logTargetEvent(targetName, "start");
       
    72 
       
    73         /**get the values needed from the event **/
       
    74         ifCondition = event.getTarget().getIf();
       
    75         unlessCondition = event.getTarget().getUnless();
       
    76         project = event.getProject();
       
    77 
       
    78         super.targetStarted(event);
       
    79 
       
    80         /**if the target is not going to execute (due to 'if' or 'unless' conditions) 
       
    81         print a message telling the user why it is not going to execute**/
       
    82         if (!testIfCondition(ifCondition) && ifCondition != null) {
       
    83             if (ifCondition.startsWith(INTERNALPROPERTY)) {
       
    84                 String enableProperty = ifCondition.substring(INTERNALPROPERTY.length());
       
    85                 project.log("Skipped because property '"
       
    86                         + enableProperty
       
    87                         + "' not set to 'true'.", Project.MSG_INFO);
       
    88             } else {
       
    89                 project.log("Skipped because property '"
       
    90                         + project.replaceProperties(ifCondition)
       
    91                         + "' is not set.", Project.MSG_INFO);
       
    92             }
       
    93             
       
    94         } else if (!testUnlessCondition(unlessCondition) && unlessCondition != null) {
       
    95             if (unlessCondition.startsWith(INTERNALPROPERTY)) {
       
    96                 String enableProperty = unlessCondition.substring(INTERNALPROPERTY.length());
       
    97                 project.log("Skipped because property '"
       
    98                         + enableProperty
       
    99                         + "' is set.", Project.MSG_INFO);
       
   100             } else {
       
   101                 project.log("Skipped because property '"
       
   102                         + project.replaceProperties(unlessCondition)
       
   103                         + "' set.", Project.MSG_INFO);
       
   104             }
       
   105         }
       
   106     }
       
   107 
       
   108     /**
       
   109      * Log the start or end of the build as a event.
       
   110      * 
       
   111      * @param targetName
       
   112      *            The name of the current target.
       
   113      * @param event
       
   114      *            A string description of the event.
       
   115      */
       
   116     private void logTargetEvent(String targetName, String event) {
       
   117         String logTargetProperty = project.getProperty("log.target");
       
   118         if ((logTargetProperty != null) && (logTargetProperty.equals("yes"))) {
       
   119             log.info("Target #### " + targetName + " ####: " + event);
       
   120         }
       
   121     }
       
   122 
       
   123     /**
       
   124      * Triggered when a target finishes.
       
   125      */
       
   126     public void targetFinished(BuildEvent event) {
       
   127         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       
   128         String time = sdf.format(Calendar.getInstance().getTime());
       
   129         
       
   130         String targetName = time + "," + event.getTarget().getName();
       
   131 
       
   132         logTargetEvent(targetName, "finish");
       
   133     }
       
   134 
       
   135     /**
       
   136      * Triggered when the build finishes.
       
   137      */
       
   138     public void buildFinished(BuildEvent event) {
       
   139         // re-enabling output of messages at the end of the build
       
   140         stopLogToConsole = false;
       
   141         cleanup();
       
   142         super.buildFinished(event);
       
   143     }
       
   144 
       
   145     /**
       
   146      * See if build needs a final cleanup target to be called.
       
   147      */
       
   148     private void cleanup() {
       
   149         if (project != null) {
       
   150             String loggingoutputfile = project.getProperty("logging.output.file");
       
   151             if (loggingoutputfile != null) {
       
   152                 File file = new File(loggingoutputfile);
       
   153                 if (file.exists()) {
       
   154                     file.delete();
       
   155                 }
       
   156             }
       
   157             if ((project.getProperty("call.cleanup") != null)
       
   158                     && (project.getProperty("call.cleanup").equals("yes"))) {
       
   159                 project.executeTarget("cleanup-all");
       
   160             }
       
   161         }
       
   162     }
       
   163 
       
   164     /**
       
   165      * Get log to console status
       
   166      */
       
   167     public static boolean getStopLogToConsole() {
       
   168         return stopLogToConsole;
       
   169     }
       
   170 
       
   171     /**
       
   172      * Set log to console status
       
   173      */
       
   174     public static void setStopLogToConsole(boolean stop) {
       
   175         stopLogToConsole = stop;
       
   176     }
       
   177 
       
   178     /**
       
   179      * {@inheritDoc}
       
   180      */
       
   181     protected void printMessage(final String message, final PrintStream stream,
       
   182             final int priority) {
       
   183         if (!stopLogToConsole) {
       
   184             stream.println(message);
       
   185         }
       
   186     }
       
   187     
       
   188     /**
       
   189      * Tests whether or not the "if" condition is satisfied.
       
   190      *
       
   191      * @return whether or not the "if" condition is satisfied. If no
       
   192      *         condition (or an empty condition) has been set,
       
   193      *         <code>true</code> is returned.
       
   194      */
       
   195     private boolean testIfCondition(String ifCondition) {
       
   196         if ("".equals(ifCondition)) {
       
   197             return true;
       
   198         }
       
   199 
       
   200         String test = project.replaceProperties(ifCondition);
       
   201         return project.getProperty(test) != null;
       
   202     }
       
   203 
       
   204     /**
       
   205      * Tests whether or not the "unless" condition is satisfied.
       
   206      *
       
   207      * @return whether or not the "unless" condition is satisfied. If no
       
   208      *         condition (or an empty condition) has been set,
       
   209      *         <code>true</code> is returned.
       
   210      */
       
   211     private boolean testUnlessCondition(String unlessCondition) {
       
   212         if ("".equals(unlessCondition)) {
       
   213             return true;
       
   214         }
       
   215         String test = project.replaceProperties(unlessCondition);
       
   216         return project.getProperty(test) == null;
       
   217     }
       
   218 }