2023-03-21 23:14:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2019-02-11 12:00:32 +00:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Common.Logging
|
|
|
|
|
{
|
|
|
|
|
internal class DefaultLogFormatter : ILogFormatter
|
|
|
|
|
{
|
2023-03-21 23:14:46 +00:00
|
|
|
|
private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();
|
2019-02-11 12:00:32 +00:00
|
|
|
|
|
|
|
|
|
public string Format(LogEventArgs args)
|
|
|
|
|
{
|
2023-03-21 23:14:46 +00:00
|
|
|
|
StringBuilder sb = _stringBuilderPool.Allocate();
|
2019-02-11 12:00:32 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
sb.Clear();
|
|
|
|
|
|
2023-03-21 23:14:46 +00:00
|
|
|
|
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", args.Time);
|
2020-08-03 23:32:53 +00:00
|
|
|
|
sb.Append($" |{args.Level.ToString()[0]}| ");
|
2020-01-13 00:21:54 +00:00
|
|
|
|
|
|
|
|
|
if (args.ThreadName != null)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(args.ThreadName);
|
|
|
|
|
sb.Append(' ');
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 12:00:32 +00:00
|
|
|
|
sb.Append(args.Message);
|
|
|
|
|
|
2023-03-21 23:14:46 +00:00
|
|
|
|
if (args.Data != null)
|
2019-02-11 12:00:32 +00:00
|
|
|
|
{
|
2023-03-21 23:14:46 +00:00
|
|
|
|
PropertyInfo[] props = args.Data.GetType().GetProperties();
|
|
|
|
|
|
|
|
|
|
sb.Append(" {");
|
|
|
|
|
|
|
|
|
|
foreach (var prop in props)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(prop.Name);
|
|
|
|
|
sb.Append(": ");
|
|
|
|
|
|
|
|
|
|
if (typeof(Array).IsAssignableFrom(prop.PropertyType))
|
|
|
|
|
{
|
|
|
|
|
Array array = (Array)prop.GetValue(args.Data);
|
|
|
|
|
foreach (var item in array)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(item.ToString());
|
|
|
|
|
sb.Append(", ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (array.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
sb.Remove(sb.Length - 2, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.Append('}');
|
2019-02-11 12:00:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2023-03-21 23:14:46 +00:00
|
|
|
|
_stringBuilderPool.Release(sb);
|
2019-02-11 12:00:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|