2009-08-09 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / ConsoleLogger.cs
index 25375a177a6ec99e73b3584fbf3f9d0edaf92307..52740db4423945c07a63d99ca265d984883be8d8 100644 (file)
@@ -29,6 +29,7 @@
 
 using System;
 using System.Runtime.InteropServices;
+using System.Collections.Generic;
 using System.IO;
 using System.Security;
 using Microsoft.Build.Framework;
@@ -46,6 +47,7 @@ namespace Microsoft.Build.BuildEngine {
                bool            performanceSummary;
                bool            showSummary;
                bool            skipProjectStartedText;
+               List<string> errors, warnings;
                
                public ConsoleLogger ()
                        : this (LoggerVerbosity.Normal, null, null, null)
@@ -74,6 +76,8 @@ namespace Microsoft.Build.BuildEngine {
                        this.performanceSummary = false;
                        this.showSummary = true;
                        this.skipProjectStartedText = false;
+                       errors = new List<string> ();
+                       warnings = new List<string> ();
                }
                
                public void ApplyParameter (string parameterName,
@@ -100,7 +104,7 @@ namespace Microsoft.Build.BuildEngine {
                
                public void BuildStartedHandler (object sender, BuildStartedEventArgs args)
                {
-                       WriteLine ("");
+                       WriteLine (String.Empty);
                        WriteLine (String.Format ("Build started {0}.", args.Timestamp));
                        WriteLine ("__________________________________________________");
                        buildStart = args.Timestamp;
@@ -115,11 +119,24 @@ namespace Microsoft.Build.BuildEngine {
                        }
                        if (performanceSummary == true) {
                        }
+
+                       if (errors.Count > 0) {
+                               WriteLine ("Errors:");
+                               foreach (string error in errors)
+                                       WriteLine (error);
+                       }
+
+                       if (warnings.Count > 0) {
+                               WriteLine ("Warnings:");
+                               foreach (string warning in warnings)
+                                       WriteLine (warning);
+                       }
+
                        if (showSummary == true){
                                TimeSpan timeElapsed = args.Timestamp - buildStart;
                                WriteLine (String.Format ("\t {0} Warning(s)", warningCount));
                                WriteLine (String.Format ("\t {0} Error(s)", errorCount));
-                               WriteLine ("");
+                               WriteLine (String.Empty);
                                WriteLine (String.Format ("Time Elapsed {0}", timeElapsed));
                        } 
                }
@@ -127,14 +144,15 @@ namespace Microsoft.Build.BuildEngine {
                public void ProjectStartedHandler (object sender, ProjectStartedEventArgs args)
                {
                        WriteLine (String.Format ("Project \"{0}\" ({1} target(s)):", args.ProjectFile, args.TargetNames));
-                       WriteLine ("");
+                       WriteLine (String.Empty);
                }
                
                public void ProjectFinishedHandler (object sender, ProjectFinishedEventArgs args)
                {
-                       if (IsVerbosityGreaterOrEqual (LoggerVerbosity.Diagnostic)) {
-                               WriteLine (String.Format ("Done building project \"{0}\".", args.ProjectFile));
-                               WriteLine ("");
+                       if (IsVerbosityGreaterOrEqual (LoggerVerbosity.Normal)) {
+                               WriteLine (String.Format ("Done building project \"{0}\".{1}", args.ProjectFile,
+                                                       args.Succeeded ? String.Empty : "-- FAILED"));
+                               WriteLine (String.Empty);
                        }
                }
                
@@ -148,9 +166,12 @@ namespace Microsoft.Build.BuildEngine {
                {
                        indent--;
                        if (IsVerbosityGreaterOrEqual (LoggerVerbosity.Diagnostic))
-                               WriteLine (String.Format ("Done building target \"{0}\" in project \"{1}\".",
-                                       args.TargetName, args.ProjectFile));
-                       WriteLine ("");
+                               WriteLine (String.Format ("Done building target \"{0}\" in project \"{1}\".{2}",
+                                       args.TargetName, args.ProjectFile,
+                                       args.Succeeded ? String.Empty : "-- FAILED"));
+                       if (!args.Succeeded)
+                               errorCount++;
+                       WriteLine (String.Empty);
                }
                
                public void TaskStartedHandler (object sender, TaskStartedEventArgs args)
@@ -163,8 +184,14 @@ namespace Microsoft.Build.BuildEngine {
                public void TaskFinishedHandler (object sender, TaskFinishedEventArgs args)
                {
                        indent--;
-                       if (this.verbosity == LoggerVerbosity.Diagnostic)
-                               WriteLine (String.Format ("Done executing task \"{0}\"",args.TaskName));
+                       if (this.verbosity == LoggerVerbosity.Diagnostic) {
+                               if (args.Succeeded)
+                                       WriteLine (String.Format ("Done executing task \"{0}\"", args.TaskName));
+                               else {
+                                       WriteLine (String.Format ("Task \"{0}\" execution failed", args.TaskName));
+                                       errorCount++;
+                               }
+                       }
                }
                
                public void MessageHandler (object sender, BuildMessageEventArgs args)
@@ -176,15 +203,19 @@ namespace Microsoft.Build.BuildEngine {
                
                public void WarningHandler (object sender, BuildWarningEventArgs args)
                {
-                       if (IsVerbosityGreaterOrEqual (LoggerVerbosity.Normal)) 
-                               WriteLineWithoutIndent (FormatWarningEvent (args));
+                       string msg = FormatWarningEvent (args);
+                       if (IsVerbosityGreaterOrEqual (LoggerVerbosity.Normal))
+                               WriteLineWithoutIndent (msg);
+                       warnings.Add (msg);
                        warningCount++;
                }
                
                public void ErrorHandler (object sender, BuildErrorEventArgs args)
                {
+                       string msg = FormatErrorEvent (args);
                        if (IsVerbosityGreaterOrEqual (LoggerVerbosity.Minimal)) 
-                               WriteLineWithoutIndent (FormatErrorEvent (args));
+                               WriteLineWithoutIndent (msg);
+                       errors.Add (msg);
                        errorCount++;
                }
                
@@ -230,25 +261,47 @@ namespace Microsoft.Build.BuildEngine {
                public virtual void Shutdown ()
                {
                }
+
+               static bool InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
                
                private string FormatErrorEvent (BuildErrorEventArgs args)
                {
-                       // FIXME: show more complicated args
-                       if (args.LineNumber != 0 && args.ColumnNumber != 0) {
-                               return String.Format ("{0}({1},{2}): {3} error {4}: {5}", args.File, args.LineNumber, args.ColumnNumber,
-                                       args.Subcategory, args.Code, args.Message);
+                       // For some reason we get an 1-char empty string as Subcategory somtimes.
+                       string subprefix = args.Subcategory == null || args.Subcategory == "" || args.Subcategory == " " ? "" : " ";
+                       string subcat = subprefix == "" ? "" : args.Subcategory;
+                               
+                       if (args.LineNumber != 0){
+                               if (args.ColumnNumber != 0 && !InEmacs) 
+                                       return String.Format ("{0}({1},{2}): {3}{4}error {5}: {6}",
+                                                             args.File, args.LineNumber, args.ColumnNumber,
+                                                             subprefix, subcat, args.Code, args.Message);
+
+                               return String.Format ("{0}({1}): {2}{3}error {4}: {5}",
+                                                     args.File, args.LineNumber,
+                                                     subprefix, subcat, args.Code, args.Message);
                        } else {
-                               return String.Format ("{0}: {1} error {2}: {3}", args.File, args.Subcategory, args.Code,
+                               return String.Format ("{0}: {1}{2}error {3}: {4}", args.File, subprefix, subcat, args.Code,
                                        args.Message);
                        }
                }
 
                private string FormatWarningEvent (BuildWarningEventArgs args)
                {
+                       // For some reason we get an 1-char empty string as Subcategory somtimes.
+                       string subprefix = args.Subcategory == null || args.Subcategory == "" || args.Subcategory == " " ? "" : " ";
+                       string subcat = subprefix == "" ? "" : args.Subcategory;
+
                        // FIXME: show more complicated args
-                       if (args.LineNumber != 0 && args.ColumnNumber != 0) {
-                               return String.Format ("{0}({1},{2}): {3} warning {4}: {5}", args.File, args.LineNumber, args.ColumnNumber,
-                                       args.Subcategory, args.Code, args.Message);
+                       if (args.LineNumber != 0){
+
+                               if (args.ColumnNumber != 0 && !InEmacs) {
+                                       return String.Format ("{0}({1},{2}): {3}{4}warning {5}: {6}",
+                                                             args.File, args.LineNumber, args.ColumnNumber,
+                                                             subprefix, subcat, args.Code, args.Message);
+                               }
+                               return String.Format ("{0}({1}): {2}{3}warning {4}: {5}",
+                                                     args.File, args.LineNumber,
+                                                     subprefix, subcat, args.Code, args.Message);
                        } else {
                                return String.Format ("{0}: {1} warning {2}: {3}", args.File, args.Subcategory, args.Code,
                                        args.Message);