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)
55 throw new ArgumentNullException ("message");
57 BuildErrorEventArgs beea = new BuildErrorEventArgs (
58 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
60 engine.EventSource.FireErrorRaised (engine, beea);
63 public static void LogError (this Engine engine, string subcategory, string errorCode,
64 string helpKeyword, string file,
65 int lineNumber, int columnNumber,
66 int endLineNumber, int endColumnNumber,
68 params object[] messageArgs)
71 throw new ArgumentNullException ("message");
73 BuildErrorEventArgs beea = new BuildErrorEventArgs (
74 subcategory, errorCode, file, lineNumber,
75 columnNumber, endLineNumber, endColumnNumber,
76 FormatString (message, messageArgs), helpKeyword /*it's helpKeyword*/,
77 null /*it's senderName*/);
79 engine.EventSource.FireErrorRaised (engine, beea);
82 public static void LogErrorFromException (this Engine engine, Exception e)
84 LogErrorFromException (engine, e, true);
87 public static void LogErrorFromException (this Engine engine, Exception e,
90 LogErrorFromException (engine, e, showStackTrace, true, String.Empty);
93 [MonoTODO ("Arguments @showDetail and @file are not honored")]
94 public static void LogErrorFromException (this Engine engine, Exception e,
95 bool showStackTrace, bool showDetail, string file)
98 throw new ArgumentNullException ("e");
100 StringBuilder sb = new StringBuilder ();
101 sb.Append (e.Message);
102 if (showStackTrace == true)
103 sb.Append (e.StackTrace);
104 BuildErrorEventArgs beea = new BuildErrorEventArgs (
105 null, null, null, 0, 0, 0, 0, sb.ToString (),
106 e.HelpLink, e.Source);
107 engine.EventSource.FireErrorRaised (engine, beea);
110 public static void LogMessage (this Engine engine, string message,
111 params object[] messageArgs)
113 LogMessage (engine, MessageImportance.Normal, message, messageArgs);
116 public static void LogMessage (this Engine engine, MessageImportance importance,
118 params object[] messageArgs)
121 throw new ArgumentNullException ("message");
123 LogMessageFromText (engine, FormatString (message, messageArgs), importance);
126 public static bool LogMessageFromText (this Engine engine, string lineOfText,
127 MessageImportance importance)
129 if (lineOfText == null)
130 throw new ArgumentNullException ("lineOfText");
132 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
136 engine.EventSource.FireMessageRaised (engine, bmea);
141 public static void LogWarning (this Engine engine, string message,
142 params object[] messageArgs)
144 // FIXME: what about all the parameters?
145 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
146 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
148 engine.EventSource.FireWarningRaised (engine, bwea);
151 public static void LogWarning (this Engine engine, string subcategory, string warningCode,
152 string helpKeyword, string file,
153 int lineNumber, int columnNumber,
154 int endLineNumber, int endColumnNumber,
156 params object[] messageArgs)
158 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
159 subcategory, warningCode, file, lineNumber,
160 columnNumber, endLineNumber, endColumnNumber,
161 FormatString (message, messageArgs), helpKeyword, null);
162 engine.EventSource.FireWarningRaised (engine, bwea);
165 public static void LogWarningFromException (this Engine engine, Exception e)
167 LogWarningFromException (engine, e, false);
170 public static void LogWarningFromException (this Engine engine, Exception e,
173 StringBuilder sb = new StringBuilder ();
174 sb.Append (e.Message);
176 sb.Append (e.StackTrace);
177 LogWarning (engine, null, null, null, null, 0, 0, 0, 0,
178 sb.ToString (), null);