diff options
Diffstat (limited to 'src/StardewModdingAPI/Framework')
-rw-r--r-- | src/StardewModdingAPI/Framework/DeprecationLevel.cs | 15 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/DeprecationManager.cs | 41 | ||||
-rw-r--r-- | src/StardewModdingAPI/Framework/LogWriter.cs | 9 |
3 files changed, 55 insertions, 10 deletions
diff --git a/src/StardewModdingAPI/Framework/DeprecationLevel.cs b/src/StardewModdingAPI/Framework/DeprecationLevel.cs new file mode 100644 index 00000000..c0044053 --- /dev/null +++ b/src/StardewModdingAPI/Framework/DeprecationLevel.cs @@ -0,0 +1,15 @@ +namespace StardewModdingAPI.Framework +{ + /// <summary>Indicates how deprecated something is.</summary> + internal enum DeprecationLevel + { + /// <summary>It's deprecated but won't be removed soon. Mod authors have some time to update their mods. Deprecation warnings should be logged, but not written to the console.</summary> + Notice, + + /// <summary>Mods should no longer be using it. Deprecation messages should be debug entries in the console.</summary> + Info, + + /// <summary>The code will be removed soon. Deprecation messages should be warnings in the console.</summary> + PendingRemoval + } +}
\ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/DeprecationManager.cs b/src/StardewModdingAPI/Framework/DeprecationManager.cs index 2d4ff614..a3d1ea41 100644 --- a/src/StardewModdingAPI/Framework/DeprecationManager.cs +++ b/src/StardewModdingAPI/Framework/DeprecationManager.cs @@ -32,24 +32,51 @@ namespace StardewModdingAPI.Framework /// <summary>Log a deprecation warning.</summary> /// <param name="nounPhrase">A noun phrase describing what is deprecated.</param> /// <param name="version">The SMAPI version which deprecated it.</param> - public void Warn(string nounPhrase, string version) + /// <param name="severity">How deprecated the code is.</param> + public void Warn(string nounPhrase, string version, DeprecationLevel severity) { - this.Warn(this.GetSourceNameFromStack(), nounPhrase, version); + this.Warn(this.GetSourceNameFromStack(), nounPhrase, version, severity); } /// <summary>Log a deprecation warning.</summary> /// <param name="source">The friendly mod name which used the deprecated code.</param> /// <param name="nounPhrase">A noun phrase describing what is deprecated.</param> /// <param name="version">The SMAPI version which deprecated it.</param> - public void Warn(string source, string nounPhrase, string version) + /// <param name="severity">How deprecated the code is.</param> + public void Warn(string source, string nounPhrase, string version, DeprecationLevel severity) { + // ignore if already warned if (source != null && !this.MarkWarned(source, nounPhrase, version)) return; - Log.Debug(source != null - ? $"NOTE: {source} used {nounPhrase}, which is deprecated since SMAPI {version}. It will work fine for now, but may be removed in a future version of SMAPI." - : $"NOTE: an unknown mod used {nounPhrase}, which is deprecated since SMAPI {version}. It will work fine for now, but may be removed in a future version of SMAPI.\n{Environment.StackTrace}" - ); + // build message + string message = source != null + ? $"{source} used {nounPhrase}, which is deprecated since SMAPI {version}." + : $"An unknown mod used {nounPhrase}, which is deprecated since SMAPI {version}."; + message += severity != DeprecationLevel.PendingRemoval + ? " It will work fine for now, but may be removed in a future version of SMAPI." + : " It will be removed soon, so the mod will break if it's not updated."; + if (source == null) + message += $"{Environment.NewLine}{Environment.StackTrace}"; + + // log message + switch (severity) + { + case DeprecationLevel.Notice: + Log.LogToFile(message); + break; + + case DeprecationLevel.Info: + Log.Debug(message); + break; + + case DeprecationLevel.PendingRemoval: + Log.Warning(message); + break; + + default: + throw new NotImplementedException($"Unknown deprecation level '{severity}'"); + } } /// <summary>Mark a deprecation warning as already logged.</summary> diff --git a/src/StardewModdingAPI/Framework/LogWriter.cs b/src/StardewModdingAPI/Framework/LogWriter.cs index 64769d2e..c21d53f4 100644 --- a/src/StardewModdingAPI/Framework/LogWriter.cs +++ b/src/StardewModdingAPI/Framework/LogWriter.cs @@ -74,9 +74,12 @@ namespace StardewModdingAPI.Framework { string message = $"[{entry.LogTime}] {entry.Message}"; - Console.ForegroundColor = entry.Colour; - Console.WriteLine(message); - Console.ForegroundColor = ConsoleColor.Gray; + if (entry.PrintConsole) + { + Console.ForegroundColor = entry.Colour; + Console.WriteLine(message); + Console.ForegroundColor = ConsoleColor.Gray; + } this.FileStream.WriteLine(message); } |