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.
35 using Microsoft.Build.Framework;
37 namespace Microsoft.Build.BuildEngine {
38 public class FileLogger : ConsoleLogger {
41 string encoding = null;
42 IEventSource eventSource;
46 base.Verbosity = LoggerVerbosity.Detailed;
49 public override void Initialize (IEventSource eventSource)
51 this.eventSource = eventSource;
52 logfile = "msbuild.log";
56 string[] splittedParameters = Parameters.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
57 foreach (string s in splittedParameters) {
58 if (String.Compare (s, "Append") == 0) {
63 if (s.StartsWith ("Encoding")) {
64 if (!TrySplitKeyValuePair (s, out key, out value))
65 throw new LoggerException ("Encoding should be specified as: Encoding=<encoding>, eg. Encoding=UTF-8");
67 if (String.IsNullOrEmpty (value))
68 throw new LoggerException ("Encoding must be non-empty");
74 if (s.StartsWith ("LogFile")) {
75 if (!TrySplitKeyValuePair (s, out key, out value))
76 throw new LoggerException ("LogFile should be specified as: LogFile=<encoding>, eg. LogFile=foo.log");
78 if (String.IsNullOrEmpty (value))
79 throw new LoggerException ("LogFile value must be non-empty");
86 // Attach *our* HandleBuildStarted as the first one,
87 // as it needs to create the writer!
88 eventSource.BuildStarted += HandleBuildStarted;
89 base.Initialize (eventSource);
90 // Attach *our* HandleBuildFinished as the last one,
91 // as it needs to close the writer!
92 eventSource.BuildFinished += HandleBuildFinished;
94 CreateWriter (append);
97 void CreateWriter (bool append_to)
102 if (!String.IsNullOrEmpty (encoding))
103 sw = new StreamWriter (logfile, append_to, Encoding.GetEncoding (encoding));
105 sw = new StreamWriter (logfile, append_to, Encoding.Default);
106 WriteHandler = sw.WriteLine;
109 void HandleBuildStarted (object sender, BuildStartedEventArgs args)
114 void HandleBuildFinished (object sender, BuildFinishedEventArgs args)
116 base.WriteHandler = null;
123 bool TrySplitKeyValuePair (string pair, out string key, out string value)
126 string[] parts = pair.Split ('=');
127 if (parts.Length != 2)
135 public override void Shutdown ()
137 base.WriteHandler = null;
143 if (eventSource != null) {
144 eventSource.BuildStarted -= HandleBuildStarted;
145 eventSource.BuildFinished -= HandleBuildFinished;