Skip to content

Commit

Permalink
[BMSPT-295] changed log for new Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
BalintBende committed Jul 4, 2024
1 parent 31d5e3b commit e8a367a
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 19 deletions.
7 changes: 7 additions & 0 deletions bcf-toolkit.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">/Users/balintbende/Library/Caches/JetBrains/Rider2024.1/resharper-host/temp/Rider/vAny/CoverageData/_bcf-toolkit.-1315391344/Snapshot/snapshot.utdcvr</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=55118c1b_002Dff37_002D4440_002Db322_002D21d585baf52f/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>




Expand Down Expand Up @@ -37,6 +41,9 @@









Expand Down
6 changes: 3 additions & 3 deletions src/bcf-toolkit/Converter/Bcf21/FileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public static async Task<string> SerializeAndWriteBcfToFolder(

var guid = markup.GetTopic()?.Guid;
if (guid == null) {
Console.WriteLine(
Log.Debug(
" - Topic Guid is missing, skipping markup");
continue;
}
Expand Down Expand Up @@ -210,7 +210,7 @@ public static async Task<string> 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);

Expand Down
6 changes: 3 additions & 3 deletions src/bcf-toolkit/Converter/Bcf30/FileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -188,7 +188,7 @@ public static async Task<string> SerializeAndWriteBcfToFolder(

var guid = markup.GetTopic()?.Guid;
if (guid == null) {
Console.WriteLine(
Log.Debug(
" - Topic Guid is missing, skipping markup");
continue;
}
Expand Down Expand Up @@ -227,7 +227,7 @@ public static async Task<string> 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);

Expand Down
74 changes: 74 additions & 0 deletions src/bcf-toolkit/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;

namespace BcfToolkit;

/// <summary>
/// Provides logging for different levels of messages.
/// </summary>
public static class Log {
/// <summary>
/// Delegate for handling log messages.
/// </summary>
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; }

/// <summary>
/// Configuring the handlers with custom logging functions.
/// </summary>
public static void Configure(
LogHandler? debugHandler = null,
LogHandler? infoHandler = null,
LogHandler? warningHandler = null,
LogHandler? errorHandler = null) {
LogDebug = debugHandler;
LogInfo = infoHandler;
LogWarning = warningHandler;
LogError = errorHandler;
}

/// <summary>
/// Configuring the handlers with `Console.WriteLine`.
/// </summary>
public static void ConfigureDefault() {
LogDebug = Console.WriteLine;
LogInfo = Console.WriteLine;
LogWarning = Console.WriteLine;
LogError = Console.WriteLine;
}

/// <summary>
/// Write a log event with the debug level.
/// </summary>
/// <param name="message">Message describing the event.</param>
public static void Debug(string message) {
LogDebug?.Invoke(message);
}

/// <summary>
/// Write a log event with the information level.
/// </summary>
/// <param name="message">Message describing the event.</param>
public static void Info(string message) {
LogInfo?.Invoke(message);
}

/// <summary>
/// Write a log event with the warning level.
/// </summary>
/// <param name="message">Message describing the event.</param>
public static void Warning(string message) {
LogWarning?.Invoke(message);
}

/// <summary>
/// Write a log event with the error level.
/// </summary>
/// <param name="message">Message describing the event.</param>
public static void Error(string message) {
LogError?.Invoke(message);
}
}
18 changes: 15 additions & 3 deletions src/bcf-toolkit/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading.Tasks;
using System.CommandLine;
using Serilog;
using Serilog.Events;

namespace BcfToolkit;

Expand All @@ -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<string>(
name: "--source",
description: "The absolute path of the source file.") { IsRequired = true };
Expand Down Expand Up @@ -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);
}

Expand Down
15 changes: 8 additions & 7 deletions src/bcf-toolkit/Utils/BcfExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand Down Expand Up @@ -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<TMarkup>();
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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(),
Expand All @@ -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();
}
Expand Down Expand Up @@ -242,7 +243,7 @@ private static Task<T> ParseRequired<T>(
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);

Expand All @@ -252,7 +253,7 @@ private static Task<T> ParseRequired<T>(
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(),
Expand Down Expand Up @@ -307,7 +308,7 @@ public static Task SerializeAndWriteXmlFile<T>(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(),
Expand Down
6 changes: 3 additions & 3 deletions src/bcf-toolkit/Utils/JsonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public static Task<ConcurrentBag<TMarkup>> ParseMarkups<TMarkup>(
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<TMarkup>(file);
markups.Add(markup);
Expand Down Expand Up @@ -147,7 +147,7 @@ public static Task<BcfVersionEnum> GetVersionFromJson(string source) {
return BcfVersion.TryParse(version);
}
catch (Exception e) {

Check warning on line 149 in src/bcf-toolkit/Utils/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / Running tests

The variable 'e' is declared but never used

Check warning on line 149 in src/bcf-toolkit/Utils/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / Running tests

The variable 'e' is declared but never used

Check warning on line 149 in src/bcf-toolkit/Utils/JsonExtensions.cs

View workflow job for this annotation

GitHub Actions / Running tests

The variable 'e' is declared but never used
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;
}
});
Expand Down
1 change: 1 addition & 0 deletions src/bcf-toolkit/bcf-toolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="Json.Net" Version="1.0.33" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RecursiveDataAnnotationsValidation" Version="2.0.0" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
Expand Down

0 comments on commit e8a367a

Please sign in to comment.