2019-07-03 07:44:53 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Security;
|
|
|
|
using Microsoft.Build.Framework;
|
|
|
|
|
|
|
|
namespace GodotTools.BuildLogger
|
|
|
|
{
|
|
|
|
public class GodotBuildLogger : ILogger
|
|
|
|
{
|
2024-01-13 04:24:12 +00:00
|
|
|
public string? Parameters { get; set; }
|
2019-07-03 07:44:53 +00:00
|
|
|
public LoggerVerbosity Verbosity { get; set; }
|
|
|
|
|
2024-01-13 04:24:12 +00:00
|
|
|
private StreamWriter _logStreamWriter = StreamWriter.Null;
|
|
|
|
private StreamWriter _issuesStreamWriter = StreamWriter.Null;
|
2021-07-25 04:16:59 +00:00
|
|
|
private int _indent;
|
|
|
|
|
2019-07-03 07:44:53 +00:00
|
|
|
public void Initialize(IEventSource eventSource)
|
|
|
|
{
|
|
|
|
if (null == Parameters)
|
2020-10-04 00:11:53 +00:00
|
|
|
throw new LoggerException("Log directory parameter not specified.");
|
2019-07-03 07:44:53 +00:00
|
|
|
|
2021-07-25 04:16:59 +00:00
|
|
|
string[] parameters = Parameters.Split(new[] { ';' });
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
string logDir = parameters[0];
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(logDir))
|
2020-10-04 00:11:53 +00:00
|
|
|
throw new LoggerException("Log directory parameter is empty.");
|
2019-07-03 07:44:53 +00:00
|
|
|
|
|
|
|
if (parameters.Length > 1)
|
|
|
|
throw new LoggerException("Too many parameters passed.");
|
|
|
|
|
|
|
|
string logFile = Path.Combine(logDir, "msbuild_log.txt");
|
|
|
|
string issuesFile = Path.Combine(logDir, "msbuild_issues.csv");
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!Directory.Exists(logDir))
|
|
|
|
Directory.CreateDirectory(logDir);
|
|
|
|
|
2021-07-25 04:16:59 +00:00
|
|
|
_logStreamWriter = new StreamWriter(logFile);
|
|
|
|
_issuesStreamWriter = new StreamWriter(issuesFile);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
if (ex is UnauthorizedAccessException
|
|
|
|
|| ex is ArgumentNullException
|
|
|
|
|| ex is PathTooLongException
|
|
|
|
|| ex is DirectoryNotFoundException
|
|
|
|
|| ex is NotSupportedException
|
|
|
|
|| ex is ArgumentException
|
|
|
|
|| ex is SecurityException
|
|
|
|
|| ex is IOException)
|
|
|
|
{
|
|
|
|
throw new LoggerException("Failed to create log file: " + ex.Message);
|
|
|
|
}
|
2020-10-04 00:11:53 +00:00
|
|
|
|
|
|
|
// Unexpected failure
|
|
|
|
throw;
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
eventSource.ProjectStarted += eventSource_ProjectStarted;
|
2020-10-04 00:11:53 +00:00
|
|
|
eventSource.ProjectFinished += eventSource_ProjectFinished;
|
2019-07-03 07:44:53 +00:00
|
|
|
eventSource.MessageRaised += eventSource_MessageRaised;
|
|
|
|
eventSource.WarningRaised += eventSource_WarningRaised;
|
|
|
|
eventSource.ErrorRaised += eventSource_ErrorRaised;
|
|
|
|
}
|
|
|
|
|
2020-10-04 00:11:53 +00:00
|
|
|
private void eventSource_ProjectStarted(object sender, ProjectStartedEventArgs e)
|
|
|
|
{
|
|
|
|
WriteLine(e.Message);
|
2021-07-25 04:16:59 +00:00
|
|
|
_indent++;
|
2020-10-04 00:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void eventSource_ProjectFinished(object sender, ProjectFinishedEventArgs e)
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
_indent--;
|
2020-10-04 00:11:53 +00:00
|
|
|
WriteLine(e.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void eventSource_ErrorRaised(object sender, BuildErrorEventArgs e)
|
2019-07-03 07:44:53 +00:00
|
|
|
{
|
|
|
|
string line = $"{e.File}({e.LineNumber},{e.ColumnNumber}): error {e.Code}: {e.Message}";
|
|
|
|
|
2020-08-20 20:38:36 +00:00
|
|
|
if (!string.IsNullOrEmpty(e.ProjectFile))
|
2019-07-03 07:44:53 +00:00
|
|
|
line += $" [{e.ProjectFile}]";
|
|
|
|
|
|
|
|
WriteLine(line);
|
|
|
|
|
2024-08-29 18:33:47 +00:00
|
|
|
string errorLine = $@"error,{e.File?.CsvEscape() ?? string.Empty},{e.LineNumber},{e.ColumnNumber}," +
|
2020-08-20 20:38:36 +00:00
|
|
|
$"{e.Code?.CsvEscape() ?? string.Empty},{e.Message.CsvEscape()}," +
|
|
|
|
$"{e.ProjectFile?.CsvEscape() ?? string.Empty}";
|
2021-07-25 04:16:59 +00:00
|
|
|
_issuesStreamWriter.WriteLine(errorLine);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-04 00:11:53 +00:00
|
|
|
private void eventSource_WarningRaised(object sender, BuildWarningEventArgs e)
|
2019-07-03 07:44:53 +00:00
|
|
|
{
|
|
|
|
string line = $"{e.File}({e.LineNumber},{e.ColumnNumber}): warning {e.Code}: {e.Message}";
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(e.ProjectFile))
|
|
|
|
line += $" [{e.ProjectFile}]";
|
|
|
|
|
|
|
|
WriteLine(line);
|
|
|
|
|
2024-08-29 18:33:47 +00:00
|
|
|
string warningLine = $@"warning,{e.File?.CsvEscape() ?? string.Empty},{e.LineNumber},{e.ColumnNumber}," +
|
2020-08-20 20:38:36 +00:00
|
|
|
$"{e.Code?.CsvEscape() ?? string.Empty},{e.Message.CsvEscape()}," +
|
|
|
|
$"{e.ProjectFile?.CsvEscape() ?? string.Empty}";
|
2021-07-25 04:16:59 +00:00
|
|
|
_issuesStreamWriter.WriteLine(warningLine);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void eventSource_MessageRaised(object sender, BuildMessageEventArgs e)
|
|
|
|
{
|
|
|
|
// BuildMessageEventArgs adds Importance to BuildEventArgs
|
|
|
|
// Let's take account of the verbosity setting we've been passed in deciding whether to log the message
|
|
|
|
if (e.Importance == MessageImportance.High && IsVerbosityAtLeast(LoggerVerbosity.Minimal)
|
|
|
|
|| e.Importance == MessageImportance.Normal && IsVerbosityAtLeast(LoggerVerbosity.Normal)
|
|
|
|
|| e.Importance == MessageImportance.Low && IsVerbosityAtLeast(LoggerVerbosity.Detailed))
|
|
|
|
{
|
|
|
|
WriteLineWithSenderAndMessage(string.Empty, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Write a line to the log, adding the SenderName and Message
|
|
|
|
/// (these parameters are on all MSBuild event argument objects)
|
|
|
|
/// </summary>
|
|
|
|
private void WriteLineWithSenderAndMessage(string line, BuildEventArgs e)
|
|
|
|
{
|
|
|
|
if (0 == string.Compare(e.SenderName, "MSBuild", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
// Well, if the sender name is MSBuild, let's leave it out for prettiness
|
|
|
|
WriteLine(line + e.Message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteLine(e.SenderName + ": " + line + e.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteLine(string line)
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
for (int i = _indent; i > 0; i--)
|
2019-07-03 07:44:53 +00:00
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
_logStreamWriter.Write("\t");
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-25 04:16:59 +00:00
|
|
|
_logStreamWriter.WriteLine(line);
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Shutdown()
|
|
|
|
{
|
2021-07-25 04:16:59 +00:00
|
|
|
_logStreamWriter.Close();
|
|
|
|
_issuesStreamWriter.Close();
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsVerbosityAtLeast(LoggerVerbosity checkVerbosity)
|
|
|
|
{
|
|
|
|
return Verbosity >= checkVerbosity;
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 18:55:50 +00:00
|
|
|
|
|
|
|
internal static class StringExtensions
|
|
|
|
{
|
|
|
|
public static string CsvEscape(this string value, char delimiter = ',')
|
|
|
|
{
|
|
|
|
bool hasSpecialChar = value.IndexOfAny(new[] { '\"', '\n', '\r', delimiter }) != -1;
|
|
|
|
|
|
|
|
if (hasSpecialChar)
|
2024-01-12 21:47:50 +00:00
|
|
|
return "\"" + value.Replace("\"", "\"\"", StringComparison.Ordinal) + "\"";
|
2020-05-09 18:55:50 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 07:44:53 +00:00
|
|
|
}
|