Skip to content

Commit

Permalink
Cleaning up the code and api
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKrivanek committed Dec 21, 2023
1 parent 4397d63 commit 8cd23de
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/StructuredLogViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ private async void OpenLogFile(string filePath)
{
try
{
return Serialization.Read(filePath, progress.Progress, UnknownDataBehavior.Error);
return Serialization.Read(filePath, progress.Progress, ReaderSettings.Default);
}
catch (Exception ex)
{
Expand Down
21 changes: 11 additions & 10 deletions src/StructuredLogger/BinaryLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public static IEnumerable<Record> ReadRecords(byte[] binlogBytes)
return reader.ReadRecords(binlogBytes);
}

public static Build ReadBuild(string filePath, UnknownDataBehavior unknownDataBehavior = UnknownDataBehavior.Error)
=> ReadBuild(filePath, progress: null, unknownDataBehavior);
public static Build ReadBuild(string filePath, ReaderSettings readerSettings = null)
=> ReadBuild(filePath, progress: null, readerSettings);

public static Build ReadBuild(string filePath, Progress progress, UnknownDataBehavior unknownDataBehavior)
public static Build ReadBuild(string filePath, Progress progress, ReaderSettings readerSettings)
{
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Expand All @@ -41,7 +41,7 @@ public static Build ReadBuild(string filePath, Progress progress, UnknownDataBeh
projectImportsArchive = File.ReadAllBytes(projectImportsZipFile);
}

var build = ReadBuild(stream, progress, projectImportsArchive, unknownDataBehavior);
var build = ReadBuild(stream, progress, projectImportsArchive, readerSettings);
build.LogFilePath = filePath;
return build;
}
Expand All @@ -56,10 +56,11 @@ public static Build ReadBuild(
Stream stream,
Progress progress,
byte[] projectImportsArchive = null,
UnknownDataBehavior unknownDataBehavior = UnknownDataBehavior.Error)
ReaderSettings readerSettings = null)
{
Build build = null;
IEnumerable<string> strings = null;
readerSettings ??= ReaderSettings.Default;

var eventSource = new BinLogReader();

Expand All @@ -84,12 +85,12 @@ public static Build ReadBuild(
int[] errorByType = new int[Enum.GetValues(typeof(ReaderErrorType)).Length];
eventSource.RecoverableReadError += eArg =>
{
if (unknownDataBehavior == UnknownDataBehavior.ThrowException)
if (readerSettings.UnknownDataBehavior == UnknownDataBehavior.ThrowException)
{
throw new Exception($"Unknown data encountered in the log file ({eArg.ErrorType}-{eArg.RecordKind}): {eArg.GetFormattedMessage()}");
}
if (unknownDataBehavior == UnknownDataBehavior.Ignore)
if (readerSettings.UnknownDataBehavior == UnknownDataBehavior.Ignore)
{
return;
}
Expand Down Expand Up @@ -161,15 +162,15 @@ public static Build ReadBuild(

if (errorByType.Any(i => i != 0))
{
string summary = string.Join(", ", errorByType.Where((count, index) => count > 0).Select((count, index) => $"{((ReaderErrorType)index).ToString()}: {count}"));
string summary = string.Join(", ", errorByType.Where((count, index) => count > 0).Select((count, index) => $"{((ReaderErrorType)index)}: {count}"));
string message = $"{errorByType.Sum()} reading errors encountered ({summary}) - unknown data was skipped in current compatibility mode.";

TreeNode node = unknownDataBehavior switch
TreeNode node = readerSettings.UnknownDataBehavior switch
{
UnknownDataBehavior.Error => new Error() { Text = message },
UnknownDataBehavior.Warning => new Warning() { Text = message },
UnknownDataBehavior.Message => new CriticalBuildMessage() { Text = message },
_ => throw new ArgumentOutOfRangeException(nameof(unknownDataBehavior), unknownDataBehavior, null)
_ => throw new ArgumentOutOfRangeException(nameof(readerSettings.UnknownDataBehavior), readerSettings.UnknownDataBehavior, "Unexpected value")
};

build.AddChildAtBeginning(node);
Expand Down
1 change: 0 additions & 1 deletion src/StructuredLogger/ObjectModel/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using TPLTask = System.Threading.Tasks.Task;

Expand Down
16 changes: 16 additions & 0 deletions src/StructuredLogger/ReaderSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Microsoft.Build.Logging.StructuredLogger
{
/// <summary>
/// Settings for the <see cref="BinaryLogReader"/>.
/// </summary>
public class ReaderSettings
{
public static ReaderSettings Default { get; } =
new() { UnknownDataBehavior = UnknownDataBehavior.Error };

/// <summary>
/// Indication of how the unknown data in future versions of binlogs should be handled.
/// </summary>
public UnknownDataBehavior UnknownDataBehavior { get; set; }
}
}
17 changes: 4 additions & 13 deletions src/StructuredLogger/Serialization/Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@

namespace Microsoft.Build.Logging.StructuredLogger
{
public enum UnknownDataBehavior
{
Error,
Warning,
Message,
Ignore,
ThrowException
}

public static class Serialization
{
public static readonly string FileDialogFilter = "Structured Log (*.buildlog)|*.buildlog|Readable (large) XML Log (*.xml)|*.xml";
Expand Down Expand Up @@ -56,9 +47,9 @@ public static Dictionary<string, Type> ObjectModelTypes
public static Build ReadBuildLog(Stream stream, byte[] projectImportsArchive = null) => BuildLogReader.Read(stream, projectImportsArchive);
public static Build ReadBinLog(Stream stream, byte[] projectImportsArchive = null) => BinaryLog.ReadBuild(stream, projectImportsArchive);

public static Build Read(string filePath) => Read(filePath, progress: null, unknownDataBehavior: UnknownDataBehavior.Error);
public static Build Read(string filePath) => Read(filePath, progress: null, readerSettings: ReaderSettings.Default);

public static Build Read(string filePath, Progress progress, UnknownDataBehavior unknownDataBehavior)
public static Build Read(string filePath, Progress progress, ReaderSettings readerSettings)
{
if (filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -68,7 +59,7 @@ public static Build Read(string filePath, Progress progress, UnknownDataBehavior
{
try
{
return BinaryLog.ReadBuild(filePath, progress, unknownDataBehavior);
return BinaryLog.ReadBuild(filePath, progress, readerSettings);
}
catch (Exception)
{
Expand Down Expand Up @@ -97,7 +88,7 @@ public static Build Read(string filePath, Progress progress, UnknownDataBehavior
{
if (DetectLogFormat(filePath) == ".binlog")
{
return BinaryLog.ReadBuild(filePath, progress, unknownDataBehavior);
return BinaryLog.ReadBuild(filePath, progress, readerSettings);
}
else
{
Expand Down
33 changes: 33 additions & 0 deletions src/StructuredLogger/UnknownDataBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Microsoft.Build.Logging.StructuredLogger
{
/// <summary>
/// Indication of how the unknown data in future versions of binlogs should be handled.
/// </summary>
public enum UnknownDataBehavior
{
/// <summary>
/// When unknown data encountered - emit a single synthetic error message for the entire build.
/// </summary>
Error,

/// <summary>
/// When unknown data encountered - emit a single synthetic warning message for the entire build.
/// </summary>
Warning,

/// <summary>
/// When unknown data encountered - emit a single synthetic message for the entire build.
/// </summary>
Message,

/// <summary>
/// Ignore the unknown data and continue reading the rest of the build.
/// </summary>
Ignore,

/// <summary>
/// Throw an exception when unknown data is encountered.
/// </summary>
ThrowException
}
}

0 comments on commit 8cd23de

Please sign in to comment.