mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Make report writing work with streams
This commit is contained in:
@@ -234,13 +234,13 @@ namespace SabreTools.DatTools.Test
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData(StatReportFormat.None, typeof(Reports.Formats.ConsoleOutput))]
|
[InlineData(StatReportFormat.None, typeof(Reports.Formats.Textfile))]
|
||||||
[InlineData(StatReportFormat.Textfile, typeof(Reports.Formats.Textfile))]
|
[InlineData(StatReportFormat.Textfile, typeof(Reports.Formats.Textfile))]
|
||||||
[InlineData(StatReportFormat.CSV, typeof(Reports.Formats.CommaSeparatedValue))]
|
[InlineData(StatReportFormat.CSV, typeof(Reports.Formats.CommaSeparatedValue))]
|
||||||
[InlineData(StatReportFormat.HTML, typeof(Reports.Formats.Html))]
|
[InlineData(StatReportFormat.HTML, typeof(Reports.Formats.Html))]
|
||||||
[InlineData(StatReportFormat.SSV, typeof(Reports.Formats.SemicolonSeparatedValue))]
|
[InlineData(StatReportFormat.SSV, typeof(Reports.Formats.SemicolonSeparatedValue))]
|
||||||
[InlineData(StatReportFormat.TSV, typeof(Reports.Formats.TabSeparatedValue))]
|
[InlineData(StatReportFormat.TSV, typeof(Reports.Formats.TabSeparatedValue))]
|
||||||
[InlineData((StatReportFormat)0xFF, typeof(Reports.Formats.ConsoleOutput))]
|
[InlineData((StatReportFormat)0xFF, typeof(Reports.Formats.Textfile))]
|
||||||
public void CreateReportTest(StatReportFormat reportFormat, Type expected)
|
public void CreateReportTest(StatReportFormat reportFormat, Type expected)
|
||||||
{
|
{
|
||||||
var report = Parser.CreateReport(reportFormat, []);
|
var report = Parser.CreateReport(reportFormat, []);
|
||||||
|
|||||||
@@ -536,15 +536,15 @@ namespace SabreTools.DatTools
|
|||||||
{
|
{
|
||||||
return statReportFormat switch
|
return statReportFormat switch
|
||||||
{
|
{
|
||||||
StatReportFormat.None => new Reports.Formats.ConsoleOutput(statsList),
|
StatReportFormat.None => new Reports.Formats.Textfile(statsList),
|
||||||
StatReportFormat.Textfile => new Reports.Formats.Textfile(statsList),
|
StatReportFormat.Textfile => new Reports.Formats.Textfile(statsList),
|
||||||
StatReportFormat.CSV => new Reports.Formats.CommaSeparatedValue(statsList),
|
StatReportFormat.CSV => new Reports.Formats.CommaSeparatedValue(statsList),
|
||||||
StatReportFormat.HTML => new Reports.Formats.Html(statsList),
|
StatReportFormat.HTML => new Reports.Formats.Html(statsList),
|
||||||
StatReportFormat.SSV => new Reports.Formats.SemicolonSeparatedValue(statsList),
|
StatReportFormat.SSV => new Reports.Formats.SemicolonSeparatedValue(statsList),
|
||||||
StatReportFormat.TSV => new Reports.Formats.TabSeparatedValue(statsList),
|
StatReportFormat.TSV => new Reports.Formats.TabSeparatedValue(statsList),
|
||||||
|
|
||||||
// We use console output as a backup for generic BaseReport
|
// We use textfile output as a backup for generic BaseReport
|
||||||
_ => new Reports.Formats.ConsoleOutput(statsList),
|
_ => new Reports.Formats.Textfile(statsList),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -220,7 +220,7 @@ namespace SabreTools.DatTools
|
|||||||
datFile.DatStatistics,
|
datFile.DatStatistics,
|
||||||
];
|
];
|
||||||
var consoleOutput = Parser.CreateReport(StatReportFormat.None, statsList);
|
var consoleOutput = Parser.CreateReport(StatReportFormat.None, statsList);
|
||||||
consoleOutput!.WriteToFile(null, true, true);
|
consoleOutput!.WriteToStream(Console.OpenStandardOutput(), true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using SabreTools.DatFiles;
|
using SabreTools.DatFiles;
|
||||||
using SabreTools.IO.Logging;
|
using SabreTools.IO.Logging;
|
||||||
|
|
||||||
@@ -40,6 +42,47 @@ namespace SabreTools.Reports
|
|||||||
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
|
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
|
||||||
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
||||||
/// <returns>True if the report was written correctly, false otherwise</returns>
|
/// <returns>True if the report was written correctly, false otherwise</returns>
|
||||||
public abstract bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false);
|
public bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
||||||
|
{
|
||||||
|
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Try to create the output file
|
||||||
|
FileStream stream = File.Create(outfile ?? string.Empty);
|
||||||
|
if (stream == null)
|
||||||
|
{
|
||||||
|
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write to the stream
|
||||||
|
bool result = WriteToStream(stream, baddumpCol, nodumpCol, throwOnError);
|
||||||
|
|
||||||
|
// Dispose of the stream
|
||||||
|
stream.Dispose();
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (!throwOnError)
|
||||||
|
{
|
||||||
|
_logger.Error(ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
watch.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Write a set of statistics to an input stream
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Stream to write to</param>
|
||||||
|
/// <param name="baddumpCol">True if baddumps should be included in output, false otherwise</param>
|
||||||
|
/// <param name="nodumpCol">True if nodumps should be included in output, false otherwise</param>
|
||||||
|
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
||||||
|
/// <returns>True if the report was written correctly, false otherwise</returns>
|
||||||
|
public abstract bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ using SabreTools.Core.Tools;
|
|||||||
using SabreTools.DatFiles;
|
using SabreTools.DatFiles;
|
||||||
using SabreTools.DatItems;
|
using SabreTools.DatItems;
|
||||||
using SabreTools.Hashing;
|
using SabreTools.Hashing;
|
||||||
using SabreTools.IO.Logging;
|
|
||||||
|
|
||||||
namespace SabreTools.Reports.Formats
|
namespace SabreTools.Reports.Formats
|
||||||
{
|
{
|
||||||
@@ -30,21 +29,11 @@ namespace SabreTools.Reports.Formats
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
public override bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
||||||
{
|
{
|
||||||
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Try to create the output file
|
XmlTextWriter xtw = new(stream, Encoding.UTF8)
|
||||||
FileStream fs = File.Create(outfile ?? string.Empty);
|
|
||||||
if (fs == null)
|
|
||||||
{
|
|
||||||
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
XmlTextWriter xtw = new(fs, Encoding.UTF8)
|
|
||||||
{
|
{
|
||||||
Formatting = Formatting.Indented,
|
Formatting = Formatting.Indented,
|
||||||
IndentChar = '\t',
|
IndentChar = '\t',
|
||||||
@@ -85,17 +74,12 @@ namespace SabreTools.Reports.Formats
|
|||||||
#if NET452_OR_GREATER
|
#if NET452_OR_GREATER
|
||||||
xtw.Dispose();
|
xtw.Dispose();
|
||||||
#endif
|
#endif
|
||||||
fs.Dispose();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (!throwOnError)
|
catch (Exception ex) when (!throwOnError)
|
||||||
{
|
{
|
||||||
_logger.Error(ex);
|
_logger.Error(ex);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
watch.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ using System.Text;
|
|||||||
using SabreTools.DatFiles;
|
using SabreTools.DatFiles;
|
||||||
using SabreTools.DatItems;
|
using SabreTools.DatItems;
|
||||||
using SabreTools.Hashing;
|
using SabreTools.Hashing;
|
||||||
using SabreTools.IO.Logging;
|
|
||||||
using SabreTools.IO.Writers;
|
using SabreTools.IO.Writers;
|
||||||
|
|
||||||
namespace SabreTools.Reports.Formats
|
namespace SabreTools.Reports.Formats
|
||||||
@@ -30,21 +29,11 @@ namespace SabreTools.Reports.Formats
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
public override bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
||||||
{
|
{
|
||||||
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Try to create the output file
|
SeparatedValueWriter svw = new(stream, Encoding.UTF8)
|
||||||
FileStream fs = File.Create(outfile ?? string.Empty);
|
|
||||||
if (fs == null)
|
|
||||||
{
|
|
||||||
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SeparatedValueWriter svw = new(fs, Encoding.UTF8)
|
|
||||||
{
|
{
|
||||||
Separator = _delim,
|
Separator = _delim,
|
||||||
Quotes = true,
|
Quotes = true,
|
||||||
@@ -77,17 +66,12 @@ namespace SabreTools.Reports.Formats
|
|||||||
}
|
}
|
||||||
|
|
||||||
svw.Dispose();
|
svw.Dispose();
|
||||||
fs.Dispose();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (!throwOnError)
|
catch (Exception ex) when (!throwOnError)
|
||||||
{
|
{
|
||||||
_logger.Error(ex);
|
_logger.Error(ex);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
watch.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ using SabreTools.Core.Tools;
|
|||||||
using SabreTools.DatFiles;
|
using SabreTools.DatFiles;
|
||||||
using SabreTools.DatItems;
|
using SabreTools.DatItems;
|
||||||
using SabreTools.Hashing;
|
using SabreTools.Hashing;
|
||||||
using SabreTools.IO.Logging;
|
|
||||||
|
|
||||||
namespace SabreTools.Reports.Formats
|
namespace SabreTools.Reports.Formats
|
||||||
{
|
{
|
||||||
@@ -15,11 +14,6 @@ namespace SabreTools.Reports.Formats
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Textfile : BaseReport
|
public class Textfile : BaseReport
|
||||||
{
|
{
|
||||||
/// <summary>
|
|
||||||
/// Represents if the output goes to console or to a file
|
|
||||||
/// </summary>
|
|
||||||
protected bool _writeToConsole = false;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new report from the filename
|
/// Create a new report from the filename
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -30,21 +24,11 @@ namespace SabreTools.Reports.Formats
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public override bool WriteToFile(string? outfile, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
public override bool WriteToStream(Stream stream, bool baddumpCol, bool nodumpCol, bool throwOnError = false)
|
||||||
{
|
{
|
||||||
InternalStopwatch watch = new($"Writing statistics to '{outfile}");
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Try to create the output file
|
StreamWriter sw = new(stream);
|
||||||
Stream fs = _writeToConsole ? Console.OpenStandardOutput() : File.Create(outfile ?? string.Empty);
|
|
||||||
if (fs == null)
|
|
||||||
{
|
|
||||||
_logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
StreamWriter sw = new(fs);
|
|
||||||
|
|
||||||
// Now process each of the statistics
|
// Now process each of the statistics
|
||||||
for (int i = 0; i < _statistics.Count; i++)
|
for (int i = 0; i < _statistics.Count; i++)
|
||||||
@@ -70,17 +54,12 @@ namespace SabreTools.Reports.Formats
|
|||||||
}
|
}
|
||||||
|
|
||||||
sw.Dispose();
|
sw.Dispose();
|
||||||
fs.Dispose();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) when (!throwOnError)
|
catch (Exception ex) when (!throwOnError)
|
||||||
{
|
{
|
||||||
_logger.Error(ex);
|
_logger.Error(ex);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
{
|
|
||||||
watch.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -133,19 +112,4 @@ namespace SabreTools.Reports.Formats
|
|||||||
sw.Flush();
|
sw.Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Console report format
|
|
||||||
/// </summary>
|
|
||||||
public sealed class ConsoleOutput : Textfile
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Create a new report from the filename
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="statsList">List of statistics objects to set</param>
|
|
||||||
public ConsoleOutput(List<DatStatistics> statsList) : base(statsList)
|
|
||||||
{
|
|
||||||
_writeToConsole = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user