Logger: Implement LogFormatters

This commit is contained in:
jduncanator 2019-02-04 18:24:18 +11:00
commit bb7c08b046
6 changed files with 81 additions and 95 deletions

View 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);
}
}
}
}

View file

@ -0,0 +1,7 @@
namespace Ryujinx.Common.Logging
{
interface ILogFormatter
{
string Format(LogEventArgs args);
}
}

View file

@ -1,17 +1,14 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Ryujinx.Common.Logging namespace Ryujinx.Common.Logging
{ {
public class ConsoleLogTarget : ILogTarget public class ConsoleLogTarget : ILogTarget
{ {
private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();
private static readonly ConcurrentDictionary<LogLevel, ConsoleColor> _logColors; private static readonly ConcurrentDictionary<LogLevel, ConsoleColor> _logColors;
private readonly ILogFormatter _formatter;
static ConsoleLogTarget() static ConsoleLogTarget()
{ {
_logColors = new ConcurrentDictionary<LogLevel, ConsoleColor> { _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
{
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 public void Log(object sender, LogEventArgs args)
if (props.Length > 0)
{ {
sb.Remove(sb.Length - 3, 3); if (_logColors.TryGetValue(args.Level, out ConsoleColor color))
}
}
if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
{ {
Console.ForegroundColor = color; Console.ForegroundColor = color;
Console.WriteLine(sb.ToString()); Console.WriteLine(_formatter.Format(args));
Console.ResetColor(); Console.ResetColor();
} }
else else
{ {
Console.WriteLine(sb.ToString()); Console.WriteLine(_formatter.Format(args));
}
}
finally
{
_stringBuilderPool.Release(sb);
} }
} }

View file

@ -1,5 +1,4 @@
using System.IO; using System.IO;
using System.Reflection;
using System.Text; using System.Text;
namespace Ryujinx.Common.Logging namespace Ryujinx.Common.Logging
@ -9,6 +8,7 @@ namespace Ryujinx.Common.Logging
private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>(); 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) public FileLogTarget(string path)
: this(path, FileShare.Read, FileMode.Append) : this(path, FileShare.Read, FileMode.Append)
@ -17,51 +17,14 @@ namespace Ryujinx.Common.Logging
public FileLogTarget(string path, FileShare fileShare, FileMode fileMode) public FileLogTarget(string path, FileShare fileShare, FileMode fileMode)
{ {
_logWriter = new StreamWriter(File.Open(path, fileMode, FileAccess.Write, fileShare)); _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(); _logWriter.WriteLine(_formatter.Format(args));
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(); _logWriter.Flush();
} }
finally
{
_stringBuilderPool.Release(sb);
}
}
public void Dispose() public void Dispose()
{ {

View file

@ -4,6 +4,6 @@ namespace Ryujinx.Common.Logging
{ {
public interface ILogTarget : IDisposable public interface ILogTarget : IDisposable
{ {
void Log(object sender, LogEventArgs e); void Log(object sender, LogEventArgs args);
} }
} }

View file

@ -1,5 +1,4 @@
using System; using System.IO;
using System.IO;
using Utf8Json; using Utf8Json;
namespace Ryujinx.Common.Logging namespace Ryujinx.Common.Logging