From f9aa76e41f84ff9ec70dc9bf5178f50617e45424 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 6 Nov 2016 10:30:25 -0500 Subject: use more nuanced deprecation warnings (#165) --- .../Framework/DeprecationLevel.cs | 15 ++++++++ .../Framework/DeprecationManager.cs | 41 ++++++++++++++++++---- src/StardewModdingAPI/Framework/LogWriter.cs | 9 +++-- 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/DeprecationLevel.cs (limited to 'src/StardewModdingAPI/Framework') 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 +{ + /// Indicates how deprecated something is. + internal enum DeprecationLevel + { + /// 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. + Notice, + + /// Mods should no longer be using it. Deprecation messages should be debug entries in the console. + Info, + + /// The code will be removed soon. Deprecation messages should be warnings in the console. + 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 /// Log a deprecation warning. /// A noun phrase describing what is deprecated. /// The SMAPI version which deprecated it. - public void Warn(string nounPhrase, string version) + /// How deprecated the code is. + public void Warn(string nounPhrase, string version, DeprecationLevel severity) { - this.Warn(this.GetSourceNameFromStack(), nounPhrase, version); + this.Warn(this.GetSourceNameFromStack(), nounPhrase, version, severity); } /// Log a deprecation warning. /// The friendly mod name which used the deprecated code. /// A noun phrase describing what is deprecated. /// The SMAPI version which deprecated it. - public void Warn(string source, string nounPhrase, string version) + /// How deprecated the code is. + 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}'"); + } } /// Mark a deprecation warning as already logged. 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); } -- cgit