From 84db42a1450cffba0a4f1efffd6c2024dae403d4 Mon Sep 17 00:00:00 2001 From: YangSpring114 Date: Tue, 30 Apr 2024 13:57:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Resolver/GameLogResolver.cs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/MinecraftLaunch/Components/Resolver/GameLogResolver.cs b/MinecraftLaunch/Components/Resolver/GameLogResolver.cs index 539fe2d..245e366 100644 --- a/MinecraftLaunch/Components/Resolver/GameLogResolver.cs +++ b/MinecraftLaunch/Components/Resolver/GameLogResolver.cs @@ -7,12 +7,30 @@ namespace MinecraftLaunch.Components.Resolver; /// /// 游戏日志解析器 /// -public sealed class GameLogResolver { +public sealed partial class GameLogResolver { + [GeneratedRegex("(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d")] + private partial Regex TimeRegex(); + + [GeneratedRegex("(at .*)")] + private partial Regex StackTraceRegex(); + + [GeneratedRegex("(?m)^.*?Exception.*")] + private partial Regex ExceptionRegex(); + + [GeneratedRegex("FATAL|ERROR|WARN|INFO|DEBUG")] + private partial Regex LogTypeRegex(); + + [GeneratedRegex("[\\w\\W\\s]{2,}/(FATAL|ERROR|WARN|INFO|DEBUG)")] + private partial Regex SourceRegex(); + + [GeneratedRegex("\\[(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\] \\[[\\w\\W\\s]{2,}/(FATAL|ERROR|WARN|INFO|DEBUG)\\]")] + private partial Regex TotalPrefixRegex(); + public GameLogEntry Resolve(string log) { return new GameLogEntry { Log = GetLog(log), Source = GetSource(log), - Time = Regex.IsMatch(log, "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d", RegexOptions.Compiled) ? GetLogTime(log) : DateTime.Now.ToString(), + Time = TimeRegex().IsMatch(log) ? GetLogTime(log) : DateTime.Now.ToString(), LogType = GetLogType(log) switch { "FATAL" => LogType.Fatal, "ERROR" => LogType.Error, @@ -39,16 +57,16 @@ public string GetLog(string log) { /// public string GetLogType(string log) { //是否是堆栈信息 - if (Regex.IsMatch(log, "(at .*)", RegexOptions.Compiled)) { + if (StackTraceRegex().IsMatch(log)) { return "STACK"; } //是否是异常信息 - if (Regex.IsMatch(log, "(?m)^.*?Exception.*", RegexOptions.Compiled)) { + if (ExceptionRegex().IsMatch(log)) { return "Exception"; } - return Regex.Match(log, "FATAL|ERROR|WARN|INFO|DEBUG", RegexOptions.Compiled).Value; + return LogTypeRegex().Match(log).Value; } /// @@ -57,12 +75,11 @@ public string GetLogType(string log) { /// /// public string GetSource(string log) { - var content = Regex.Match(log, $"[\\w\\W\\s]{{2,}}/(FATAL|ERROR|WARN|INFO|DEBUG)", RegexOptions.Compiled) + var content = SourceRegex().Match(log) .Value.Split('/') .FirstOrDefault(); - return content?.Replace($"{Regex.Match(log, - $"\\[(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\]").Value} [", + return content?.Replace($"{TimeRegex().Match(log).Value} [", string.Empty)!; } @@ -72,8 +89,7 @@ public string GetSource(string log) { /// /// public string GetLogTime(string log) { - return Regex.Match(log, "(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d", - RegexOptions.Compiled).Value; + return TimeRegex().Match(log).Value; } /// @@ -82,8 +98,6 @@ public string GetLogTime(string log) { /// /// public string GetTotalPrefix(string log) { - return Regex.Match(log, - $"\\[(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d\\] \\[[\\w\\W\\s]{{2,}}/(FATAL|ERROR|WARN|INFO|DEBUG)\\]", - RegexOptions.Compiled).Value; + return TotalPrefixRegex().Match(log).Value; } } \ No newline at end of file