forked from Evengard/MsBuildToCCNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
241 lines (212 loc) · 8.92 KB
/
Logger.cs
File metadata and controls
241 lines (212 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright © 2006 by Christian Rodemeyer (mailto:christian@atombrenner.de)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.IO;
using System.Collections;
namespace Rodemeyer.MsBuildToCCNet
{
/// <summary>
/// Implements a custom MSBuild logger for integration into CruiseControl.NET
/// The following MSBuild verbosity options (/v:) are supported
/// - Quiet => errors, warnings and number of projects build
/// - Minimal => errors, warnings, one entry for every project
/// - Normal => like Minimal plus messages with high importance
/// - Detailed => like Normal plus messages with normal importance
/// - Diagnostic => like Detailed plus messages with low importance
/// </summary>
public class MsBuildToCCNetLogger : Logger
{
public MsBuildToCCNetLogger()
{ }
private string logfile;
private string commonPrefix;
private string currentSolution = null;
private int commonPrefixLength;
MessageImportance loglevel;
private Dictionary<string, Project> file_to_project = new Dictionary<string, Project>();
private Stack<Project> project_stack = new Stack<Project>();
private List<Project> projects = new List<Project>();
private Project current_project;
/// <summary>
/// called by MSBuild just before starting the build
/// </summary>
/// <param name="eventSource"></param>
public override void Initialize(IEventSource eventSource)
{
if (base.Parameters != null)
this.logfile = base.Parameters.Split(';')[0]; // ignore all parameters but the first (which is the name of the file to log to)
else
this.logfile = "msbuild-output.xml"; // default, in case we are not startet from a ccnet task
this.commonPrefix = Environment.CurrentDirectory;
this.commonPrefixLength = commonPrefix.Length + 1;
current_project = new Project("MSBuild");
projects.Add(current_project);
project_stack.Push(current_project);
if (this.Verbosity > LoggerVerbosity.Minimal)
{
eventSource.MessageRaised += new BuildMessageEventHandler(OnMessageRaised);
switch (Verbosity)
{
case LoggerVerbosity.Normal: loglevel = MessageImportance.High; break;
case LoggerVerbosity.Detailed: loglevel = MessageImportance.Normal; break;
default: loglevel = MessageImportance.Low; break;
}
}
eventSource.WarningRaised += new BuildWarningEventHandler(OnWarningRaised);
eventSource.ErrorRaised += new BuildErrorEventHandler(OnErrorRaised);
eventSource.ProjectStarted += new ProjectStartedEventHandler(OnProjectStarted);
eventSource.ProjectFinished += new ProjectFinishedEventHandler(OnProjectFinished);
}
/// <summary>
/// called by MSBuild after the build has finished
/// </summary>
public override void Shutdown()
{
XmlTextWriter w = new XmlTextWriter(logfile, System.Text.Encoding.UTF8);
w.Formatting = this.Verbosity > LoggerVerbosity.Quiet ? Formatting.Indented : Formatting.None;
WriteLog(w);
w.Flush();
w.Close();
}
/// <summary>
/// Needed for sorting the project list by number of warnings
/// </summary>
class WarningComparer : IComparer<Project>
{
public int Compare(Project x, Project y)
{
return y.WarningCount - x.WarningCount;
}
}
/// <summary>
/// writes the in memory gathered information into the xml log file
/// </summary>
/// <param name="w"></param>
private void WriteLog(XmlWriter w)
{
w.WriteStartDocument();
w.WriteStartElement("msbuild");
if (currentSolution != null)
{
w.WriteAttributeString("solution_name", Path.GetFileName(currentSolution));
w.WriteAttributeString("solution_dir", Path.GetDirectoryName(currentSolution));
}
w.WriteAttributeString("project_count", XmlConvert.ToString(projects.Count));
int errorCount = 0;
int warningCount = 0;
foreach (Project p in projects)
{
errorCount += p.ErrorCount;
warningCount += p.WarningCount;
}
w.WriteAttributeString("warning_count", XmlConvert.ToString(warningCount));
w.WriteAttributeString("error_count", XmlConvert.ToString(errorCount));
buildHasErrors = errorCount > 0;
if (!buildHasErrors) // Sort after WarningCount
{
projects.Sort(new WarningComparer());
}
foreach (Project p in projects)
{
if (Verbosity > LoggerVerbosity.Quiet || p.ErrorCount > 0 || p.WarningCount > 0)
{
WriteProject(w, p);
}
}
w.WriteEndElement();
w.WriteEndDocument();
}
private bool buildHasErrors;
/// <summary>
/// removes the working folder from the filename
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
private string RemoveCommonPrefix(string file)
{
if (file.StartsWith(commonPrefix))
return file.Substring(commonPrefixLength, file.Length - commonPrefixLength);
else
return file;
}
private void WriteProject(XmlWriter w, Project p)
{
w.WriteStartElement("project");
string file = RemoveCommonPrefix(p.File);
w.WriteAttributeString("dir", Path.GetDirectoryName(file));
w.WriteAttributeString("name", Path.GetFileName(file));
WriteErrorsOrWarnings(w, "error", p.Errors);
if (!buildHasErrors) WriteErrorsOrWarnings(w, "warning", p.Warnings);
WriteMessages(w, p.Messages);
w.WriteEndElement();
}
private void WriteErrorsOrWarnings(XmlWriter w, string type, IEnumerable list)
{
foreach (ErrorOrWarningBase ew in list)
{
w.WriteStartElement(type);
w.WriteAttributeString("code", ew.Code);
w.WriteAttributeString("message", ew.Text);
if (ew.File != null)
{
w.WriteAttributeString("dir", Path.GetDirectoryName(ew.File));
w.WriteAttributeString("name", Path.GetFileName(ew.File));
w.WriteAttributeString("pos", "(" + XmlConvert.ToString(ew.Line) + ", " + XmlConvert.ToString(ew.Column) + ")");
}
w.WriteEndElement();
}
}
private void WriteMessages(XmlWriter w, IEnumerable<Message> list)
{
foreach (Message m in list)
{
w.WriteStartElement("message");
w.WriteAttributeString("importance", m.Importance.ToString());
w.WriteString(m.Text);
w.WriteEndElement();
}
}
private void OnProjectStarted(object sender, ProjectStartedEventArgs e)
{
if ( (currentSolution == null || currentSolution.EndsWith(".csproj")) && e.ProjectFile.EndsWith(".sln"))
{
currentSolution = RemoveCommonPrefix(e.ProjectFile);
}
if (currentSolution == null && e.ProjectFile.EndsWith(".csproj"))
{
currentSolution = RemoveCommonPrefix(e.ProjectFile);
}
if (!file_to_project.TryGetValue(e.ProjectFile, out this.current_project))
{
current_project = new Project(e.ProjectFile);
file_to_project.Add(e.ProjectFile, current_project);
projects.Add(current_project);
}
project_stack.Push(current_project);
}
void OnProjectFinished(object sender, ProjectFinishedEventArgs e)
{
project_stack.Pop();
current_project = (project_stack.Count == 0) ? null : project_stack.Peek();
}
private void OnWarningRaised(object sender, BuildWarningEventArgs e)
{
current_project.Add(new Warning(e));
}
private void OnErrorRaised(object sender, BuildErrorEventArgs e)
{
current_project.Add(new Error(e));
}
private void OnMessageRaised(object sender, BuildMessageEventArgs e)
{
if (e.Importance <= loglevel)
{
current_project.Add(new Message(e));
}
}
}
}