aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Lm/ILogger.cs
blob: 070cf9aed114c3e4f92afbf5f0875012d0b79d5c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using Ryujinx.Common.Logging;
using Ryujinx.HLE.HOS.Ipc;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Ryujinx.HLE.HOS.Services.Lm
{
    class ILogger : IpcService
    {
        private Dictionary<int, ServiceProcessRequest> _commands;

        public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;

        public ILogger()
        {
            _commands = new Dictionary<int, ServiceProcessRequest>
            {
                { 0, Log }
            };
        }

        public long Log(ServiceCtx context)
        {

            (long bufPos, long bufSize) = context.Request.GetBufferType0x21();
            byte[] logBuffer = context.Memory.ReadBytes(bufPos, bufSize);

            using (MemoryStream ms = new MemoryStream(logBuffer))
            {
                BinaryReader reader = new BinaryReader(ms);

                long  pid           = reader.ReadInt64();
                long  threadContext = reader.ReadInt64();
                short flags         = reader.ReadInt16();
                byte  level         = reader.ReadByte();
                byte  verbosity     = reader.ReadByte();
                int   payloadLength = reader.ReadInt32();

                StringBuilder sb = new StringBuilder();

                sb.AppendLine("Guest log:");

                while (ms.Position < ms.Length)
                {
                    byte type = reader.ReadByte();
                    byte size = reader.ReadByte();

                    LmLogField field = (LmLogField)type;

                    string fieldStr = string.Empty;

                    if (field == LmLogField.Start)
                    {
                        reader.ReadBytes(size);

                        continue;
                    }
                    else if (field == LmLogField.Stop)
                    {
                        break;
                    }
                    else if (field == LmLogField.Line)
                    {
                        fieldStr = $"{field}: {reader.ReadInt32()}";
                    }
                    else if (field == LmLogField.DropCount)
                    {
                        fieldStr = $"{field}: {reader.ReadInt64()}";
                    }
                    else if (field == LmLogField.Time)
                    {
                        fieldStr = $"{field}: {reader.ReadInt64()}s";
                    }
                    else if (field < LmLogField.Count)
                    {
                        fieldStr = $"{field}: '{Encoding.UTF8.GetString(reader.ReadBytes(size)).TrimEnd()}'";
                    }
                    else
                    {
                        fieldStr = $"Field{field}: '{Encoding.UTF8.GetString(reader.ReadBytes(size)).TrimEnd()}'";
                    }

                    sb.AppendLine(" " + fieldStr);
                }

                string text = sb.ToString();

                switch((LmLogLevel)level)
                {
                    case LmLogLevel.Trace:    Logger.PrintDebug  (LogClass.ServiceLm, text); break;
                    case LmLogLevel.Info:     Logger.PrintInfo   (LogClass.ServiceLm, text); break;
                    case LmLogLevel.Warning:  Logger.PrintWarning(LogClass.ServiceLm, text); break;
                    case LmLogLevel.Error:    Logger.PrintError  (LogClass.ServiceLm, text); break;
                    case LmLogLevel.Critical: Logger.PrintError  (LogClass.ServiceLm, text); break;
                }
            }

            return 0;
        }
    }
}