Pre-calculate the strings for log levels.

This commit is contained in:
atravita-mods 2022-08-15 19:13:24 -04:00 committed by Jesse Plamondon-Willard
parent e7d29a2f7d
commit 7c90385d8d
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
1 changed files with 4 additions and 2 deletions

View File

@ -25,7 +25,9 @@ namespace StardewModdingAPI.Framework
private readonly LogFileManager LogFile;
/// <summary>The maximum length of the <see cref="LogLevel"/> values.</summary>
private static readonly int MaxLevelLength = (from level in Enum.GetValues(typeof(LogLevel)).Cast<LogLevel>() select level.ToString().Length).Max();
private static readonly int MaxLevelLength = (from level in Enum.GetValues<LogLevel>() select level.ToString().Length).Max();
private static readonly Dictionary<ConsoleLogLevel, string> LogStrings = Enum.GetValues<ConsoleLogLevel>().ToDictionary(k => k, v => v.ToString().ToUpper().PadRight(MaxLevelLength));
/// <summary>A cache of messages that should only be logged once.</summary>
private readonly HashSet<string> LogOnceCache = new();
@ -147,7 +149,7 @@ namespace StardewModdingAPI.Framework
/// <param name="level">The log level.</param>
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}]";