2 // FileLogger.cs: Logs to file
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
6 // Ankit Jain (jankit@novell.com)
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2011 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using Microsoft.Build.Framework;
35 namespace Microsoft.Build.BuildEngine {
36 public class FileLogger : ConsoleLogger {
39 string encoding = null;
40 IEventSource eventSource;
44 base.Verbosity = LoggerVerbosity.Detailed;
47 public override void Initialize (IEventSource eventSource)
49 this.eventSource = eventSource;
50 logfile = "msbuild.log";
54 string[] splittedParameters = Parameters.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
55 foreach (string s in splittedParameters) {
56 if (String.Compare (s, "Append") == 0) {
61 if (s.StartsWith ("Encoding")) {
62 if (!TrySplitKeyValuePair (s, out key, out value))
63 throw new LoggerException ("Encoding should be specified as: Encoding=<encoding>, eg. Encoding=UTF-8");
65 if (String.IsNullOrEmpty (value))
66 throw new LoggerException ("Encoding must be non-empty");
72 if (s.StartsWith ("LogFile")) {
73 if (!TrySplitKeyValuePair (s, out key, out value))
74 throw new LoggerException ("LogFile should be specified as: LogFile=<encoding>, eg. LogFile=foo.log");
76 if (String.IsNullOrEmpty (value))
77 throw new LoggerException ("LogFile value must be non-empty");
84 // Attach *our* HandleBuildStarted as the first one,
85 // as it needs to create the writer!
86 eventSource.BuildStarted += HandleBuildStarted;
87 base.Initialize (eventSource);
88 // Attach *our* HandleBuildFinished as the last one,
89 // as it needs to close the writer!
90 eventSource.BuildFinished += HandleBuildFinished;
92 CreateWriter (append);
95 void CreateWriter (bool append_to)
100 if (!String.IsNullOrEmpty (encoding))
101 sw = new StreamWriter (logfile, append_to, Encoding.GetEncoding (encoding));
103 sw = new StreamWriter (logfile, append_to, Encoding.Default);
104 WriteHandler = sw.WriteLine;
107 void HandleBuildStarted (object sender, BuildStartedEventArgs args)
112 void HandleBuildFinished (object sender, BuildFinishedEventArgs args)
114 base.WriteHandler = null;
121 bool TrySplitKeyValuePair (string pair, out string key, out string value)
124 string[] parts = pair.Split ('=');
125 if (parts.Length != 2)
133 public override void Shutdown ()
135 base.WriteHandler = null;
141 if (eventSource != null) {
142 eventSource.BuildStarted -= HandleBuildStarted;
143 eventSource.BuildFinished -= HandleBuildFinished;