2 // TaskLoggingHelper.cs: Wrapper aroudn IBuildEngine.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
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.
32 using System.Resources;
34 using Microsoft.Build.Framework;
36 namespace Microsoft.Build.Utilities
38 public class TaskLoggingHelper : MarshalByRefObject
40 IBuildEngine buildEngine;
42 string helpKeywordPrefix;
44 ResourceManager taskResources;
46 public TaskLoggingHelper (ITask taskInstance)
48 if (taskInstance != null)
49 this.buildEngine = taskInstance.BuildEngine;
53 public string ExtractMessageCode (string message,
54 out string messageWithoutCodePrefix)
57 throw new ArgumentNullException ("message");
59 messageWithoutCodePrefix = String.Empty;
64 public virtual string FormatResourceString (string resourceName,
67 if (resourceName == null)
68 throw new ArgumentNullException ("resourceName");
74 public virtual string FormatString (string unformatted,
77 if (unformatted == null)
78 throw new ArgumentNullException ("unformatted");
83 return String.Format (unformatted, args);
87 public void LogCommandLine (string commandLine)
92 public void LogCommandLine (MessageImportance importance,
97 public void LogError (string message,
98 params object[] messageArgs)
101 throw new ArgumentNullException ("message");
103 BuildErrorEventArgs beea = new BuildErrorEventArgs (
104 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
105 helpKeywordPrefix, null);
106 buildEngine.LogErrorEvent (beea);
107 hasLoggedErrors = true;
110 public void LogError (string subcategory, string errorCode,
111 string helpKeyword, string file,
112 int lineNumber, int columnNumber,
113 int endLineNumber, int endColumnNumber,
115 params object[] messageArgs)
118 throw new ArgumentNullException ("message");
120 BuildErrorEventArgs beea = new BuildErrorEventArgs (
121 subcategory, errorCode, file, lineNumber,
122 columnNumber, endLineNumber, endColumnNumber,
123 FormatString (message, messageArgs), helpKeyword /*it's helpKeyword*/,
124 null /*it's senderName*/);
125 buildEngine.LogErrorEvent (beea);
126 hasLoggedErrors = true;
129 public void LogErrorFromException (Exception e)
131 LogErrorFromException (e, true);
134 public void LogErrorFromException (Exception e,
138 throw new ArgumentNullException ("e");
140 StringBuilder sb = new StringBuilder ();
141 sb.Append (e.Message);
142 if (showStackTrace == true)
143 sb.Append (e.StackTrace);
144 BuildErrorEventArgs beea = new BuildErrorEventArgs (
145 null, null, null, 0, 0, 0, 0, sb.ToString (),
146 e.HelpLink, e.Source);
147 buildEngine.LogErrorEvent (beea);
148 hasLoggedErrors = true;
151 public void LogErrorFromResources (string messageResourceName,
152 params object[] messageArgs)
154 LogErrorFromResources (null, null, null, null, 0, 0, 0,
155 0, messageResourceName, null);
158 public void LogErrorFromResources (string subcategoryResourceName,
161 string file, int lineNumber,
165 string messageResourceName,
166 params object[] messageArgs)
168 BuildErrorEventArgs beea = new BuildErrorEventArgs (
169 taskResources.GetString (subcategoryResourceName),
170 errorCode, file, lineNumber, columnNumber,
171 endLineNumber, endColumnNumber,
172 taskResources.GetString (messageResourceName),
174 buildEngine.LogErrorEvent (beea);
175 hasLoggedErrors = true;
178 public void LogErrorWithCodeFromResources (string messageResourceName,
179 params object[] messageArgs)
181 // FIXME: there should be something different than normal
182 // LogErrorFromResources
183 LogErrorFromResources (messageResourceName, messageArgs);
186 public void LogErrorWithCodeFromResources (string subcategoryResourceName,
192 string messageResourceName,
193 params object[] messageArgs)
195 // FIXME: there should be something different than normal
196 // LogErrorFromResources
197 LogErrorFromResources (subcategoryResourceName, file,
198 lineNumber, columnNumber, endLineNumber,
199 endColumnNumber, messageResourceName,
203 public void LogMessage (string message,
204 params object[] messageArgs)
206 LogMessage (MessageImportance.Normal, message, messageArgs);
209 public void LogMessage (MessageImportance importance,
211 params object[] messageArgs)
214 throw new ArgumentNullException ("message");
216 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
217 FormatString (message, messageArgs), helpKeywordPrefix,
219 buildEngine.LogMessageEvent (bmea);
222 public void LogMessageFromResources (string messageResourceName,
223 params object[] messageArgs)
225 LogMessage (taskResources.GetString (messageResourceName),
229 public void LogMessageFromResources (MessageImportance importance,
230 string messageResourceName,
231 params object[] messageArgs)
233 LogMessage (importance, taskResources.GetString (
234 messageResourceName), messageArgs);
237 public bool LogMessagesFromFile (string filename)
239 return LogMessagesFromFile (filename, MessageImportance.Normal);
242 public bool LogMessagesFromFile (string filename,
243 MessageImportance messageImportance)
246 StreamReader sr = new StreamReader (filename);
247 LogMessage (messageImportance, sr.ReadToEnd (),
257 public bool LogMessagesFromStream (TextReader stream,
258 MessageImportance messageImportance)
261 LogMessage (messageImportance, stream.ReadToEnd (), null);
268 // FIXME: should it be done here?
274 public bool LogMessageFromText (string lineOfText,
275 MessageImportance importance)
277 if (lineOfText == null)
278 throw new ArgumentNullException ("lineOfText");
282 public void LogWarning (string message,
283 params object[] messageArgs)
285 // FIXME: what about all the parameters?
286 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
287 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
288 helpKeywordPrefix, null);
289 buildEngine.LogWarningEvent (bwea);
292 public void LogWarning (string subcategory, string warningCode,
293 string helpKeyword, string file,
294 int lineNumber, int columnNumber,
295 int endLineNumber, int endColumnNumber,
297 params object[] messageArgs)
299 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
300 subcategory, warningCode, file, lineNumber,
301 columnNumber, endLineNumber, endColumnNumber,
302 FormatString (message, messageArgs), helpKeyword, null);
303 buildEngine.LogWarningEvent (bwea);
306 public void LogWarningFromException (Exception e)
308 LogWarningFromException (e, false);
311 public void LogWarningFromException (Exception e,
314 StringBuilder sb = new StringBuilder ();
315 sb.Append (e.Message);
317 sb.Append (e.StackTrace);
318 LogWarning (null, null, null, null, 0, 0, 0, 0,
319 sb.ToString (), null);
322 public void LogWarningFromResources (string messageResourceName,
323 params object[] messageArgs)
325 LogWarning (taskResources.GetString (messageResourceName),
329 public void LogWarningFromResources (string subcategoryResourceName,
337 string messageResourceName,
338 params object[] messageArgs)
340 LogWarning (taskResources.GetString (subcategoryResourceName),
341 warningCode, helpKeyword, file, lineNumber,
342 columnNumber, endLineNumber, endColumnNumber,
343 taskResources.GetString (messageResourceName),
347 public void LogWarningWithCodeFromResources (string messageResourceName,
348 params object[] messageArgs)
350 // FIXME: what's different from normal logwarning?
351 LogWarningFromResources (messageResourceName, messageArgs);
354 public void LogWarningWithCodeFromResources (string subcategoryResourceName,
360 string messageResourceName,
361 params object[] messageArgs)
363 LogWarningFromResources (subcategoryResourceName, file,
364 lineNumber, columnNumber, endLineNumber,
365 endColumnNumber, messageResourceName,
370 public void LogExternalProjectFinished (string message,
378 public void LogExternalProjectStarted (string message,
385 protected IBuildEngine BuildEngine {
391 public bool HasLoggedErrors {
393 return hasLoggedErrors;
397 public string HelpKeywordPrefix {
399 return helpKeywordPrefix;
402 helpKeywordPrefix = value;
406 protected string TaskName {
412 public ResourceManager TaskResources {
414 return taskResources;
417 taskResources = value;