summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/LogWriter.cs
blob: c8c9b9e14be066c296d5b857695c853ae36f35d0 (plain)
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
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;

namespace StardewModdingAPI
{
    /// <summary>A log writer which queues messages for output, and periodically flushes them to the console and log file.</summary>
    /// <remarks>Only one instance should be created.</remarks>
    [Obsolete("This class is internal and should not be referenced outside SMAPI. It will no longer be exposed in a future version.")]
    public class LogWriter
    {
        /*********
        ** Properties
        *********/
        /// <summary>The queued messages to flush.</summary>
        private readonly ConcurrentQueue<LogInfo> Queue;

        /// <summary>The underlying file stream.</summary>
        private readonly StreamWriter FileStream;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="path">The log path to write.</param>
        public LogWriter(string path)
        {
            // create log directory (required for stream writer)
            Directory.CreateDirectory(Path.GetDirectoryName(path));

            // initialise
            this.Queue = new ConcurrentQueue<LogInfo>();
            this.FileStream = new StreamWriter(Constants.LogPath, false);
        }

        /// <summary>Queue a message for output.</summary>
        /// <param name="message">The message to log.</param>
        public void WriteToLog(string message)
        {
            lock (this.Queue)
            {
                var logEntry = new LogInfo(message);
                this.Queue.Enqueue(logEntry);

                if (this.Queue.Any())
                    this.FlushLog();
            }
        }

        /// <summary>Queue a message for output.</summary>
        /// <param name="message">The message to log.</param>
        public void WriteToLog(LogInfo message)
        {
            lock (this.Queue)
            {
                this.Queue.Enqueue(message);
                if (this.Queue.Any())
                    this.FlushLog();
            }
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Flush the underlying queue to the console and file.</summary>
        private void FlushLog()
        {
            lock (this.FileStream)
            {
                LogInfo entry;
                while (this.Queue.TryDequeue(out entry))
                {
                    string message = $"[{entry.LogTime}] {entry.Message}";

                    if (entry.PrintConsole)
                    {
                        Console.ForegroundColor = entry.Colour;
                        Console.WriteLine(message);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }

                    this.FileStream.WriteLine(message);
                }
                this.FileStream.Flush();
            }
        }
    }
}