Added a log analyzer for CCNet

This commit is contained in:
Rick Falkvinge
2016-09-19 10:17:41 +02:00
parent e4d1b9edee
commit 3f31a4a880
5 changed files with 362 additions and 0 deletions
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
@@ -0,0 +1,242 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
namespace XBuildLog2Xml
{
class Program
{
static void Main(string[] args)
{
string logFileName = "msbuild.log";
if (Debugger.IsAttached)
{
logFileName = "E:\\Temp\\" + logFileName;
}
List<ProjectResult> projectResults = new List<ProjectResult>();
int sumWarningsCode = 0;
int sumWarningsEnvironment = 0;
int sumErrors = 0;
ProjectResult currentProject = new ProjectResult
{
ProjectName = "Solution"
};
const string projectStartRegex = "Building target \"Compile\" in project \"[a-zA-Z/]+/(?<projectName>[A-Za-z0-9\\-]+)\\.csproj\"";
const string messageRegex = "(?<fileName>[A-Za-z\\-_0-9]+).cs\\((?<lineNumber>[0-9]+),[0-9]+\\): (?<messageType>[a-z]+) CS(?<messageCode>[0-9]{4}): (?<messageText>.+)";
Regex regexProject = new Regex (projectStartRegex, RegexOptions.Compiled);
Regex regexMessage = new Regex (messageRegex, RegexOptions.Compiled);
// first, read original log into parsed data
using (TextReader reader = new StreamReader (logFileName, Encoding.UTF8, true))
{
string line = reader.ReadLine();
while (line != null)
{
// 'Building target "Compile" in ".../.../[A-Za-z]+.csproj"'
// 'Database-ExchangeRates.cs(59,34): warning CS0219: The variable `testDate' is assigned but its value is never used'
line = line.Trim();
Match matchProject = regexProject.Match (line);
if (matchProject.Success)
{
// Store old project
projectResults.Add (currentProject);
currentProject = new ProjectResult
{
ProjectName = "Project " + matchProject.Groups["projectName"].Value
};
}
Match matchMessage = regexMessage.Match (line);
if (matchMessage.Success)
{
BuildMessage newMessage = new BuildMessage
{
Code = matchMessage.Groups["messageCode"].Value,
File = matchMessage.Groups["fileName"].Value,
Line = Int32.Parse (matchMessage.Groups["lineNumber"].Value),
Description = matchMessage.Groups["messageText"].Value
};
if (matchMessage.Groups["messageType"].Value.ToLowerInvariant() == "warning")
{
sumWarningsCode++;
newMessage.Type = MessageType.CodeWarning;
currentProject.CodeWarnings.Add (newMessage);
}
else
{
sumErrors++;
newMessage.Type = MessageType.Error;
currentProject.Errors.Add (newMessage);
}
}
else if (line.Contains ("error"))
{
currentProject.Errors.Add (new BuildMessage
{
Type = MessageType.Error,
Description = line
});
sumErrors++;
}
else if (line.Contains ("warning"))
{
currentProject.EnvironmentWarnings.Add(new BuildMessage
{
Type = MessageType.EnvironmentWarning,
Description = line
});
sumWarningsEnvironment++;
}
line = reader.ReadLine();
}
projectResults.Add (currentProject);
}
// and once we have it in data format, write it as an xml document
using (XmlWriter xmlWriter = new XmlTextWriter ("xbuild.xml", Encoding.UTF8))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement ("xbuild");
xmlWriter.WriteAttributeString ("environment_warning_count", sumWarningsEnvironment.ToString());
xmlWriter.WriteAttributeString ("code_warning_count", sumWarningsCode.ToString());
xmlWriter.WriteAttributeString ("error_count", sumErrors.ToString());
foreach (ProjectResult result in projectResults)
{
if (result.WarningsTotal + result.ErrorsTotal > 0)
{
xmlWriter.WriteStartElement ("project");
xmlWriter.WriteAttributeString ("name", result.ProjectName);
if (result.ErrorsTotal > 0)
{
WriteXmlBuildMessages (xmlWriter, result.Errors);
}
else
{
WriteXmlBuildMessages (xmlWriter, result.CodeWarnings);
WriteXmlBuildMessages (xmlWriter, result.EnvironmentWarnings);
}
xmlWriter.WriteEndElement();
}
}
xmlWriter.WriteEndDocument();
}
}
private static void WriteXmlBuildMessages (XmlWriter xmlWriter, List<BuildMessage> messages)
{
foreach (BuildMessage message in messages)
{
WriteXmlBuildMessage (xmlWriter, message);
}
}
private static void WriteXmlBuildMessage (XmlWriter xmlWriter, BuildMessage message)
{
if (message.Type == MessageType.Error)
{
xmlWriter.WriteStartElement ("error");
}
else
{
xmlWriter.WriteStartElement("warning");
}
xmlWriter.WriteAttributeString ("message", message.Description);
xmlWriter.WriteAttributeString ("code", message.Code); // may be null or empty
if (message.Line > 0 || !string.IsNullOrEmpty (message.File))
{
xmlWriter.WriteAttributeString ("name", message.File);
xmlWriter.WriteAttributeString ("line", message.Line.ToString());
}
xmlWriter.WriteEndElement();
}
public enum MessageType
{
Unknown = 0,
Error,
CodeWarning,
EnvironmentWarning
};
public class ProjectResult
{
public ProjectResult()
{
Errors = new List<BuildMessage>();
CodeWarnings = new List<BuildMessage>();
EnvironmentWarnings = new List<BuildMessage>();
}
public string ProjectName { get; set; }
public int ErrorsTotal
{
get { return Errors.Count; }
}
public int WarningsTotal
{
get { return CodeWarningsTotal + EnvironmentWarningsTotal; }
}
public int CodeWarningsTotal
{
get { return CodeWarnings.Count; }
}
public int EnvironmentWarningsTotal
{
get { return EnvironmentWarnings.Count; }
}
public List<BuildMessage> Errors;
public List<BuildMessage> CodeWarnings;
public List<BuildMessage> EnvironmentWarnings;
}
public class BuildMessage
{
public MessageType Type { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public string Folder { get; set; }
public string File { get; set; }
public int Line { get; set; }
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("XBuildLog2Xml")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("XBuildLog2Xml")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("cafe795c-105f-4006-9056-f5fc6f317dd0")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{23891870-C691-4AE2-B76D-509A864CC8C5}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>XBuildLog2Xml</RootNamespace>
<AssemblyName>XBuildLog2Xml</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
@@ -0,0 +1,20 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XBuildLog2Xml", "XBuildLog2Xml.csproj", "{23891870-C691-4AE2-B76D-509A864CC8C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{23891870-C691-4AE2-B76D-509A864CC8C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{23891870-C691-4AE2-B76D-509A864CC8C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{23891870-C691-4AE2-B76D-509A864CC8C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{23891870-C691-4AE2-B76D-509A864CC8C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal