Skip to content

Commit

Permalink
优化日志解析器
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Apr 30, 2024
1 parent 8341fed commit 84db42a
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions MinecraftLaunch/Components/Resolver/GameLogResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ namespace MinecraftLaunch.Components.Resolver;
/// <summary>
/// 游戏日志解析器
/// </summary>
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,
Expand All @@ -39,16 +57,16 @@ public string GetLog(string log) {
/// <returns></returns>
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;
}

/// <summary>
Expand All @@ -57,12 +75,11 @@ public string GetLogType(string log) {
/// <param name="log"></param>
/// <returns></returns>
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)!;
}

Expand All @@ -72,8 +89,7 @@ public string GetSource(string log) {
/// <param name="log"></param>
/// <returns></returns>
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;
}

/// <summary>
Expand All @@ -82,8 +98,6 @@ public string GetLogTime(string log) {
/// <param name="log"></param>
/// <returns></returns>
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;
}
}

0 comments on commit 84db42a

Please sign in to comment.