diff --git a/bcf-toolkit.sln.DotSettings.user b/bcf-toolkit.sln.DotSettings.user index a1358d1..d63ccf6 100644 --- a/bcf-toolkit.sln.DotSettings.user +++ b/bcf-toolkit.sln.DotSettings.user @@ -1,5 +1,9 @@  /Users/balintbende/Library/Caches/JetBrains/Rider2024.1/resharper-host/temp/Rider/vAny/CoverageData/_bcf-toolkit.-1315391344/Snapshot/snapshot.utdcvr + <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Solution /> +</SessionState> + @@ -37,6 +41,9 @@ + + + diff --git a/src/bcf-toolkit/Converter/Bcf21/FileWriter.cs b/src/bcf-toolkit/Converter/Bcf21/FileWriter.cs index 4d2593b..6e86e1b 100644 --- a/src/bcf-toolkit/Converter/Bcf21/FileWriter.cs +++ b/src/bcf-toolkit/Converter/Bcf21/FileWriter.cs @@ -101,7 +101,7 @@ public static void SerializeAndWriteBcfToStream( var guid = markup.GetTopic()?.Guid; if (guid == null) { - Console.WriteLine(" - Topic Guid is missing, skipping markup"); + Log.Debug(" - Topic Guid is missing, skipping markup"); continue; } @@ -168,7 +168,7 @@ public static async Task SerializeAndWriteBcfToFolder( var guid = markup.GetTopic()?.Guid; if (guid == null) { - Console.WriteLine( + Log.Debug( " - Topic Guid is missing, skipping markup"); continue; } @@ -210,7 +210,7 @@ public static async Task SerializeAndWriteBcfToFolder( await Task.WhenAll(writeTasks); - Console.WriteLine($"Zipping the output: {target}"); + Log.Debug($"Zipping the output: {target}"); if (File.Exists(target)) File.Delete(target); ZipFile.CreateFromDirectory(tmpFolder, target); diff --git a/src/bcf-toolkit/Converter/Bcf30/FileWriter.cs b/src/bcf-toolkit/Converter/Bcf30/FileWriter.cs index 34453da..d9f55b9 100644 --- a/src/bcf-toolkit/Converter/Bcf30/FileWriter.cs +++ b/src/bcf-toolkit/Converter/Bcf30/FileWriter.cs @@ -120,7 +120,7 @@ public static void SerializeAndWriteBcfToStream(IBcf bcf, ZipArchive zip, var guid = markup.GetTopic()?.Guid; if (guid == null) { - Console.WriteLine(" - Topic Guid is missing, skipping markup"); + Log.Debug(" - Topic Guid is missing, skipping markup"); continue; } @@ -188,7 +188,7 @@ public static async Task SerializeAndWriteBcfToFolder( var guid = markup.GetTopic()?.Guid; if (guid == null) { - Console.WriteLine( + Log.Debug( " - Topic Guid is missing, skipping markup"); continue; } @@ -227,7 +227,7 @@ public static async Task SerializeAndWriteBcfToFolder( await Task.WhenAll(writeTasks); - Console.WriteLine($"Zipping the output: {target}"); + Log.Debug($"Zipping the output: {target}"); if (File.Exists(target)) File.Delete(target); ZipFile.CreateFromDirectory(tmpFolder, target); diff --git a/src/bcf-toolkit/Log.cs b/src/bcf-toolkit/Log.cs new file mode 100644 index 0000000..0be8650 --- /dev/null +++ b/src/bcf-toolkit/Log.cs @@ -0,0 +1,74 @@ +using System; + +namespace BcfToolkit; + +/// +/// Provides logging for different levels of messages. +/// +public static class Log { + /// + /// Delegate for handling log messages. + /// + public delegate void LogHandler(string message); + + private static LogHandler? LogDebug { get; set; } + private static LogHandler? LogInfo { get; set; } + private static LogHandler? LogWarning { get; set; } + private static LogHandler? LogError { get; set; } + + /// + /// Configuring the handlers with custom logging functions. + /// + public static void Configure( + LogHandler? debugHandler = null, + LogHandler? infoHandler = null, + LogHandler? warningHandler = null, + LogHandler? errorHandler = null) { + LogDebug = debugHandler; + LogInfo = infoHandler; + LogWarning = warningHandler; + LogError = errorHandler; + } + + /// + /// Configuring the handlers with `Console.WriteLine`. + /// + public static void ConfigureDefault() { + LogDebug = Console.WriteLine; + LogInfo = Console.WriteLine; + LogWarning = Console.WriteLine; + LogError = Console.WriteLine; + } + + /// + /// Write a log event with the debug level. + /// + /// Message describing the event. + public static void Debug(string message) { + LogDebug?.Invoke(message); + } + + /// + /// Write a log event with the information level. + /// + /// Message describing the event. + public static void Info(string message) { + LogInfo?.Invoke(message); + } + + /// + /// Write a log event with the warning level. + /// + /// Message describing the event. + public static void Warning(string message) { + LogWarning?.Invoke(message); + } + + /// + /// Write a log event with the error level. + /// + /// Message describing the event. + public static void Error(string message) { + LogError?.Invoke(message); + } +} \ No newline at end of file diff --git a/src/bcf-toolkit/Program.cs b/src/bcf-toolkit/Program.cs index 22e1758..89c80a8 100644 --- a/src/bcf-toolkit/Program.cs +++ b/src/bcf-toolkit/Program.cs @@ -1,6 +1,8 @@ using System; using System.Threading.Tasks; using System.CommandLine; +using Serilog; +using Serilog.Events; namespace BcfToolkit; @@ -9,6 +11,18 @@ private static async Task Main(string[] args) { await HandleArguments(args); } private static async Task HandleArguments(string[] args) { + // Logger setup + Log.ConfigureDefault(); + + Serilog.Log.Logger = new LoggerConfiguration() + .MinimumLevel.Debug() + .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) + .Enrich.FromLogContext() + .CreateLogger(); + + Log.Configure(Serilog.Log.Debug, null, null, Serilog.Log.Error); + + var sourcePathOption = new Option( name: "--source", description: "The absolute path of the source file.") { IsRequired = true }; @@ -43,9 +57,7 @@ private static async Task DoRootCommand(InputArguments arguments) { await worker.Convert(arguments.SourcePath, arguments.Target); } catch (Exception e) { - var errorWriter = Console.Error; - await errorWriter.WriteLineAsync(e.Message); - await errorWriter.WriteLineAsync(e.StackTrace); + Log.Error(e.Message); Environment.Exit(9); } diff --git a/src/bcf-toolkit/Utils/BcfExtensions.cs b/src/bcf-toolkit/Utils/BcfExtensions.cs index 0f4e2fc..ca97df2 100644 --- a/src/bcf-toolkit/Utils/BcfExtensions.cs +++ b/src/bcf-toolkit/Utils/BcfExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Linq; @@ -43,7 +44,7 @@ public static class BcfExtensions { throw new ArgumentException("Source stream is not readable."); var objType = typeof(TMarkup); - Console.WriteLine($"\nProcessing {objType.Name}\n"); + Log.Debug($"\nProcessing {objType.Name}\n"); // A thread-safe storage for the parsed topics. var markups = new ConcurrentBag(); @@ -76,7 +77,7 @@ public static class BcfExtensions { foreach (var entry in topicEntries) { var isLastTopicEntry = entry == topicEntries.Last(); - Console.WriteLine(entry.FullName); + Log.Debug(entry.FullName); // This sets the folder context var uuid = entry.FullName.Split("/")[0]; @@ -104,7 +105,7 @@ public static class BcfExtensions { else if (entry.IsBcfViewpoint()) { if (viewpoint != null) // TODO: No support for multiple viewpoints! - Console.WriteLine("No support for multiple viewpoints!"); + Log.Debug("No support for multiple viewpoints!"); //continue; var document = await XDocument.LoadAsync( entry.Open(), @@ -117,7 +118,7 @@ public static class BcfExtensions { else if (entry.IsSnapshot()) { if (snapshot != null) // TODO: No support for multiple snapshots! - Console.WriteLine("No support for multiple snapshots!"); + Log.Debug("No support for multiple snapshots!"); //continue; snapshot = entry.Snapshot(); } @@ -242,7 +243,7 @@ private static Task ParseRequired( throw new ArgumentException("Source stream is not readable."); var objType = typeof(T); - Console.WriteLine($"\nProcessing {objType.Name}\n"); + Log.Debug($"\nProcessing {objType.Name}\n"); var obj = default(T); @@ -252,7 +253,7 @@ private static Task ParseRequired( foreach (var entry in archive.Entries) { if (!filterFn(entry)) continue; - Console.WriteLine(entry.FullName); + Log.Debug(entry.FullName); var document = await XDocument.LoadAsync( entry.Open(), @@ -307,7 +308,7 @@ public static Task SerializeAndWriteXmlFile(string folder, string file, foreach (var entry in archive.Entries) { if (!entry.IsVersion()) continue; - Console.WriteLine(entry.FullName); + Log.Debug(entry.FullName); var document = await XDocument.LoadAsync( entry.Open(), diff --git a/src/bcf-toolkit/Utils/JsonExtensions.cs b/src/bcf-toolkit/Utils/JsonExtensions.cs index 498cde2..0638cce 100644 --- a/src/bcf-toolkit/Utils/JsonExtensions.cs +++ b/src/bcf-toolkit/Utils/JsonExtensions.cs @@ -54,11 +54,11 @@ public static Task> ParseMarkups( foreach (var file in topicFiles) { if (file.EndsWith("json") == false) { - Console.WriteLine($" - File is not json, skipping ${file}"); + Log.Debug($" - File is not json, skipping ${file}"); continue; } - Console.WriteLine($" - Processing {file}"); + Log.Debug($" - Processing {file}"); var markup = await ParseObject(file); markups.Add(markup); @@ -147,7 +147,7 @@ public static Task GetVersionFromJson(string source) { return BcfVersion.TryParse(version); } catch (Exception e) { - Console.WriteLine("Unable to detect the bcf version of the json. Assumed version is 2.1."); + Log.Error("Unable to detect the bcf version of the json. Assumed version is 2.1."); return BcfVersionEnum.Bcf21; } }); diff --git a/src/bcf-toolkit/bcf-toolkit.csproj b/src/bcf-toolkit/bcf-toolkit.csproj index bec0a86..f2e2364 100644 --- a/src/bcf-toolkit/bcf-toolkit.csproj +++ b/src/bcf-toolkit/bcf-toolkit.csproj @@ -25,6 +25,7 @@ +