summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Logging/InterceptingTextWriter.cs
blob: bad69a2ad51bf3831d61796ae5d7137b5f62a10d (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
using System;
using System.IO;
using System.Text;

namespace StardewModdingAPI.Framework.Logging
{
    /// <summary>A text writer which allows intercepting output.</summary>
    internal class InterceptingTextWriter : TextWriter
    {
        /*********
        ** Accessors
        *********/
        /// <summary>Prefixing a message with this character indicates that the console interceptor should write the string without intercepting it. (The character itself is not written.)</summary>
        public const char IgnoreChar = '\u200B';

        /// <summary>The underlying console output.</summary>
        public TextWriter Out { get; }

        /// <inheritdoc />
        public override Encoding Encoding => this.Out.Encoding;

        /// <summary>The event raised when a message is written to the console directly.</summary>
        public event Action<string> OnMessageIntercepted;

        /// <summary>Whether the text writer should ignore the next input if it's a newline.</summary>
        /// <remarks>This is used when log output is suppressed from the console, since <c>Console.WriteLine</c> writes the trailing newline as a separate call.</remarks>
        public bool IgnoreNextIfNewline { get; set; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="output">The underlying output writer.</param>
        public InterceptingTextWriter(TextWriter output)
        {
            this.Out = output;
        }

        /// <inheritdoc />
        public override void Write(char[] buffer, int index, int count)
        {
            // track newline skip
            bool ignoreIfNewline = this.IgnoreNextIfNewline;
            this.IgnoreNextIfNewline = false;

            // get first character if valid
            if (count == 0 || index < 0 || index >= buffer.Length)
            {
                this.Out.Write(buffer, index, count);
                return;
            }
            char firstChar = buffer[index];

            // handle output
            if (firstChar == InterceptingTextWriter.IgnoreChar)
                this.Out.Write(buffer, index + 1, count - 1);
            else if (char.IsControl(firstChar) && firstChar is not ('\r' or '\n'))
                this.Out.Write(buffer, index, count);
            else if (this.IsEmptyOrNewline(buffer))
            {
                if (!ignoreIfNewline)
                    this.Out.Write(buffer, index, count);
            }
            else
                this.OnMessageIntercepted?.Invoke(new string(buffer, index, count));
        }

        /// <inheritdoc />
        public override void Write(char ch)
        {
            this.Out.Write(ch);
        }

        /// <inheritdoc />
        protected override void Dispose(bool disposing)
        {
            this.OnMessageIntercepted = null;
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Get whether a buffer represents a line break.</summary>
        /// <param name="buffer">The buffer to check.</param>
        private bool IsEmptyOrNewline(char[] buffer)
        {
            foreach (char ch in buffer)
            {
                if (ch != '\n' && ch != '\r')
                    return false;
            }

            return true;
        }
    }
}