From 7c90385d8df7bbf9469fc468480b26ebb134abd8 Mon Sep 17 00:00:00 2001
From: atravita-mods <94934860+atravita-mods@users.noreply.github.com>
Date: Mon, 15 Aug 2022 19:13:24 -0400
Subject: [PATCH] Pre-calculate the strings for log levels.
---
src/SMAPI/Framework/Monitor.cs | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/SMAPI/Framework/Monitor.cs b/src/SMAPI/Framework/Monitor.cs
index 6b53daff..8ba175e6 100644
--- a/src/SMAPI/Framework/Monitor.cs
+++ b/src/SMAPI/Framework/Monitor.cs
@@ -25,7 +25,9 @@ namespace StardewModdingAPI.Framework
private readonly LogFileManager LogFile;
/// The maximum length of the values.
- private static readonly int MaxLevelLength = (from level in Enum.GetValues(typeof(LogLevel)).Cast() select level.ToString().Length).Max();
+ private static readonly int MaxLevelLength = (from level in Enum.GetValues() select level.ToString().Length).Max();
+
+ private static readonly Dictionary LogStrings = Enum.GetValues().ToDictionary(k => k, v => v.ToString().ToUpper().PadRight(MaxLevelLength));
/// A cache of messages that should only be logged once.
private readonly HashSet LogOnceCache = new();
@@ -147,7 +149,7 @@ namespace StardewModdingAPI.Framework
/// The log level.
private string GenerateMessagePrefix(string source, ConsoleLogLevel level)
{
- string levelStr = level.ToString().ToUpper().PadRight(Monitor.MaxLevelLength);
+ string levelStr = LogStrings[level];
int? playerIndex = this.GetScreenIdForLog();
return $"[{DateTime.Now:HH:mm:ss} {levelStr}{(playerIndex != null ? $" screen_{playerIndex}" : "")} {source}]";