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.
31 using System.Resources;
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.Utilities
37 public class TaskLoggingHelper : MarshalByRefObject
39 IBuildEngine buildEngine;
41 string helpKeywordPrefix;
43 ResourceManager taskResources;
45 public TaskLoggingHelper (ITask taskInstance)
47 if (taskInstance != null)
48 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");
80 if (args == null || args.Length == 0)
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, buildEngine.ProjectFileOfTaskNode, 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 exception)
131 LogErrorFromException (exception, true);
134 public void LogErrorFromException (Exception exception,
137 LogErrorFromException (exception, showStackTrace, true, String.Empty);
140 [MonoTODO ("Arguments @showDetail and @file are not honored")]
141 public void LogErrorFromException (Exception exception,
142 bool showStackTrace, bool showDetail, string file)
144 if (exception == null)
145 throw new ArgumentNullException ("exception");
147 StringBuilder sb = new StringBuilder ();
148 sb.Append (exception.Message);
149 if (showStackTrace == true)
150 sb.Append (exception.StackTrace);
151 BuildErrorEventArgs beea = new BuildErrorEventArgs (
152 null, null, buildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, sb.ToString (),
153 exception.HelpLink, exception.Source);
154 buildEngine.LogErrorEvent (beea);
155 hasLoggedErrors = true;
158 public void LogErrorFromResources (string messageResourceName,
159 params object[] messageArgs)
161 LogErrorFromResources (null, null, null, null, 0, 0, 0,
162 0, messageResourceName, null);
165 public void LogErrorFromResources (string subcategoryResourceName,
168 string file, int lineNumber,
172 string messageResourceName,
173 params object[] messageArgs)
175 BuildErrorEventArgs beea = new BuildErrorEventArgs (
176 taskResources.GetString (subcategoryResourceName),
177 errorCode, file, lineNumber, columnNumber,
178 endLineNumber, endColumnNumber,
179 taskResources.GetString (messageResourceName),
181 buildEngine.LogErrorEvent (beea);
182 hasLoggedErrors = true;
185 public void LogErrorWithCodeFromResources (string messageResourceName,
186 params object[] messageArgs)
188 // FIXME: there should be something different than normal
189 // LogErrorFromResources
190 LogErrorFromResources (messageResourceName, messageArgs);
193 public void LogErrorWithCodeFromResources (string subcategoryResourceName,
199 string messageResourceName,
200 params object[] messageArgs)
202 // FIXME: there should be something different than normal
203 // LogErrorFromResources
204 LogErrorFromResources (subcategoryResourceName, file,
205 lineNumber, columnNumber, endLineNumber,
206 endColumnNumber, messageResourceName,
210 public void LogMessage (string message,
211 params object[] messageArgs)
213 LogMessage (MessageImportance.Normal, message, messageArgs);
216 public void LogMessage (MessageImportance importance,
218 params object[] messageArgs)
221 throw new ArgumentNullException ("message");
223 LogMessageFromText (FormatString (message, messageArgs), importance);
226 public void LogMessageFromResources (string messageResourceName,
227 params object[] messageArgs)
229 LogMessage (taskResources.GetString (messageResourceName),
233 public void LogMessageFromResources (MessageImportance importance,
234 string messageResourceName,
235 params object[] messageArgs)
237 LogMessage (importance, taskResources.GetString (
238 messageResourceName), messageArgs);
241 public bool LogMessagesFromFile (string fileName)
243 return LogMessagesFromFile (fileName, MessageImportance.Normal);
246 public bool LogMessagesFromFile (string fileName,
247 MessageImportance messageImportance)
250 StreamReader sr = new StreamReader (fileName);
251 LogMessage (messageImportance, sr.ReadToEnd (),
261 public bool LogMessagesFromStream (TextReader stream,
262 MessageImportance messageImportance)
265 LogMessage (messageImportance, stream.ReadToEnd (), null);
272 // FIXME: should it be done here?
277 public bool LogMessageFromText (string lineOfText,
278 MessageImportance messageImportance)
280 if (lineOfText == null)
281 throw new ArgumentNullException ("lineOfText");
283 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
284 lineOfText, helpKeywordPrefix,
285 null, messageImportance);
286 buildEngine.LogMessageEvent (bmea);
291 public void LogWarning (string message,
292 params object[] messageArgs)
294 // FIXME: what about all the parameters?
295 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
296 null, null, buildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, FormatString (message, messageArgs),
297 helpKeywordPrefix, null);
298 buildEngine.LogWarningEvent (bwea);
301 public void LogWarning (string subcategory, string warningCode,
302 string helpKeyword, string file,
303 int lineNumber, int columnNumber,
304 int endLineNumber, int endColumnNumber,
306 params object[] messageArgs)
308 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
309 subcategory, warningCode, file, lineNumber,
310 columnNumber, endLineNumber, endColumnNumber,
311 FormatString (message, messageArgs), helpKeyword, null);
312 buildEngine.LogWarningEvent (bwea);
315 public void LogWarningFromException (Exception exception)
317 LogWarningFromException (exception, false);
320 public void LogWarningFromException (Exception exception,
323 StringBuilder sb = new StringBuilder ();
324 sb.Append (exception.Message);
326 sb.Append (exception.StackTrace);
327 LogWarning (null, null, null, null, 0, 0, 0, 0,
328 sb.ToString (), null);
331 public void LogWarningFromResources (string messageResourceName,
332 params object[] messageArgs)
334 LogWarning (taskResources.GetString (messageResourceName),
338 public void LogWarningFromResources (string subcategoryResourceName,
346 string messageResourceName,
347 params object[] messageArgs)
349 LogWarning (taskResources.GetString (subcategoryResourceName),
350 warningCode, helpKeyword, file, lineNumber,
351 columnNumber, endLineNumber, endColumnNumber,
352 taskResources.GetString (messageResourceName),
356 public void LogWarningWithCodeFromResources (string messageResourceName,
357 params object[] messageArgs)
359 // FIXME: what's different from normal logwarning?
360 LogWarningFromResources (messageResourceName, messageArgs);
363 public void LogWarningWithCodeFromResources (string subcategoryResourceName,
369 string messageResourceName,
370 params object[] messageArgs)
372 LogWarningFromResources (subcategoryResourceName, file,
373 lineNumber, columnNumber, endLineNumber,
374 endColumnNumber, messageResourceName,
379 public void LogExternalProjectFinished (string message,
387 public void LogExternalProjectStarted (string message,
394 protected IBuildEngine BuildEngine {
400 public bool HasLoggedErrors {
402 return hasLoggedErrors;
406 public string HelpKeywordPrefix {
408 return helpKeywordPrefix;
411 helpKeywordPrefix = value;
415 protected string TaskName {
421 public ResourceManager TaskResources {
423 return taskResources;
426 taskResources = value;