2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / LogExtensions.cs
blob030ea8aa86a9607fda0eb7996a426013048a0574
1 //
2 // LogExtensions.cs: Extension methods for logging on Engine
3 //
4 // Author:
5 // Ankit Jain (jankit@novell.com)
6 //
7 // Copyright 2010 Novell, Inc (http://www.novell.com)
8 //
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.
28 #if NET_2_0
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.BuildEngine
37 static class LogExtensions
39 public static string FormatString (string unformatted,
40 params object[] args)
42 if (unformatted == null)
43 throw new ArgumentNullException ("unformatted");
45 if (args == null || args.Length == 0)
46 return unformatted;
47 else
48 return String.Format (unformatted, args);
51 public static void LogError (this Engine engine, string message,
52 params object[] messageArgs)
54 if (message == null)
55 throw new ArgumentNullException ("message");
57 BuildErrorEventArgs beea = new BuildErrorEventArgs (
58 null, null, null, 0, 0, 0, 0, FormatString (message, messageArgs),
59 null, null);
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,
67 string message,
68 params object[] messageArgs)
70 if (message == null)
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,
88 bool showStackTrace)
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)
97 if (e == null)
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,
117 string message,
118 params object[] messageArgs)
120 if (message == null)
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 (
133 lineOfText, null,
134 null, importance);
136 engine.EventSource.FireMessageRaised (engine, bmea);
138 return true;
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),
147 null, null);
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,
155 string message,
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,
171 bool showStackTrace)
173 StringBuilder sb = new StringBuilder ();
174 sb.Append (e.Message);
175 if (showStackTrace)
176 sb.Append (e.StackTrace);
177 LogWarning (engine, null, null, null, null, 0, 0, 0, 0,
178 sb.ToString (), null);
183 #endif