030ea8aa86a9607fda0eb7996a426013048a0574
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / LogExtensions.cs
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27
28 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.BuildEngine
36 {
37         static class LogExtensions
38         {
39                 public static string FormatString (string unformatted,
40                                                    params object[] args)
41                 {
42                         if (unformatted == null)
43                                 throw new ArgumentNullException ("unformatted");
44                 
45                         if (args == null || args.Length == 0)
46                                 return unformatted;
47                         else
48                                 return String.Format (unformatted, args);
49                 }
50                 
51                 public static void LogError (this Engine engine, string message,
52                                      params object[] messageArgs)
53                 {
54                         if (message == null)
55                                 throw new ArgumentNullException ("message");
56                                 
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);
61                 }
62
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)
69                 {
70                         if (message == null)
71                                 throw new ArgumentNullException ("message");
72                         
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*/);
78
79                         engine.EventSource.FireErrorRaised (engine, beea);
80                 }
81
82                 public static void LogErrorFromException (this Engine engine, Exception e)
83                 {
84                         LogErrorFromException (engine, e, true);
85                 }
86
87                 public static void LogErrorFromException (this Engine engine, Exception e,
88                                                    bool showStackTrace)
89                 {
90                         LogErrorFromException (engine, e, showStackTrace, true, String.Empty);
91                 }
92
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)
96                 {
97                         if (e == null)
98                                 throw new ArgumentNullException ("e");
99                 
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);
108                 }
109
110                 public static void LogMessage (this Engine engine, string message,
111                                        params object[] messageArgs)
112                 {
113                         LogMessage (engine, MessageImportance.Normal, message, messageArgs); 
114                 }
115
116                 public static void LogMessage (this Engine engine, MessageImportance importance,
117                                         string message,
118                                         params object[] messageArgs)
119                 {
120                         if (message == null)
121                                 throw new ArgumentNullException ("message");
122                 
123                         LogMessageFromText (engine, FormatString (message, messageArgs), importance);
124                 }
125
126                 public static bool LogMessageFromText (this Engine engine, string lineOfText,
127                                                 MessageImportance importance)
128                 {
129                         if (lineOfText == null)
130                                 throw new ArgumentNullException ("lineOfText");
131
132                         BuildMessageEventArgs bmea = new BuildMessageEventArgs (
133                                 lineOfText, null,
134                                 null, importance);
135                         
136                         engine.EventSource.FireMessageRaised (engine, bmea);
137
138                         return true;
139                 }
140
141                 public static void LogWarning (this Engine engine, string message,
142                                        params object[] messageArgs)
143                 {
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);
149                 }
150
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)
157                 {
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);
163                 }
164
165                 public static void LogWarningFromException (this Engine engine, Exception e)
166                 {
167                         LogWarningFromException (engine, e, false);
168                 }
169
170                 public static void LogWarningFromException (this Engine engine, Exception e,
171                                                      bool showStackTrace)
172                 {
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);
179                 }
180         }
181 }
182
183 #endif