Merge pull request #799 from kebby/master
[mono.git] / mcs / class / Microsoft.Build.Engine / Microsoft.Build.BuildEngine / FileLogger.cs
1 //
2 // FileLogger.cs: Logs to file
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 // 
8 // (C) 2005 Marek Sieradzki
9 // Copyright 2011 Novell, Inc (http://www.novell.com)
10 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.BuildEngine {
36         public class FileLogger : ConsoleLogger {
37                 StreamWriter sw;
38                 string logfile;
39                 string encoding = null;
40                 IEventSource eventSource;
41
42                 public FileLogger ()
43                 {
44                         base.Verbosity = LoggerVerbosity.Detailed;
45                 }
46
47                 public override void Initialize (IEventSource eventSource)
48                 {
49                         this.eventSource = eventSource;
50                         logfile = "msbuild.log";
51
52                         bool append = false;
53                         string key, value;
54                         string[] splittedParameters = Parameters.Split (new char [] {';'}, StringSplitOptions.RemoveEmptyEntries);
55                         foreach (string s in splittedParameters) {
56                                 if (String.Compare (s, "Append") == 0) {
57                                         append = true;
58                                         continue;
59                                 }
60
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");
64
65                                         if (String.IsNullOrEmpty (value))
66                                                 throw new LoggerException ("Encoding must be non-empty");
67
68                                         encoding = value;
69                                         continue;
70                                 }
71
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");
75
76                                         if (String.IsNullOrEmpty (value))
77                                                 throw new LoggerException ("LogFile value must be non-empty");
78
79                                         logfile = value;
80                                         continue;
81                                 }
82                         }
83
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;
91
92                         CreateWriter (append);
93                 }
94
95                 void CreateWriter (bool append_to)
96                 {
97                         if (sw != null)
98                                 return;
99
100                         if (!String.IsNullOrEmpty (encoding))
101                                 sw = new StreamWriter (logfile, append_to, Encoding.GetEncoding (encoding));
102                         else
103                                 sw = new StreamWriter (logfile, append_to, Encoding.Default);
104                         WriteHandler = sw.WriteLine;
105                 }
106
107                 void HandleBuildStarted (object sender, BuildStartedEventArgs args)
108                 {
109                         CreateWriter (true);
110                 }
111
112                 void HandleBuildFinished (object sender, BuildFinishedEventArgs args)
113                 {
114                         base.WriteHandler = null;
115                         if (sw != null) {
116                                 sw.Close ();
117                                 sw = null;
118                         }
119                 }
120
121                 bool TrySplitKeyValuePair (string pair, out string key, out string value)
122                 {
123                         key = value = null;
124                         string[] parts = pair.Split ('=');
125                         if (parts.Length != 2)
126                                 return false;
127
128                         key = parts [0];
129                         value = parts [1];
130                         return true;
131                 }
132
133                 public override void Shutdown ()
134                 {
135                         base.WriteHandler = null;
136                         if (sw != null) {
137                                 sw.Close ();
138                                 sw = null;
139                         }
140
141                         if (eventSource != null) {
142                                 eventSource.BuildStarted -= HandleBuildStarted;
143                                 eventSource.BuildFinished -= HandleBuildFinished;
144                         }
145
146                         base.Shutdown ();
147                 }
148         }
149 }