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;
54 public string ExtractMessageCode (string message,
55 out string messageWithoutCodePrefix)
58 throw new ArgumentNullException ("message");
60 messageWithoutCodePrefix = String.Empty;
65 public virtual string FormatResourceString (string resourceName,
68 if (resourceName == null)
69 throw new ArgumentNullException ("resourceName");
75 public virtual string FormatString (string unformatted,
78 if (unformatted == null)
79 throw new ArgumentNullException ("unformatted");
81 if (args == null || args.Length == 0)
84 return String.Format (unformatted, args);
88 public void LogCommandLine (string commandLine)
93 public void LogCommandLine (MessageImportance importance,
98 public void LogError (string message,
99 params object[] messageArgs)
102 throw new ArgumentNullException ("message");
104 BuildErrorEventArgs beea = new BuildErrorEventArgs (
105 null, null, buildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, FormatString (message, messageArgs),
106 helpKeywordPrefix, null);
107 buildEngine.LogErrorEvent (beea);
108 hasLoggedErrors = true;
111 public void LogError (string subcategory, string errorCode,
112 string helpKeyword, string file,
113 int lineNumber, int columnNumber,
114 int endLineNumber, int endColumnNumber,
116 params object[] messageArgs)
119 throw new ArgumentNullException ("message");
121 BuildErrorEventArgs beea = new BuildErrorEventArgs (
122 subcategory, errorCode, file, lineNumber,
123 columnNumber, endLineNumber, endColumnNumber,
124 FormatString (message, messageArgs), helpKeyword /*it's helpKeyword*/,
125 null /*it's senderName*/);
126 buildEngine.LogErrorEvent (beea);
127 hasLoggedErrors = true;
130 public void LogErrorFromException (Exception e)
132 LogErrorFromException (e, true);
135 public void LogErrorFromException (Exception e,
138 LogErrorFromException (e, showStackTrace, true, String.Empty);
141 [MonoTODO ("Arguments @showDetail and @file are not honored")]
142 public void LogErrorFromException (Exception e,
143 bool showStackTrace, bool showDetail, string file)
146 throw new ArgumentNullException ("e");
148 StringBuilder sb = new StringBuilder ();
149 sb.Append (e.Message);
150 if (showStackTrace == true)
151 sb.Append (e.StackTrace);
152 BuildErrorEventArgs beea = new BuildErrorEventArgs (
153 null, null, buildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, sb.ToString (),
154 e.HelpLink, e.Source);
155 buildEngine.LogErrorEvent (beea);
156 hasLoggedErrors = true;
159 public void LogErrorFromResources (string messageResourceName,
160 params object[] messageArgs)
162 LogErrorFromResources (null, null, null, null, 0, 0, 0,
163 0, messageResourceName, null);
166 public void LogErrorFromResources (string subcategoryResourceName,
169 string file, int lineNumber,
173 string messageResourceName,
174 params object[] messageArgs)
176 BuildErrorEventArgs beea = new BuildErrorEventArgs (
177 taskResources.GetString (subcategoryResourceName),
178 errorCode, file, lineNumber, columnNumber,
179 endLineNumber, endColumnNumber,
180 taskResources.GetString (messageResourceName),
182 buildEngine.LogErrorEvent (beea);
183 hasLoggedErrors = true;
186 public void LogErrorWithCodeFromResources (string messageResourceName,
187 params object[] messageArgs)
189 // FIXME: there should be something different than normal
190 // LogErrorFromResources
191 LogErrorFromResources (messageResourceName, messageArgs);
194 public void LogErrorWithCodeFromResources (string subcategoryResourceName,
200 string messageResourceName,
201 params object[] messageArgs)
203 // FIXME: there should be something different than normal
204 // LogErrorFromResources
205 LogErrorFromResources (subcategoryResourceName, file,
206 lineNumber, columnNumber, endLineNumber,
207 endColumnNumber, messageResourceName,
211 public void LogMessage (string message,
212 params object[] messageArgs)
214 LogMessage (MessageImportance.Normal, message, messageArgs);
217 public void LogMessage (MessageImportance importance,
219 params object[] messageArgs)
222 throw new ArgumentNullException ("message");
224 LogMessageFromText (FormatString (message, messageArgs), importance);
227 public void LogMessageFromResources (string messageResourceName,
228 params object[] messageArgs)
230 LogMessage (taskResources.GetString (messageResourceName),
234 public void LogMessageFromResources (MessageImportance importance,
235 string messageResourceName,
236 params object[] messageArgs)
238 LogMessage (importance, taskResources.GetString (
239 messageResourceName), messageArgs);
242 public bool LogMessagesFromFile (string filename)
244 return LogMessagesFromFile (filename, MessageImportance.Normal);
247 public bool LogMessagesFromFile (string filename,
248 MessageImportance messageImportance)
251 StreamReader sr = new StreamReader (filename);
252 LogMessage (messageImportance, sr.ReadToEnd (),
262 public bool LogMessagesFromStream (TextReader stream,
263 MessageImportance messageImportance)
266 LogMessage (messageImportance, stream.ReadToEnd (), null);
273 // FIXME: should it be done here?
278 public bool LogMessageFromText (string lineOfText,
279 MessageImportance importance)
281 if (lineOfText == null)
282 throw new ArgumentNullException ("lineOfText");
284 BuildMessageEventArgs bmea = new BuildMessageEventArgs (
285 lineOfText, helpKeywordPrefix,
287 buildEngine.LogMessageEvent (bmea);
292 public void LogWarning (string message,
293 params object[] messageArgs)
295 // FIXME: what about all the parameters?
296 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
297 null, null, buildEngine.ProjectFileOfTaskNode, 0, 0, 0, 0, FormatString (message, messageArgs),
298 helpKeywordPrefix, null);
299 buildEngine.LogWarningEvent (bwea);
302 public void LogWarning (string subcategory, string warningCode,
303 string helpKeyword, string file,
304 int lineNumber, int columnNumber,
305 int endLineNumber, int endColumnNumber,
307 params object[] messageArgs)
309 BuildWarningEventArgs bwea = new BuildWarningEventArgs (
310 subcategory, warningCode, file, lineNumber,
311 columnNumber, endLineNumber, endColumnNumber,
312 FormatString (message, messageArgs), helpKeyword, null);
313 buildEngine.LogWarningEvent (bwea);
316 public void LogWarningFromException (Exception e)
318 LogWarningFromException (e, false);
321 public void LogWarningFromException (Exception e,
324 StringBuilder sb = new StringBuilder ();
325 sb.Append (e.Message);
327 sb.Append (e.StackTrace);
328 LogWarning (null, null, null, null, 0, 0, 0, 0,
329 sb.ToString (), null);
332 public void LogWarningFromResources (string messageResourceName,
333 params object[] messageArgs)
335 LogWarning (taskResources.GetString (messageResourceName),
339 public void LogWarningFromResources (string subcategoryResourceName,
347 string messageResourceName,
348 params object[] messageArgs)
350 LogWarning (taskResources.GetString (subcategoryResourceName),
351 warningCode, helpKeyword, file, lineNumber,
352 columnNumber, endLineNumber, endColumnNumber,
353 taskResources.GetString (messageResourceName),
357 public void LogWarningWithCodeFromResources (string messageResourceName,
358 params object[] messageArgs)
360 // FIXME: what's different from normal logwarning?
361 LogWarningFromResources (messageResourceName, messageArgs);
364 public void LogWarningWithCodeFromResources (string subcategoryResourceName,
370 string messageResourceName,
371 params object[] messageArgs)
373 LogWarningFromResources (subcategoryResourceName, file,
374 lineNumber, columnNumber, endLineNumber,
375 endColumnNumber, messageResourceName,
380 public void LogExternalProjectFinished (string message,
388 public void LogExternalProjectStarted (string message,
395 protected IBuildEngine BuildEngine {
401 public bool HasLoggedErrors {
403 return hasLoggedErrors;
407 public string HelpKeywordPrefix {
409 return helpKeywordPrefix;
412 helpKeywordPrefix = value;
416 protected string TaskName {
422 public ResourceManager TaskResources {
424 return taskResources;
427 taskResources = value;