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;
46 public TaskLoggingHelper (ITask taskInstance)
48 if (taskInstance == null)
49 throw new ArgumentNullException ("taskInstance");
51 this.taskInstance = taskInstance;
56 public string ExtractMessageCode (string message,
57 out string messageWithoutCodePrefix)
60 throw new ArgumentNullException ("message");
62 messageWithoutCodePrefix = String.Empty;
67 public virtual string FormatResourceString (string resourceName,
70 if (resourceName == null)
71 throw new ArgumentNullException ("resourceName");
73 if (taskResources == null)
74 throw new InvalidOperationException ("Task did not register any task resources");
76 string resourceString = taskResources.GetString (resourceName);
77 if (resourceString == null)
78 throw new ArgumentException ($"No resource string found for resource named {resourceName}");
80 return FormatString (resourceString, args);
84 public virtual string FormatString (string unformatted,
87 if (unformatted == null)
88 throw new ArgumentNullException ("unformatted");
90 if (args == null || args.Length == 0)
93 return String.Format (unformatted, args);
97 public void LogCommandLine (string commandLine)
102 public void LogCommandLine (MessageImportance importance,
107 public void LogError (string message,
108 params object[] messageArgs)
111 throw new ArgumentNullException ("message");
113 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
115 BuildErrorEventArgs beea = new BuildErrorEventArgs (
116 null, null, BuildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, FormatString (message, messageArgs),
117 helpKeywordPrefix, null);
118 BuildEngine.LogErrorEvent (beea);
119 hasLoggedErrors = true;
122 public void LogError (string subcategory, string errorCode,
123 string helpKeyword, string file,
124 int lineNumber, int columnNumber,
125 int endLineNumber, int endColumnNumber,
127 params object[] messageArgs)
130 throw new ArgumentNullException ("message");
132 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
134 BuildErrorEventArgs beea = new BuildErrorEventArgs (
135 subcategory, errorCode, file, lineNumber,
136 columnNumber, endLineNumber, endColumnNumber,
137 FormatString (message, messageArgs), helpKeyword /*it's helpKeyword*/,
138 null /*it's senderName*/);
139 BuildEngine.LogErrorEvent (beea);
140 hasLoggedErrors = true;
143 public void LogErrorFromException (Exception exception)
145 LogErrorFromException (exception, true);
148 public void LogErrorFromException (Exception exception,
151 LogErrorFromException (exception, showStackTrace, true, String.Empty);
154 [MonoTODO ("Arguments @showDetail and @file are not honored")]
155 public void LogErrorFromException (Exception exception,
156 bool showStackTrace, bool showDetail, string file)
158 if (exception == null)
159 throw new ArgumentNullException ("exception");
161 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
163 StringBuilder sb = new StringBuilder ();
164 sb.Append (exception.Message);
165 if (showStackTrace == true)
166 sb.Append (exception.StackTrace);
167 BuildErrorEventArgs beea = new BuildErrorEventArgs (
168 null, null, BuildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, sb.ToString (),
169 exception.HelpLink, exception.Source);
170 BuildEngine.LogErrorEvent (beea);
171 hasLoggedErrors = true;
174 public void LogErrorFromResources (string messageResourceName,
175 params object[] messageArgs)
177 LogErrorFromResources (null, null, null, null, 0, 0, 0,
178 0, messageResourceName, messageArgs);
181 public void LogErrorFromResources (string subcategoryResourceName,
184 string file, int lineNumber,
188 string messageResourceName,
189 params object[] messageArgs)
191 if (messageResourceName == null)
192 throw new ArgumentNullException ("messageResourceName");
194 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
196 string subcategory = null;
197 if (!String.IsNullOrEmpty (subcategoryResourceName))
198 subcategory = taskResources.GetString (subcategoryResourceName);
200 BuildErrorEventArgs beea = new BuildErrorEventArgs (
202 errorCode, file, lineNumber, columnNumber,
203 endLineNumber, endColumnNumber,
204 FormatResourceString (messageResourceName, messageArgs),
206 BuildEngine.LogErrorEvent (beea);
207 hasLoggedErrors = true;
210 public void LogErrorWithCodeFromResources (string messageResourceName,
211 params object[] messageArgs)
213 // FIXME: there should be something different than normal
214 // LogErrorFromResources
215 LogErrorFromResources (messageResourceName, messageArgs);
218 public void LogErrorWithCodeFromResources (string subcategoryResourceName,
224 string messageResourceName,
225 params object[] messageArgs)
227 // FIXME: there should be something different than normal
228 // LogErrorFromResources
229 LogErrorFromResources (subcategoryResourceName, file,
230 lineNumber, columnNumber, endLineNumber,
231 endColumnNumber, messageResourceName,
235 public void LogMessage (string message,
236 params object[] messageArgs)
238 LogMessage (MessageImportance.Normal, message, messageArgs);
241 public void LogMessage (MessageImportance importance,
243 params object[] messageArgs)
246 throw new ArgumentNullException ("message");
248 LogMessageFromText (FormatString (message, messageArgs), importance);
251 public void LogMessageFromResources (string messageResourceName,
252 params object[] messageArgs)
254 LogMessageFromResources (MessageImportance.Normal, messageResourceName, messageArgs);
257 public void LogMessageFromResources (MessageImportance importance,
258 string messageResourceName,
259 params object[] messageArgs)
261 if (messageResourceName == null)
262 throw new ArgumentNullException ("messageResourceName");
264 LogMessage (importance, FormatResourceString (messageResourceName, messageArgs));
267 public bool LogMessagesFromFile (string fileName)
269 return LogMessagesFromFile (fileName, MessageImportance.Normal);
272 public bool LogMessagesFromFile (string fileName,
273 MessageImportance messageImportance)
276 StreamReader sr = new StreamReader (fileName);
277 LogMessage (messageImportance, sr.ReadToEnd (),
287 public bool LogMessagesFromStream (TextReader stream,
288 MessageImportance messageImportance)
291 LogMessage (messageImportance, stream.ReadToEnd (), null);
298 // FIXME: should it be done here?
303 public bool LogMessageFromText (string lineOfText,
304 MessageImportance messageImportance)
306 if (lineOfText == null)
307 throw new ArgumentNullException ("lineOfText");
309 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
311 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
312 lineOfText, helpKeywordPrefix,
313 null, messageImportance);
314 BuildEngine.LogMessageEvent (bmea);
319 public void LogWarning (string message,
320 params object[] messageArgs)
322 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
324 // FIXME: what about all the parameters?
325 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
326 null, null, BuildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, FormatString (message, messageArgs),
327 helpKeywordPrefix, null);
328 BuildEngine.LogWarningEvent (bwea);
331 public void LogWarning (string subcategory, string warningCode,
332 string helpKeyword, string file,
333 int lineNumber, int columnNumber,
334 int endLineNumber, int endColumnNumber,
336 params object[] messageArgs)
338 ThrowInvalidOperationIf (BuildEngine == null, "Task is attempting to log before it was initialized");
340 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
341 subcategory, warningCode, file, lineNumber,
342 columnNumber, endLineNumber, endColumnNumber,
343 FormatString (message, messageArgs), helpKeyword, null);
344 BuildEngine.LogWarningEvent (bwea);
347 public void LogWarningFromException (Exception exception)
349 LogWarningFromException (exception, false);
352 public void LogWarningFromException (Exception exception,
355 StringBuilder sb = new StringBuilder ();
356 sb.Append (exception.Message);
358 sb.Append (exception.StackTrace);
359 LogWarning (null, null, null, null, 0, 0, 0, 0,
360 sb.ToString (), null);
363 public void LogWarningFromResources (string messageResourceName,
364 params object[] messageArgs)
366 if (messageResourceName == null)
367 throw new ArgumentNullException ("messageResourceName");
369 LogWarningFromResources (null, null, null, null, 0, 0, 0, 0, messageResourceName, messageArgs);
372 public void LogWarningFromResources (string subcategoryResourceName,
380 string messageResourceName,
381 params object[] messageArgs)
383 if (messageResourceName == null)
384 throw new ArgumentNullException ("messageResourceName");
386 string subcategory = null;
387 if (!String.IsNullOrEmpty (subcategoryResourceName))
388 subcategory = taskResources.GetString (subcategoryResourceName);
390 LogWarning (subcategory,
391 warningCode, helpKeyword, file, lineNumber,
392 columnNumber, endLineNumber, endColumnNumber,
393 FormatResourceString (messageResourceName, messageArgs));
396 public void LogWarningWithCodeFromResources (string messageResourceName,
397 params object[] messageArgs)
399 // FIXME: what's different from normal logwarning?
400 LogWarningFromResources (messageResourceName, messageArgs);
403 public void LogWarningWithCodeFromResources (string subcategoryResourceName,
409 string messageResourceName,
410 params object[] messageArgs)
412 LogWarningFromResources (subcategoryResourceName, file,
413 lineNumber, columnNumber, endLineNumber,
414 endColumnNumber, messageResourceName,
419 public void LogExternalProjectFinished (string message,
427 public void LogExternalProjectStarted (string message,
434 void ThrowInvalidOperationIf (bool condition, string message)
437 throw new InvalidOperationException (message);
440 protected IBuildEngine BuildEngine {
442 return taskInstance?.BuildEngine;
446 public bool HasLoggedErrors {
448 return hasLoggedErrors;
452 public string HelpKeywordPrefix {
454 return helpKeywordPrefix;
457 helpKeywordPrefix = value;
461 protected string TaskName {
467 public ResourceManager TaskResources {
469 return taskResources;
472 taskResources = value;