[xbuild] Log build errors and raise events even if a build fails.
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / LogExtensions.cs
1 //
2 // LogExtensions.cs: Extension methods for logging on Engine
3 //
4 // Author:
5 //      Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.BuildEngine
36 {
37         static class LogExtensions
38         {
39                 public static string FormatString (string unformatted,
40                                                    params object[] args)
41                 {
42                         if (unformatted == null)
43                                 throw new ArgumentNullException ("unformatted");
44                 
45                         if (args == null || args.Length == 0)
46                                 return unformatted;
47                         else
48                                 return String.Format (unformatted, args);
49                 }
50
51                 public static void LogError (this Engine engine, string message,
52                                      params object[] messageArgs)
53                 {
54                         engine.LogError (null, message, messageArgs);
55                 }
56
57                 public static void LogError (this Engine engine, string filename, string message,
58                                      params object[] messageArgs)
59                 {
60                         if (message == null)
61                                 throw new ArgumentNullException ("message");
62                                 
63                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
64                                 null, null, filename, 0, 0, 0, 0, FormatString (message, messageArgs),
65                                 null, null);
66                         engine.EventSource.FireErrorRaised (engine, beea);
67                 }
68
69                 public static void LogError (this Engine engine, string subcategory, string errorCode,
70                                       string helpKeyword, string file,
71                                       int lineNumber, int columnNumber,
72                                       int endLineNumber, int endColumnNumber,
73                                       string message,
74                                       params object[] messageArgs)
75                 {
76                         if (message == null)
77                                 throw new ArgumentNullException ("message");
78                         
79                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
80                                 subcategory, errorCode, file, lineNumber,
81                                 columnNumber, endLineNumber, endColumnNumber,
82                                 FormatString (message, messageArgs), helpKeyword /*it's helpKeyword*/,
83                                 null /*it's senderName*/);
84
85                         engine.EventSource.FireErrorRaised (engine, beea);
86                 }
87
88                 public static void LogErrorFromException (this Engine engine, Exception e)
89                 {
90                         LogErrorFromException (engine, e, true);
91                 }
92
93                 public static void LogErrorFromException (this Engine engine, Exception e,
94                                                    bool showStackTrace)
95                 {
96                         LogErrorFromException (engine, e, showStackTrace, true, String.Empty);
97                 }
98
99                 [MonoTODO ("Arguments @showDetail and @file are not honored")]
100                 public static void LogErrorFromException (this Engine engine, Exception e,
101                                                    bool showStackTrace, bool showDetail, string file)
102                 {
103                         if (e == null)
104                                 throw new ArgumentNullException ("e");
105                 
106                         StringBuilder sb = new StringBuilder ();
107                         sb.Append (e.Message);
108                         if (showStackTrace == true)
109                                 sb.Append (e.StackTrace);
110                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
111                                 null, null, null, 0, 0, 0, 0, sb.ToString (),
112                                 e.HelpLink, e.Source);
113                         engine.EventSource.FireErrorRaised (engine, beea);
114                 }
115
116                 public static void LogMessage (this Engine engine, string message,
117                                        params object[] messageArgs)
118                 {
119                         LogMessage (engine, MessageImportance.Normal, message, messageArgs); 
120                 }
121
122                 public static void LogMessage (this Engine engine, MessageImportance importance,
123                                         string message,
124                                         params object[] messageArgs)
125                 {
126                         if (message == null)
127                                 throw new ArgumentNullException ("message");
128                 
129                         LogMessageFromText (engine, FormatString (message, messageArgs), importance);
130                 }
131
132                 public static bool LogMessageFromText (this Engine engine, string lineOfText,
133                                                 MessageImportance importance)
134                 {
135                         if (lineOfText == null)
136                                 throw new ArgumentNullException ("lineOfText");
137
138                         BuildMessageEventArgs bmea = new BuildMessageEventArgs (
139                                 lineOfText, null,
140                                 null, importance);
141                         
142                         engine.EventSource.FireMessageRaised (engine, bmea);
143
144                         return true;
145                 }
146
147                 public static void LogWarning (this Engine engine, string message,
148                                        params object[] messageArgs)
149                 {
150                         // FIXME: what about all the parameters?
151                         BuildWarningEventArgs bwea = new BuildWarningEventArgs (
152                                 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
153                                 null, null);
154                         engine.EventSource.FireWarningRaised (engine, bwea);
155                 }
156
157                 public static void LogWarning (this Engine engine, string subcategory, string warningCode,
158                                         string helpKeyword, string file,
159                                         int lineNumber, int columnNumber,
160                                         int endLineNumber, int endColumnNumber,
161                                         string message,
162                                         params object[] messageArgs)
163                 {
164                         BuildWarningEventArgs bwea = new BuildWarningEventArgs (
165                                 subcategory, warningCode, file, lineNumber,
166                                 columnNumber, endLineNumber, endColumnNumber,
167                                 FormatString (message, messageArgs), helpKeyword, null);
168                         engine.EventSource.FireWarningRaised (engine, bwea);
169                 }
170
171                 public static void LogWarningFromException (this Engine engine, Exception e)
172                 {
173                         LogWarningFromException (engine, e, false);
174                 }
175
176                 public static void LogWarningFromException (this Engine engine, Exception e,
177                                                      bool showStackTrace)
178                 {
179                         StringBuilder sb = new StringBuilder ();
180                         sb.Append (e.Message);
181                         if (showStackTrace)
182                                 sb.Append (e.StackTrace);
183                         LogWarning (engine, null, null, null, null, 0, 0, 0, 0,
184                                 sb.ToString (), null);
185                 }
186         }
187 }
188
189 #endif