ILogger.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.Logging;
  3. using Ryujinx.Core.OsHle.Ipc;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. namespace Ryujinx.Core.OsHle.Services.Lm
  8. {
  9. class ILogger : IpcService
  10. {
  11. private Dictionary<int, ServiceProcessRequest> m_Commands;
  12. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  13. public ILogger()
  14. {
  15. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  16. {
  17. { 0, Log }
  18. };
  19. }
  20. public long Log(ServiceCtx Context)
  21. {
  22. byte[] LogBuffer = AMemoryHelper.ReadBytes(
  23. Context.Memory,
  24. Context.Request.PtrBuff[0].Position,
  25. Context.Request.PtrBuff[0].Size);
  26. using (MemoryStream MS = new MemoryStream(LogBuffer))
  27. {
  28. BinaryReader Reader = new BinaryReader(MS);
  29. long Pid = Reader.ReadInt64();
  30. long ThreadContext = Reader.ReadInt64();
  31. short Flags = Reader.ReadInt16();
  32. byte Level = Reader.ReadByte();
  33. byte Verbosity = Reader.ReadByte();
  34. int PayloadLength = Reader.ReadInt32();
  35. StringBuilder SB = new StringBuilder();
  36. SB.AppendLine("Guest log:");
  37. while (MS.Position < MS.Length)
  38. {
  39. byte Type = Reader.ReadByte();
  40. byte Size = Reader.ReadByte();
  41. LmLogField Field = (LmLogField)Type;
  42. string FieldStr = string.Empty;
  43. if (Field == LmLogField.Skip)
  44. {
  45. Reader.ReadByte();
  46. continue;
  47. }
  48. else if (Field == LmLogField.Line)
  49. {
  50. FieldStr = Field + ": " + Reader.ReadInt32();
  51. }
  52. else
  53. {
  54. FieldStr = Field + ": \"" + Encoding.UTF8.GetString(Reader.ReadBytes(Size)) + "\"";
  55. }
  56. SB.AppendLine(" " + FieldStr);
  57. }
  58. string Text = SB.ToString();
  59. switch((LmLogLevel)Level)
  60. {
  61. case LmLogLevel.Trace: Context.Ns.Log.PrintDebug (LogClass.ServiceLm, Text); break;
  62. case LmLogLevel.Info: Context.Ns.Log.PrintInfo (LogClass.ServiceLm, Text); break;
  63. case LmLogLevel.Warning: Context.Ns.Log.PrintWarning(LogClass.ServiceLm, Text); break;
  64. case LmLogLevel.Error: Context.Ns.Log.PrintError (LogClass.ServiceLm, Text); break;
  65. case LmLogLevel.Critical: Context.Ns.Log.PrintError (LogClass.ServiceLm, Text); break;
  66. }
  67. }
  68. return 0;
  69. }
  70. }
  71. }