From a50e78efd827bfeab32dac37749c001af0c5a2d3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 22 Mar 2020 17:40:31 -0400 Subject: add monitor.LogOnce method --- src/SMAPI/Framework/Monitor.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/SMAPI/Framework/Monitor.cs') diff --git a/src/SMAPI/Framework/Monitor.cs b/src/SMAPI/Framework/Monitor.cs index 06cf1b46..f630c7fe 100644 --- a/src/SMAPI/Framework/Monitor.cs +++ b/src/SMAPI/Framework/Monitor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Framework.Logging; using StardewModdingAPI.Internal.ConsoleWriting; @@ -26,6 +27,9 @@ namespace StardewModdingAPI.Framework /// The maximum length of the values. private static readonly int MaxLevelLength = (from level in Enum.GetValues(typeof(LogLevel)).Cast() select level.ToString().Length).Max(); + /// A cache of messages that should only be logged once. + private readonly HashSet LogOnceCache = new HashSet(); + /********* ** Accessors @@ -74,6 +78,15 @@ namespace StardewModdingAPI.Framework this.LogImpl(this.Source, message, (ConsoleLogLevel)level); } + /// Log a message for the player or developer, but only if it hasn't already been logged since the last game launch. + /// The message to log. + /// The log severity level. + public void LogOnce(string message, LogLevel level = LogLevel.Trace) + { + if (this.LogOnceCache.Add($"{message}|{level}")) + this.LogImpl(this.Source, message, (ConsoleLogLevel)level); + } + /// Log a message that only appears when is enabled. /// The message to log. public void VerboseLog(string message) -- cgit