Logger: Implement LogFormatters
This commit is contained in:
parent
748e72c496
commit
bb7c08b046
6 changed files with 81 additions and 95 deletions
53
Ryujinx.Common/Logging/Formatters/DefaultLogFormatter.cs
Normal file
53
Ryujinx.Common/Logging/Formatters/DefaultLogFormatter.cs
Normal file
|
@ -0,0 +1,53 @@
|
|||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.Common.Logging
|
||||
{
|
||||
internal class DefaultLogFormatter : ILogFormatter
|
||||
{
|
||||
private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();
|
||||
|
||||
public string Format(LogEventArgs args)
|
||||
{
|
||||
StringBuilder sb = _stringBuilderPool.Allocate();
|
||||
|
||||
try
|
||||
{
|
||||
sb.Clear();
|
||||
|
||||
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", args.Time);
|
||||
sb.Append(" | ");
|
||||
sb.AppendFormat("{0:d4}", args.ThreadId);
|
||||
sb.Append(' ');
|
||||
sb.Append(args.Message);
|
||||
|
||||
if (args.Data != null)
|
||||
{
|
||||
PropertyInfo[] props = args.Data.GetType().GetProperties();
|
||||
|
||||
sb.Append(' ');
|
||||
|
||||
foreach (var prop in props)
|
||||
{
|
||||
sb.Append(prop.Name);
|
||||
sb.Append(": ");
|
||||
sb.Append(prop.GetValue(args.Data));
|
||||
sb.Append(" - ");
|
||||
}
|
||||
|
||||
// We remove the final '-' from the string
|
||||
if (props.Length > 0)
|
||||
{
|
||||
sb.Remove(sb.Length - 3, 3);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stringBuilderPool.Release(sb);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
Ryujinx.Common/Logging/Formatters/ILogFormatter.cs
Normal file
7
Ryujinx.Common/Logging/Formatters/ILogFormatter.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Ryujinx.Common.Logging
|
||||
{
|
||||
interface ILogFormatter
|
||||
{
|
||||
string Format(LogEventArgs args);
|
||||
}
|
||||
}
|
|
@ -1,17 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.Common.Logging
|
||||
{
|
||||
public class ConsoleLogTarget : ILogTarget
|
||||
{
|
||||
private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();
|
||||
|
||||
private static readonly ConcurrentDictionary<LogLevel, ConsoleColor> _logColors;
|
||||
|
||||
private readonly ILogFormatter _formatter;
|
||||
|
||||
static ConsoleLogTarget()
|
||||
{
|
||||
_logColors = new ConcurrentDictionary<LogLevel, ConsoleColor> {
|
||||
|
@ -22,57 +19,24 @@ namespace Ryujinx.Common.Logging
|
|||
};
|
||||
}
|
||||
|
||||
public void Log(object sender, LogEventArgs e)
|
||||
public ConsoleLogTarget()
|
||||
{
|
||||
StringBuilder sb = _stringBuilderPool.Allocate();
|
||||
_formatter = new DefaultLogFormatter();
|
||||
}
|
||||
|
||||
try
|
||||
public void Log(object sender, LogEventArgs args)
|
||||
{
|
||||
if (_logColors.TryGetValue(args.Level, out ConsoleColor color))
|
||||
{
|
||||
sb.Clear();
|
||||
Console.ForegroundColor = color;
|
||||
|
||||
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time);
|
||||
sb.Append(" | ");
|
||||
sb.AppendFormat("{0:d4}", e.ThreadId);
|
||||
sb.Append(' ');
|
||||
sb.Append(e.Message);
|
||||
Console.WriteLine(_formatter.Format(args));
|
||||
|
||||
if (e.Data != null)
|
||||
{
|
||||
PropertyInfo[] props = e.Data.GetType().GetProperties();
|
||||
|
||||
sb.Append(' ');
|
||||
|
||||
foreach (var prop in props)
|
||||
{
|
||||
sb.Append(prop.Name);
|
||||
sb.Append(": ");
|
||||
sb.Append(prop.GetValue(e.Data));
|
||||
sb.Append(" - ");
|
||||
}
|
||||
|
||||
// We remove the final '-' from the string
|
||||
if (props.Length > 0)
|
||||
{
|
||||
sb.Remove(sb.Length - 3, 3);
|
||||
}
|
||||
}
|
||||
|
||||
if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
|
||||
{
|
||||
Console.ForegroundColor = color;
|
||||
|
||||
Console.WriteLine(sb.ToString());
|
||||
|
||||
Console.ResetColor();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(sb.ToString());
|
||||
}
|
||||
Console.ResetColor();
|
||||
}
|
||||
finally
|
||||
else
|
||||
{
|
||||
_stringBuilderPool.Release(sb);
|
||||
Console.WriteLine(_formatter.Format(args));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace Ryujinx.Common.Logging
|
||||
|
@ -8,7 +7,8 @@ namespace Ryujinx.Common.Logging
|
|||
{
|
||||
private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();
|
||||
|
||||
private readonly StreamWriter _logWriter;
|
||||
private readonly StreamWriter _logWriter;
|
||||
private readonly ILogFormatter _formatter;
|
||||
|
||||
public FileLogTarget(string path)
|
||||
: this(path, FileShare.Read, FileMode.Append)
|
||||
|
@ -17,50 +17,13 @@ namespace Ryujinx.Common.Logging
|
|||
public FileLogTarget(string path, FileShare fileShare, FileMode fileMode)
|
||||
{
|
||||
_logWriter = new StreamWriter(File.Open(path, fileMode, FileAccess.Write, fileShare));
|
||||
_formatter = new DefaultLogFormatter();
|
||||
}
|
||||
|
||||
public void Log(object sender, LogEventArgs e)
|
||||
public void Log(object sender, LogEventArgs args)
|
||||
{
|
||||
StringBuilder sb = _stringBuilderPool.Allocate();
|
||||
|
||||
try
|
||||
{
|
||||
sb.Clear();
|
||||
|
||||
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time);
|
||||
sb.Append(" | ");
|
||||
sb.AppendFormat("{0:d4}", e.ThreadId);
|
||||
sb.Append(' ');
|
||||
sb.Append(e.Message);
|
||||
|
||||
if (e.Data != null)
|
||||
{
|
||||
PropertyInfo[] props = e.Data.GetType().GetProperties();
|
||||
|
||||
sb.Append(' ');
|
||||
|
||||
foreach (var prop in props)
|
||||
{
|
||||
sb.Append(prop.Name);
|
||||
sb.Append(": ");
|
||||
sb.Append(prop.GetValue(e.Data));
|
||||
sb.Append(" - ");
|
||||
}
|
||||
|
||||
// We remove the final '-' from the string
|
||||
if (props.Length > 0)
|
||||
{
|
||||
sb.Remove(sb.Length - 3, 3);
|
||||
}
|
||||
}
|
||||
|
||||
_logWriter.WriteLine(sb.ToString());
|
||||
_logWriter.Flush();
|
||||
}
|
||||
finally
|
||||
{
|
||||
_stringBuilderPool.Release(sb);
|
||||
}
|
||||
_logWriter.WriteLine(_formatter.Format(args));
|
||||
_logWriter.Flush();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
|
|
|
@ -4,6 +4,6 @@ namespace Ryujinx.Common.Logging
|
|||
{
|
||||
public interface ILogTarget : IDisposable
|
||||
{
|
||||
void Log(object sender, LogEventArgs e);
|
||||
void Log(object sender, LogEventArgs args);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using Utf8Json;
|
||||
|
||||
namespace Ryujinx.Common.Logging
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue