2023-01-04 14:15:45 -08:00
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
using Ryujinx.Common.Memory;
|
|
|
|
using Ryujinx.Horizon.Common;
|
|
|
|
using Ryujinx.Horizon.Sdk.Lm;
|
|
|
|
using Ryujinx.Horizon.Sdk.Sf;
|
|
|
|
using Ryujinx.Horizon.Sdk.Sf.Hipc;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Text;
|
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
namespace Ryujinx.Horizon.LogManager.Ipc
|
2023-01-04 14:15:45 -08:00
|
|
|
{
|
2023-01-08 04:13:39 -08:00
|
|
|
partial class LmLogger : ILmLogger
|
2023-01-04 14:15:45 -08:00
|
|
|
{
|
2023-01-08 04:13:39 -08:00
|
|
|
private readonly LogService _log;
|
|
|
|
private readonly ulong _pid;
|
2023-01-04 14:15:45 -08:00
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
public LmLogger(LogService log, ulong pid)
|
2023-01-04 14:15:45 -08:00
|
|
|
{
|
|
|
|
_log = log;
|
2023-01-08 04:13:39 -08:00
|
|
|
_pid = pid;
|
2023-01-04 14:15:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
[CmifCommand(0)]
|
|
|
|
public Result Log([Buffer(HipcBufferFlags.In | HipcBufferFlags.AutoSelect)] Span<byte> message)
|
|
|
|
{
|
2023-01-08 04:13:39 -08:00
|
|
|
if (!SetProcessId(message, _pid))
|
2023-01-04 14:15:45 -08:00
|
|
|
{
|
|
|
|
return Result.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Guest?.Print(LogClass.ServiceLm, LogImpl(message));
|
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
}
|
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
[CmifCommand(1)] // 3.0.0+
|
2023-01-04 14:15:45 -08:00
|
|
|
public Result SetDestination(LogDestination destination)
|
|
|
|
{
|
|
|
|
_log.LogDestination = destination;
|
|
|
|
|
|
|
|
return Result.Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool SetProcessId(Span<byte> message, ulong processId)
|
|
|
|
{
|
|
|
|
ref LogPacketHeader header = ref MemoryMarshal.Cast<byte, LogPacketHeader>(message)[0];
|
|
|
|
|
|
|
|
uint expectedMessageSize = (uint)Unsafe.SizeOf<LogPacketHeader>() + header.PayloadSize;
|
|
|
|
if (expectedMessageSize != (uint)message.Length)
|
|
|
|
{
|
|
|
|
Logger.Warning?.Print(LogClass.ServiceLm, $"Invalid message size (expected 0x{expectedMessageSize:X} but got 0x{message.Length:X}).");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
header.ProcessId = processId;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string LogImpl(ReadOnlySpan<byte> message)
|
|
|
|
{
|
2023-01-08 04:13:39 -08:00
|
|
|
SpanReader reader = new(message);
|
|
|
|
LogPacketHeader header = reader.Read<LogPacketHeader>();
|
|
|
|
StringBuilder builder = new();
|
2023-01-04 14:15:45 -08:00
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
builder.AppendLine($"Guest Log:\n Log level: {header.Severity}");
|
2023-01-04 14:15:45 -08:00
|
|
|
|
|
|
|
while (reader.Length > 0)
|
|
|
|
{
|
|
|
|
int type = ReadUleb128(ref reader);
|
|
|
|
int size = ReadUleb128(ref reader);
|
|
|
|
|
|
|
|
LogDataChunkKey field = (LogDataChunkKey)type;
|
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
string fieldStr;
|
2023-01-04 14:15:45 -08:00
|
|
|
|
|
|
|
if (field == LogDataChunkKey.Start)
|
|
|
|
{
|
|
|
|
reader.Skip(size);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (field == LogDataChunkKey.Stop)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (field == LogDataChunkKey.Line)
|
|
|
|
{
|
|
|
|
fieldStr = $"{field}: {reader.Read<int>()}";
|
|
|
|
}
|
|
|
|
else if (field == LogDataChunkKey.DropCount)
|
|
|
|
{
|
|
|
|
fieldStr = $"{field}: {reader.Read<long>()}";
|
|
|
|
}
|
|
|
|
else if (field == LogDataChunkKey.Time)
|
|
|
|
{
|
|
|
|
fieldStr = $"{field}: {reader.Read<long>()}s";
|
|
|
|
}
|
|
|
|
else if (field < LogDataChunkKey.Count)
|
|
|
|
{
|
|
|
|
fieldStr = $"{field}: '{Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd()}'";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fieldStr = $"Field{field}: '{Encoding.UTF8.GetString(reader.GetSpan(size)).TrimEnd()}'";
|
|
|
|
}
|
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
builder.AppendLine($" {fieldStr}");
|
2023-01-04 14:15:45 -08:00
|
|
|
}
|
|
|
|
|
2023-01-08 04:13:39 -08:00
|
|
|
return builder.ToString();
|
2023-01-04 14:15:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static int ReadUleb128(ref SpanReader reader)
|
|
|
|
{
|
|
|
|
int result = 0;
|
2023-01-08 04:13:39 -08:00
|
|
|
int count = 0;
|
2023-01-04 14:15:45 -08:00
|
|
|
|
|
|
|
byte encoded;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
encoded = reader.Read<byte>();
|
|
|
|
|
|
|
|
result += (encoded & 0x7F) << (7 * count);
|
|
|
|
|
|
|
|
count++;
|
|
|
|
} while ((encoded & 0x80) != 0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 04:13:39 -08:00
|
|
|
}
|