Add missing ProjectPropertyGroupTaskIntance and descendant, and implement more of...
[mono.git] / mcs / class / Microsoft.Build / Microsoft.Build.Logging / ConsoleLogger.cs
1 // ConsoleLogger.cs
2 //
3 // Author:
4 //   Rolf Bjarne Kvinge (rolf@xamarin.com)
5 //
6 // Copyright (C) 2011 Xamarin Inc.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27
28 using System;
29 using System.Globalization;
30 using Microsoft.Build.Framework;
31
32 namespace Microsoft.Build.Logging
33 {
34         public class ConsoleLogger : INodeLogger
35         {
36                 public ConsoleLogger ()
37                         : this (LoggerVerbosity.Normal)
38                 {
39                 }
40
41                 public ConsoleLogger (LoggerVerbosity verbosity)
42                         : this (verbosity, message => Console.WriteLine (message), color => Console.ForegroundColor = color, Console.ResetColor)
43                 {
44                 }
45
46                 public ConsoleLogger (LoggerVerbosity verbosity, WriteHandler write, ColorSetter colorSet, ColorResetter colorReset)
47                 {
48                         if (write == null)
49                                 throw new ArgumentNullException ("write");
50                         if (colorSet == null)
51                                 throw new ArgumentNullException ("colorSet");
52                         if (colorReset == null)
53                                 throw new ArgumentNullException ("colorReset");
54                         Verbosity = verbosity;
55                         this.write = write;
56                         set_color = colorSet;
57                         reset_color = colorReset;
58                 }
59
60                 WriteHandler write;
61                 ColorSetter set_color;
62                 ColorResetter reset_color;
63
64                 #region INodeLogger implementation
65
66                 public virtual void Initialize (IEventSource eventSource, int nodeCount)
67                 {
68                         throw new NotImplementedException ();
69                 }
70
71                 #endregion
72
73                 #region ILogger implementation
74
75                 public virtual void Initialize (IEventSource eventSource)
76                 {
77                         throw new NotImplementedException ();
78                 }
79
80                 public virtual void Shutdown ()
81                 {
82                         throw new NotImplementedException ();
83                 }
84
85                 public string Parameters { get; set; }
86
87                 public LoggerVerbosity Verbosity { get; set; }
88
89                 public bool ShowSummary { get; set; }
90
91                 public bool SkipProjectStartedText { get; set; }
92                 
93                 public WriteHandler WriteHandler {
94                         get { return write; }
95                         set {
96                                 if (value == null)
97                                         throw new ArgumentNullException ("value");
98                                 write = value;
99                         }
100                 }
101
102                 #endregion
103
104                 public void ApplyParameter (string parameterName, string parameterValue)
105                 {
106                         throw new NotImplementedException ();
107                 }
108
109                 DateTime build_started;
110
111                 public void BuildFinishedHandler (object sender, BuildFinishedEventArgs e)
112                 {
113                         if (Verbosity == LoggerVerbosity.Quiet || Verbosity == LoggerVerbosity.Minimal)
114                                 return;
115
116                         set_color (ConsoleColor.White);
117                         write (e.Message);
118                         write ("");
119                         write ("");
120                         write ("");
121                         // .NET doesn't care if BuildStarted is actually invoked.
122                         write (string.Format ("Time Elapsed {0}", (e.Timestamp - build_started).ToString ("hh\\:mm\\:ss\\.ff")));
123                         write ("");
124                         reset_color ();
125                 }
126
127                 public void BuildStartedHandler (object sender, BuildStartedEventArgs e)
128                 {
129                         if (Verbosity == LoggerVerbosity.Quiet || Verbosity == LoggerVerbosity.Minimal)
130                                 return;
131
132                         build_started = e.Timestamp;
133                         set_color (ConsoleColor.White);
134                         write (string.Format ("Build started {0}.", e.Timestamp.ToString ("yyyy/MM/dd HH:mm:ss")));
135                         write ("");
136                         reset_color ();
137                 }
138
139                 public void CustomEventHandler (object sender, CustomBuildEventArgs e)
140                 {
141                         // nothing happens.
142                 }
143
144                 string GetLocation (int lineNumber, int columnNumber, int endLineNumber, int endColumnNumber)
145                 {
146                         string line = null, col = null;
147                         if (lineNumber > 0) {
148                                 if (endLineNumber > 0)
149                                         line = string.Format ("{0}-{1}", lineNumber, endLineNumber);
150                                 else
151                                         line = lineNumber.ToString ();
152                         }
153                         if (columnNumber > 0) {
154                                 if (endColumnNumber > 0)
155                                         col = string.Format ("{0}-{1}", columnNumber, endColumnNumber);
156                                 else
157                                         col = columnNumber.ToString ();
158                         }
159                         string loc = line != null ? line + (col != null ? "," + col : null) : null;
160                         return string.IsNullOrEmpty (loc) ? string.Empty : '(' + loc + ')';
161                 }
162                 
163                 public void ErrorHandler (object sender, BuildErrorEventArgs e)
164                 {
165                         if (Verbosity == LoggerVerbosity.Quiet)
166                                 return;
167
168                         set_color (ConsoleColor.Red);
169                         string loc = GetLocation (e.LineNumber, e.ColumnNumber, e.EndLineNumber, e.EndColumnNumber);
170                         write (string.Format ("{0}{1} : {2} error {3}: {4}", e.File, loc, e.Subcategory, e.Code, e.Message));
171                         write ("");
172                         reset_color ();
173                 }
174                 
175                 public void MessageHandler (object sender, BuildMessageEventArgs e)
176                 {
177                         switch (e.Importance) {
178                         case MessageImportance.High:
179                                 set_color (ConsoleColor.White);
180                                 break;
181                         case MessageImportance.Low:
182                         case MessageImportance.Normal:
183                                 set_color (ConsoleColor.Gray);
184                                 break;
185                         }
186                         write (e.Message);
187                         reset_color ();
188                 }
189
190                 public void ProjectFinishedHandler (object sender, ProjectFinishedEventArgs e)
191                 {
192                         if (Verbosity == LoggerVerbosity.Quiet || Verbosity == LoggerVerbosity.Minimal)
193                                 return;
194
195                         set_color (ConsoleColor.Cyan);
196                         write (e.Message);
197                         write ("");
198                         write ("");
199                         write ("");
200                         reset_color ();
201                 }
202                 
203                 public void ProjectStartedHandler (object sender, ProjectStartedEventArgs e)
204                 {
205                         if (Verbosity == LoggerVerbosity.Quiet || Verbosity == LoggerVerbosity.Minimal)
206                                 return;
207
208                         set_color (ConsoleColor.Cyan);
209                         write ("__________________________________________________");
210                         write ("");
211                         write (string.Format ("Project \"{0}\" ({1} target(s)):", e.ProjectFile, e.TargetNames));
212                         write ("");
213                         write ("");
214                         write ("");
215                         reset_color ();
216                 }
217                 
218                 public void TargetFinishedHandler (object sender, TargetFinishedEventArgs e)
219                 {
220                         if (Verbosity != LoggerVerbosity.Detailed && Verbosity != LoggerVerbosity.Diagnostic)
221                                 return;
222
223                         set_color (ConsoleColor.Cyan);
224                         write (e.Message);
225                         write ("");
226                         write ("");
227                         write ("");
228                         reset_color ();
229                 }
230
231                 public void TargetStartedHandler (object sender, TargetStartedEventArgs e)
232                 {
233                         if (Verbosity != LoggerVerbosity.Detailed && Verbosity != LoggerVerbosity.Diagnostic)
234                                 return;
235
236                         string message = Verbosity == LoggerVerbosity.Detailed ?
237                                 string.Format ("Target \"{0}\":", e.TargetName) :
238                                 string.Format ("Target \"{0}\" in file \"{1}\":", e.TargetName, e.TargetFile);
239
240                         set_color (ConsoleColor.Cyan);
241                         write (message);
242                         write ("");
243                         reset_color ();
244                 }
245                 
246                 public void TaskFinishedHandler (object sender, TaskFinishedEventArgs e)
247                 {
248                         if (Verbosity != LoggerVerbosity.Detailed && Verbosity != LoggerVerbosity.Diagnostic)
249                                 return;
250
251                         set_color (ConsoleColor.Cyan);
252                         write ("  " + e.Message);
253                         write ("");
254                         reset_color ();
255                 }
256
257                 public void TaskStartedHandler (object sender, TaskStartedEventArgs e)
258                 {
259                         if (Verbosity != LoggerVerbosity.Detailed && Verbosity != LoggerVerbosity.Diagnostic)
260                                 return;
261
262                         set_color (ConsoleColor.Cyan);
263                         write ("  " + e.Message);
264                         write ("");
265                         reset_color ();
266                 }
267                 
268                 public void WarningHandler (object sender, BuildWarningEventArgs e)
269                 {
270                         if (Verbosity == LoggerVerbosity.Quiet)
271                                 return;
272
273                         set_color (ConsoleColor.Yellow);
274                         string loc = GetLocation (e.LineNumber, e.ColumnNumber, e.EndLineNumber, e.EndColumnNumber);
275                         write (string.Format ("{0}{1} : {2} warning {3}: {4}", e.File, loc, e.Subcategory, e.Code, e.Message));
276                         write ("");
277                         reset_color ();
278                 }
279         }
280 }
281