aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Logging/Targets/FileLogTarget.cs
blob: 85dc82497af00297bead08d8dd76befe60d0ced9 (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
using System.IO;
using System.Text;

namespace Ryujinx.Common.Logging
{
    public class FileLogTarget : ILogTarget
    {
        private static readonly ObjectPool<StringBuilder> _stringBuilderPool = SharedPools.Default<StringBuilder>();

        private readonly StreamWriter  _logWriter;
        private readonly ILogFormatter _formatter;

        public FileLogTarget(string path)
            : this(path, FileShare.Read, FileMode.Append)
        { }

        public FileLogTarget(string path, FileShare fileShare, FileMode fileMode)
        {
            _logWriter = new StreamWriter(File.Open(path, fileMode, FileAccess.Write, fileShare));
            _formatter = new DefaultLogFormatter();
        }

        public void Log(object sender, LogEventArgs args)
        {
            _logWriter.WriteLine(_formatter.Format(args));
            _logWriter.Flush();
        }

        public void Dispose()
        {
            _logWriter.WriteLine("---- End of Log ----");
            _logWriter.Flush();
            _logWriter.Dispose();
        }
    }
}