optimise console interception for the way Stardew Valley logs messages

This commit is contained in:
Jesse Plamondon-Willard 2017-04-26 16:04:20 -04:00
parent e7606884ad
commit 4ef957c191
4 changed files with 27 additions and 37 deletions

View File

@ -10,6 +10,12 @@ For mod developers:
images).
-->
## 1.11
See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11).
For players:
* Optimised console logging.
## 1.10
See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...1.10).

View File

@ -18,8 +18,8 @@ namespace StardewModdingAPI.Framework.Logging
/// <summary>Whether the current console supports color formatting.</summary>
public bool SupportsColor { get; }
/// <summary>The event raised when something writes a line to the console directly.</summary>
public event Action<string> OnLineIntercepted;
/// <summary>The event raised when a message is written to the console directly.</summary>
public event Action<string> OnMessageIntercepted;
/*********
@ -30,7 +30,7 @@ namespace StardewModdingAPI.Framework.Logging
{
// redirect output through interceptor
this.Output = new InterceptingTextWriter(Console.Out);
this.Output.OnLineIntercepted += line => this.OnLineIntercepted?.Invoke(line);
this.Output.OnMessageIntercepted += line => this.OnMessageIntercepted?.Invoke(line);
Console.SetOut(this.Output);
// test color support

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@ -8,13 +7,6 @@ namespace StardewModdingAPI.Framework.Logging
/// <summary>A text writer which allows intercepting output.</summary>
internal class InterceptingTextWriter : TextWriter
{
/*********
** Properties
*********/
/// <summary>The current line being intercepted.</summary>
private readonly List<char> Line = new List<char>();
/*********
** Accessors
*********/
@ -27,8 +19,8 @@ namespace StardewModdingAPI.Framework.Logging
/// <summary>Whether to intercept console output.</summary>
public bool ShouldIntercept { get; set; }
/// <summary>The event raised when a line of text is intercepted.</summary>
public event Action<string> OnLineIntercepted;
/// <summary>The event raised when a message is written to the console directly.</summary>
public event Action<string> OnMessageIntercepted;
/*********
@ -41,39 +33,31 @@ namespace StardewModdingAPI.Framework.Logging
this.Out = output;
}
/// <summary>Writes a subarray of characters to the text string or stream.</summary>
/// <param name="buffer">The character array to write data from.</param>
/// <param name="index">The character position in the buffer at which to start retrieving data.</param>
/// <param name="count">The number of characters to write.</param>
public override void Write(char[] buffer, int index, int count)
{
if (this.ShouldIntercept)
this.OnMessageIntercepted?.Invoke(new string(buffer, index, count).TrimEnd('\r', '\n'));
else
this.Out.Write(buffer, index, count);
}
/// <summary>Writes a character to the text string or stream.</summary>
/// <param name="ch">The character to write to the text stream.</param>
/// <remarks>Console log messages from the game should be caught by <see cref="Write(char[],int,int)"/>. This method passes through anything that bypasses that method for some reason, since it's better to show it to users than hide it from everyone.</remarks>
public override void Write(char ch)
{
// intercept
if (this.ShouldIntercept)
{
switch (ch)
{
case '\r':
return;
case '\n':
this.OnLineIntercepted?.Invoke(new string(this.Line.ToArray()));
this.Line.Clear();
break;
default:
this.Line.Add(ch);
break;
}
}
// pass through
else
this.Out.Write(ch);
this.Out.Write(ch);
}
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.TextWriter" /> and optionally releases the managed resources.</summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
this.OnLineIntercepted = null;
this.OnMessageIntercepted = null;
}
}
}

View File

@ -249,7 +249,7 @@ namespace StardewModdingAPI
Monitor monitor = this.GetSecondaryMonitor("Console.Out");
monitor.WriteToFile = false; // not useful for troubleshooting mods per discussion
if (monitor.WriteToConsole)
this.ConsoleManager.OnLineIntercepted += line => monitor.Log(line, LogLevel.Trace);
this.ConsoleManager.OnMessageIntercepted += line => monitor.Log(line, LogLevel.Trace);
}
// add warning headers