2 // LogExtensions.cs: Extension methods for logging on Engine
5 // Ankit Jain (jankit@novell.com)
7 // Copyright 2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.BuildEngine
37 static class LogExtensions
39 public static string FormatString (string unformatted,
42 if (unformatted == null)
43 throw new ArgumentNullException ("unformatted");
45 if (args == null || args.Length == 0)
48 return String.Format (unformatted, args);
51 public static void LogError (this Engine engine, string message,
52 params object[] messageArgs)
54 engine.LogError (null, message, messageArgs);
57 public static void LogError (this Engine engine, string filename, string message,
58 params object[] messageArgs)
61 throw new ArgumentNullException ("message");
63 BuildErrorEventArgs beea = new BuildErrorEventArgs (
64 null, null, filename, 0, 0, 0, 0, FormatString (message, messageArgs),
66 engine.EventSource.FireErrorRaised (engine, beea);
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,
74 params object[] messageArgs)
77 throw new ArgumentNullException ("message");
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*/);
85 engine.EventSource.FireErrorRaised (engine, beea);
88 public static void LogErrorFromException (this Engine engine, Exception e)
90 LogErrorFromException (engine, e, true);
93 public static void LogErrorFromException (this Engine engine, Exception e,
96 LogErrorFromException (engine, e, showStackTrace, true, String.Empty);
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)
104 throw new ArgumentNullException ("e");
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);
116 public static void LogMessage (this Engine engine, string message,
117 params object[] messageArgs)
119 LogMessage (engine, MessageImportance.Normal, message, messageArgs);
122 public static void LogMessage (this Engine engine, MessageImportance importance,
124 params object[] messageArgs)
127 throw new ArgumentNullException ("message");
129 LogMessageFromText (engine, FormatString (message, messageArgs), importance);
132 public static bool LogMessageFromText (this Engine engine, string lineOfText,
133 MessageImportance importance)
135 if (lineOfText == null)
136 throw new ArgumentNullException ("lineOfText");
138 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
142 engine.EventSource.FireMessageRaised (engine, bmea);
147 public static void LogWarning (this Engine engine, string message,
148 params object[] messageArgs)
150 // FIXME: what about all the parameters?
151 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
152 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
154 engine.EventSource.FireWarningRaised (engine, bwea);
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,
162 params object[] messageArgs)
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);
171 public static void LogWarningFromException (this Engine engine, Exception e)
173 LogWarningFromException (engine, e, false);
176 public static void LogWarningFromException (this Engine engine, Exception e,
179 StringBuilder sb = new StringBuilder ();
180 sb.Append (e.Message);
182 sb.Append (e.StackTrace);
183 LogWarning (engine, null, null, null, null, 0, 0, 0, 0,
184 sb.ToString (), null);