diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-18 12:43:31 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-09-18 12:43:31 -0400 |
commit | 6ac5ca090da088e96b0c6508866cebe7eedc439d (patch) | |
tree | 949166e5875c1969491fecb9d2c4d34afab936f8 | |
parent | 8a117744608dfff970855defbffb1f40466ab755 (diff) | |
download | SMAPI-6ac5ca090da088e96b0c6508866cebe7eedc439d.tar.gz SMAPI-6ac5ca090da088e96b0c6508866cebe7eedc439d.tar.bz2 SMAPI-6ac5ca090da088e96b0c6508866cebe7eedc439d.zip |
simplify exception logs
-rw-r--r-- | docs/release-notes.md | 3 | ||||
-rw-r--r-- | src/SMAPI.Internal/ExceptionHelper.cs (renamed from src/SMAPI.Internal/ExceptionExtensions.cs) | 36 | ||||
-rw-r--r-- | src/SMAPI.Internal/SMAPI.Internal.projitems | 2 | ||||
-rw-r--r-- | src/SMAPI/Framework/Logging/LogManager.cs | 4 |
4 files changed, 39 insertions, 6 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md index f330c677..f619623f 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -4,7 +4,8 @@ ## Upcoming release * For players: * Added more progress updates in the log during startup. - * Simplified common asset load error message. + * Simplified asset load error message. + * Simplified exception logs. * Fixed crash loading mods with corrupted translation files. * For mod authors: diff --git a/src/SMAPI.Internal/ExceptionExtensions.cs b/src/SMAPI.Internal/ExceptionHelper.cs index d8189048..4bc55f95 100644 --- a/src/SMAPI.Internal/ExceptionExtensions.cs +++ b/src/SMAPI.Internal/ExceptionHelper.cs @@ -1,10 +1,11 @@ using System; using System.Reflection; +using System.Text.RegularExpressions; namespace StardewModdingAPI.Internal { /// <summary>Provides extension methods for handling exceptions.</summary> - internal static class ExceptionExtensions + internal static class ExceptionHelper { /********* ** Public methods @@ -15,20 +16,26 @@ namespace StardewModdingAPI.Internal { try { + string message; switch (exception) { case TypeLoadException ex: - return $"Failed loading type '{ex.TypeName}': {exception}"; + message = $"Failed loading type '{ex.TypeName}': {exception}"; + break; case ReflectionTypeLoadException ex: string summary = ex.ToString(); foreach (Exception childEx in ex.LoaderExceptions ?? new Exception[0]) summary += $"\n\n{childEx?.GetLogSummary()}"; - return summary; + message = summary; + break; default: - return exception?.ToString() ?? $"<null exception>\n{Environment.StackTrace}"; + message = exception?.ToString() ?? $"<null exception>\n{Environment.StackTrace}"; + break; } + + return ExceptionHelper.SimplifyExtensionMessage(message); } catch (Exception ex) { @@ -44,5 +51,26 @@ namespace StardewModdingAPI.Internal exception = exception.InnerException; return exception; } + + /// <summary>Simplify common patterns in exception log messages that don't convey useful info.</summary> + /// <param name="message">The log message to simplify.</param> + public static string SimplifyExtensionMessage(string message) + { + // remove namespace for core exception types + message = Regex.Replace( + message, + @"(?:StardewModdingAPI\.Framework\.Exceptions|Microsoft\.Xna\.Framework|System|System\.IO)\.([a-zA-Z]+Exception):", + "$1:" + ); + + // remove unneeded root build paths for SMAPI and Stardew Valley + message = message + .Replace(@"C:\source\_Stardew\SMAPI\src\", "") + .Replace(@"C:\GitlabRunner\builds\Gq5qA5P4\0\ConcernedApe\", ""); + + // remove placeholder info in Linux/macOS stack traces + return message + .Replace(@"<filename unknown>:0", ""); + } } } diff --git a/src/SMAPI.Internal/SMAPI.Internal.projitems b/src/SMAPI.Internal/SMAPI.Internal.projitems index 0ee94a5b..41d356c0 100644 --- a/src/SMAPI.Internal/SMAPI.Internal.projitems +++ b/src/SMAPI.Internal/SMAPI.Internal.projitems @@ -14,6 +14,6 @@ <Compile Include="$(MSBuildThisFileDirectory)ConsoleWriting\ConsoleLogLevel.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ConsoleWriting\IConsoleWriter.cs" /> <Compile Include="$(MSBuildThisFileDirectory)ConsoleWriting\MonitorColorScheme.cs" /> - <Compile Include="$(MSBuildThisFileDirectory)ExceptionExtensions.cs" /> + <Compile Include="$(MSBuildThisFileDirectory)ExceptionHelper.cs" /> </ItemGroup> </Project>
\ No newline at end of file diff --git a/src/SMAPI/Framework/Logging/LogManager.cs b/src/SMAPI/Framework/Logging/LogManager.cs index 6fe44d98..f2876146 100644 --- a/src/SMAPI/Framework/Logging/LogManager.cs +++ b/src/SMAPI/Framework/Logging/LogManager.cs @@ -406,6 +406,10 @@ namespace StardewModdingAPI.Framework.Logging } } + // simplify exception messages + if (level == LogLevel.Error) + message = ExceptionHelper.SimplifyExtensionMessage(message); + // forward to monitor gameMonitor.Log(message, level); } |