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.
31 using Microsoft.Build.Framework;
33 namespace Microsoft.Build.BuildEngine
35 static class LogExtensions
37 public static string FormatString (string unformatted,
40 if (unformatted == null)
41 throw new ArgumentNullException ("unformatted");
43 if (args == null || args.Length == 0)
46 return String.Format (unformatted, args);
49 public static void LogError (this Engine engine, string message,
50 params object[] messageArgs)
52 engine.LogErrorWithFilename (null, message, messageArgs);
55 public static void LogErrorWithFilename (this Engine engine, string filename, string message,
56 params object[] messageArgs)
59 throw new ArgumentNullException ("message");
61 BuildErrorEventArgs beea = new BuildErrorEventArgs (
62 null, null, filename, 0, 0, 0, 0, FormatString (message, messageArgs),
64 engine.EventSource.FireErrorRaised (engine, beea);
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,
72 params object[] messageArgs)
75 throw new ArgumentNullException ("message");
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*/);
83 engine.EventSource.FireErrorRaised (engine, beea);
86 public static void LogErrorFromException (this Engine engine, Exception e)
88 LogErrorFromException (engine, e, true);
91 public static void LogErrorFromException (this Engine engine, Exception e,
94 LogErrorFromException (engine, e, showStackTrace, true, String.Empty);
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)
102 throw new ArgumentNullException ("e");
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);
114 public static void LogMessage (this Engine engine, string message,
115 params object[] messageArgs)
117 LogMessage (engine, MessageImportance.Normal, message, messageArgs);
120 public static void LogMessage (this Engine engine, MessageImportance importance,
122 params object[] messageArgs)
125 throw new ArgumentNullException ("message");
127 LogMessageFromText (engine, FormatString (message, messageArgs), importance);
130 public static bool LogMessageFromText (this Engine engine, string lineOfText,
131 MessageImportance importance)
133 if (lineOfText == null)
134 throw new ArgumentNullException ("lineOfText");
136 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
140 engine.EventSource.FireMessageRaised (engine, bmea);
145 public static void LogWarning (this Engine engine, string message,
146 params object[] messageArgs)
148 // FIXME: what about all the parameters?
149 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
150 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
152 engine.EventSource.FireWarningRaised (engine, bwea);
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,
160 params object[] messageArgs)
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);
169 public static void LogWarningFromException (this Engine engine, Exception e)
171 LogWarningFromException (engine, e, false);
174 public static void LogWarningFromException (this Engine engine, Exception e,
177 StringBuilder sb = new StringBuilder ();
178 sb.Append (e.Message);
180 sb.Append (e.StackTrace);
181 LogWarning (engine, null, null, null, null, 0, 0, 0, 0,
182 sb.ToString (), null);