Skip to content

Commit

Permalink
LogDebug() only for DEBUG build configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
MikiraSora committed Aug 29, 2024
1 parent 7fad74a commit 85d090e
Showing 1 changed file with 101 additions and 95 deletions.
196 changes: 101 additions & 95 deletions OngekiFumenEditor/Utils/Log.cs
Original file line number Diff line number Diff line change
@@ -1,112 +1,118 @@
using Caliburn.Micro;
using OngekiFumenEditor.Utils.Logs;
using OngekiFumenEditor.Utils.Logs.DefaultImpls;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using static OngekiFumenEditor.Utils.Logs.ILogOutput;

namespace OngekiFumenEditor.Utils
{
[Export(typeof(Log))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Log
{
private List<ILogOutput> outputs = new List<ILogOutput>();
private IEnumerable<ILogOutput> LogOutputs => outputs;

private StringBuilder sb = new StringBuilder(2048);

private static Log cacheInstance;
public static Log Instance => cacheInstance ?? (cacheInstance = IoC.Get<Log>());
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using Caliburn.Micro;
using OngekiFumenEditor.Utils.Logs;
using static OngekiFumenEditor.Utils.Logs.ILogOutput;

[ImportingConstructor]
public Log([ImportMany] IEnumerable<ILogOutput> outputs)
{
this.outputs.AddRange(outputs);
}
namespace OngekiFumenEditor.Utils;

public void RemoveOutputIfNotExist<T>() where T : ILogOutput
{
outputs.RemoveAll(x => x is T);
}
[Export(typeof(Log))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Log
{
private static Log cacheInstance;
private readonly List<ILogOutput> outputs = new();

public void AddOutputIfNotExist<T>() where T : ILogOutput, new()
{
if (outputs.OfType<T>().Any())
return;
outputs.Add(new T());
}
private readonly StringBuilder sb = new(2048);

internal void Output(Severity severity, string message)
{
foreach (var output in LogOutputs)
output.WriteLog(severity, message);
}
[ImportingConstructor]
public Log([ImportMany] IEnumerable<ILogOutput> outputs)
{
this.outputs.AddRange(outputs);
}

private string BuildLogMessage(string message, Severity severity, bool new_line, bool time, string prefix)
{
lock (sb)
{
sb.Clear();
private IEnumerable<ILogOutput> LogOutputs => outputs;
public static Log Instance => cacheInstance ?? (cacheInstance = IoC.Get<Log>());

sb.AppendFormat("[{0} {1}:{2}]", time ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") : string.Empty, severity.ToString().ToUpper(), Thread.CurrentThread.ManagedThreadId);
public void RemoveOutputIfNotExist<T>() where T : ILogOutput
{
outputs.RemoveAll(x => x is T);
}

if (!string.IsNullOrWhiteSpace(prefix))
sb.AppendFormat("{0}", prefix);
public void AddOutputIfNotExist<T>() where T : ILogOutput, new()
{
if (outputs.OfType<T>().Any())
return;
outputs.Add(new T());
}

sb.AppendFormat(":{0}", message);
internal void Output(Severity severity, string message)
{
foreach (var output in LogOutputs)
output.WriteLog(severity, message);
}

if (new_line)
sb.AppendLine();
private string BuildLogMessage(string message, Severity severity, bool new_line, bool time, string prefix)
{
lock (sb)
{
sb.Clear();

return sb.ToString();
}
}
sb.AppendFormat("[{0} {1}:{2}]", time ? DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") : string.Empty,
severity.ToString().ToUpper(), Thread.CurrentThread.ManagedThreadId);

public static void LogDebug(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Debug;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}
if (!string.IsNullOrWhiteSpace(prefix))
sb.AppendFormat("{0}", prefix);

public static void LogInfo(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Info;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}
sb.AppendFormat(":{0}", message);

public static void LogWarn(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Warn;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}
if (new_line)
sb.AppendLine();

public static void LogError(string message, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Error;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
return sb.ToString();
}
}

public static void LogError(string message, Exception e, bool newLine = true, bool time = true, [CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Error;
var msg = instance.BuildLogMessage($"{message}\nContains exception:{e.Message}\n{e.StackTrace}", severity, newLine, time, prefix);
instance.Output(severity, msg);
}
}
}
[Conditional("DEBUG")]
public static void LogDebug(string message, bool newLine = true, bool time = true,
[CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Debug;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}

public static void LogInfo(string message, bool newLine = true, bool time = true,
[CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Info;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}

public static void LogWarn(string message, bool newLine = true, bool time = true,
[CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Warn;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}

public static void LogError(string message, bool newLine = true, bool time = true,
[CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Error;
var msg = instance.BuildLogMessage(message, severity, newLine, time, prefix);
instance.Output(severity, msg);
}

public static void LogError(string message, Exception e, bool newLine = true, bool time = true,
[CallerMemberName] string prefix = "<Unknown>")
{
var instance = Instance;
var severity = Severity.Error;
var msg = instance.BuildLogMessage($"{message}\nContains exception:{e.Message}\n{e.StackTrace}", severity,
newLine, time, prefix);
instance.Output(severity, msg);
}
}

0 comments on commit 85d090e

Please sign in to comment.