summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/LogWriter.cs
blob: 9c2ef515fad271b3f91722857ca20a1e6dba7b5f (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
using System;
using StardewModdingAPI.Framework;

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>Manages reading and writing to the log file.</summary>
        private readonly LogFileManager LogFile;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="logFile">Manages reading and writing to the log file.</param>
        internal LogWriter(LogFileManager logFile)
        {
            this.WarnDeprecated();
            this.LogFile = logFile;
        }

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

        /// <summary>Queue a message for output.</summary>
        /// <param name="message">The message to log.</param>
        public void WriteToLog(LogInfo message)
        {
            this.WarnDeprecated();
            string output = $"[{message.LogTime}] {message.Message}";
            if (message.PrintConsole)
            {
                if (Monitor.ConsoleSupportsColor)
                {
                    Console.ForegroundColor = message.Colour;
                    Console.WriteLine(message);
                    Console.ResetColor();
                }
                else
                    Console.WriteLine(message);
            }
            this.LogFile.WriteLine(output);
        }

        /*********
        ** Private methods
        *********/
        /// <summary>Raise a deprecation warning.</summary>
        private void WarnDeprecated()
        {
            Program.DeprecationManager.Warn($"the {nameof(LogWriter)} class", "1.0", DeprecationLevel.Info);
        }
    }
}