Merge pull request #5714 from alexischr/update_bockbuild
[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 using System;
29 using System.IO;
30 using System.Text;
31 using Microsoft.Build.Framework;
32
33 namespace Microsoft.Build.BuildEngine
34 {
35         static class LogExtensions
36         {
37                 public static string FormatString (string unformatted,
38                                                    params object[] args)
39                 {
40                         if (unformatted == null)
41                                 throw new ArgumentNullException ("unformatted");
42                 
43                         if (args == null || args.Length == 0)
44                                 return unformatted;
45                         else
46                                 return String.Format (unformatted, args);
47                 }
48
49                 public static void LogError (this Engine engine, string message,
50                                      params object[] messageArgs)
51                 {
52                         engine.LogErrorWithFilename (null, message, messageArgs);
53                 }
54
55                 public static void LogErrorWithFilename (this Engine engine, string filename, string message,
56                                      params object[] messageArgs)
57                 {
58                         if (message == null)
59                                 throw new ArgumentNullException ("message");
60                                 
61                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
62                                 null, null, filename, 0, 0, 0, 0, FormatString (message, messageArgs),
63                                 null, null);
64                         engine.EventSource.FireErrorRaised (engine, beea);
65                 }
66
67                 public static void LogError (this Engine engine, string subcategory, string errorCode,
68                                       string helpKeyword, string file,
69                                       int lineNumber, int columnNumber,
70                                       int endLineNumber, int endColumnNumber,
71                                       string message,
72                                       params object[] messageArgs)
73                 {
74                         if (message == null)
75                                 throw new ArgumentNullException ("message");
76                         
77                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
78                                 subcategory, errorCode, file, lineNumber,
79                                 columnNumber, endLineNumber, endColumnNumber,
80                                 FormatString (message, messageArgs), helpKeyword /*it's helpKeyword*/,
81                                 null /*it's senderName*/);
82
83                         engine.EventSource.FireErrorRaised (engine, beea);
84                 }
85
86                 public static void LogErrorFromException (this Engine engine, Exception e)
87                 {
88                         LogErrorFromException (engine, e, true);
89                 }
90
91                 public static void LogErrorFromException (this Engine engine, Exception e,
92                                                    bool showStackTrace)
93                 {
94                         LogErrorFromException (engine, e, showStackTrace, true, String.Empty);
95                 }
96
97                 [MonoTODO ("Arguments @showDetail and @file are not honored")]
98                 public static void LogErrorFromException (this Engine engine, Exception e,
99                                                    bool showStackTrace, bool showDetail, string file)
100                 {
101                         if (e == null)
102                                 throw new ArgumentNullException ("e");
103                 
104                         StringBuilder sb = new StringBuilder ();
105                         sb.Append (e.Message);
106                         if (showStackTrace == true)
107                                 sb.Append (e.StackTrace);
108                         BuildErrorEventArgs beea = new BuildErrorEventArgs (
109                                 null, null, null, 0, 0, 0, 0, sb.ToString (),
110                                 e.HelpLink, e.Source);
111                         engine.EventSource.FireErrorRaised (engine, beea);
112                 }
113
114                 public static void LogMessage (this Engine engine, string message,
115                                        params object[] messageArgs)
116                 {
117                         LogMessage (engine, MessageImportance.Normal, message, messageArgs); 
118                 }
119
120                 public static void LogMessage (this Engine engine, MessageImportance importance,
121                                         string message,
122                                         params object[] messageArgs)
123                 {
124                         if (message == null)
125                                 throw new ArgumentNullException ("message");
126                 
127                         LogMessageFromText (engine, FormatString (message, messageArgs), importance);
128                 }
129
130                 public static bool LogMessageFromText (this Engine engine, string lineOfText,
131                                                 MessageImportance importance)
132                 {
133                         if (lineOfText == null)
134                                 throw new ArgumentNullException ("lineOfText");
135
136                         BuildMessageEventArgs bmea = new BuildMessageEventArgs (
137                                 lineOfText, null,
138                                 null, importance);
139                         
140                         engine.EventSource.FireMessageRaised (engine, bmea);
141
142                         return true;
143                 }
144
145                 public static void LogWarning (this Engine engine, string message,
146                                        params object[] messageArgs)
147                 {
148                         // FIXME: what about all the parameters?
149                         BuildWarningEventArgs bwea = new BuildWarningEventArgs (
150                                 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
151                                 null, null);
152                         engine.EventSource.FireWarningRaised (engine, bwea);
153                 }
154
155                 public static void LogWarning (this Engine engine, string subcategory, string warningCode,
156                                         string helpKeyword, string file,
157                                         int lineNumber, int columnNumber,
158                                         int endLineNumber, int endColumnNumber,
159                                         string message,
160                                         params object[] messageArgs)
161                 {
162                         BuildWarningEventArgs bwea = new BuildWarningEventArgs (
163                                 subcategory, warningCode, file, lineNumber,
164                                 columnNumber, endLineNumber, endColumnNumber,
165                                 FormatString (message, messageArgs), helpKeyword, null);
166                         engine.EventSource.FireWarningRaised (engine, bwea);
167                 }
168
169                 public static void LogWarningFromException (this Engine engine, Exception e)
170                 {
171                         LogWarningFromException (engine, e, false);
172                 }
173
174                 public static void LogWarningFromException (this Engine engine, Exception e,
175                                                      bool showStackTrace)
176                 {
177                         StringBuilder sb = new StringBuilder ();
178                         sb.Append (e.Message);
179                         if (showStackTrace)
180                                 sb.Append (e.StackTrace);
181                         LogWarning (engine, null, null, null, null, 0, 0, 0, 0,
182                                 sb.ToString (), null);
183                 }
184         }
185 }