summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/LogWriter.cs
blob: 2cabb8d5d8e446cfc5ed0a43bd8a2f393623ff3a (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;

namespace StardewModdingAPI.Framework
{
    /// <summary>A log writer which queues messages for output, and periodically flushes them to the console and log file.</summary>
    internal class LogWriter
    {
        /*********
        ** Properties
        *********/
        /// <summary>The singleton instance.</summary>
        private static LogWriter _instance;

        /// <summary>The queued messages to flush.</summary>
        private static ConcurrentQueue<LogInfo> Queue;

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


        /*********
        ** Accessors
        *********/
        /// <summary>The singleton instance.</summary>
        internal static LogWriter Instance
        {
            get
            {
                if (LogWriter._instance == null)
                {
                    LogWriter._instance = new LogWriter();
                    // Field cannot be used by anything else regardless, do not surround with lock { }
                    // ReSharper disable once InconsistentlySynchronizedField
                    LogWriter.Queue = new ConcurrentQueue<LogInfo>();
                    Console.WriteLine(Constants.LogPath);

                    // If the ErrorLogs dir doesn't exist StreamWriter will throw an exception.
                    if (!Directory.Exists(Constants.LogDir))
                    {
                        Directory.CreateDirectory(Constants.LogDir);
                    }

                    LogWriter.FileStream = new StreamWriter(Constants.LogPath, false);
                    Console.WriteLine("Created log instance");
                }
                return LogWriter._instance;
            }
        }


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

                if (LogWriter.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 (LogWriter.Queue)
            {
                LogWriter.Queue.Enqueue(message);
                if (LogWriter.Queue.Any())
                    this.FlushLog();
            }
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Construct an instance.</summary>
        private LogWriter() { }

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

                    Console.ForegroundColor = entry.Colour;
                    Console.WriteLine(m);
                    Console.ForegroundColor = ConsoleColor.Gray;

                    LogWriter.FileStream.WriteLine(m);
                }
                LogWriter.FileStream.Flush();
            }
        }
    }
}