aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Common/Logging/Targets/JsonLogTarget.cs
blob: 1873dc606320ecc9f49f3f016cfeb99432d5d1d7 (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
using Ryujinx.Common.Utilities;
using System;
using System.IO;

namespace Ryujinx.Common.Logging.Targets
{
    public class JsonLogTarget : ILogTarget
    {
        private readonly Stream _stream;
        private readonly bool _leaveOpen;
        private readonly string _name;

        string ILogTarget.Name { get => _name; }

        public JsonLogTarget(Stream stream, string name)
        {
            _stream = stream;
            _name = name;
        }

        public JsonLogTarget(Stream stream, bool leaveOpen)
        {
            _stream = stream;
            _leaveOpen = leaveOpen;
        }

        public void Log(object sender, LogEventArgs e)
        {
            var logEventArgsJson = LogEventArgsJson.FromLogEventArgs(e);
            JsonHelper.SerializeToStream(_stream, logEventArgsJson, LogEventJsonSerializerContext.Default.LogEventArgsJson);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
            if (!_leaveOpen)
            {
                _stream.Dispose();
            }
        }
    }
}