|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace FSMPLogVisualizer.Core |
| 8 | +{ |
| 9 | + public class LogParser |
| 10 | + { |
| 11 | + // "smp cost in main loop (msecs): {:.2f}, cost outside main loop: {:.2f}, percentage outside vs total: {:.2f}" |
| 12 | + private static readonly Regex smpCostRegex = new Regex( |
| 13 | + @"smp cost in main loop \(msecs\):\s*([\d\.]+),\s*cost outside main loop:\s*([\d\.]+),\s*percentage outside vs total:\s*([\d\.]+)", |
| 14 | + RegexOptions.Compiled | RegexOptions.IgnoreCase); |
| 15 | + |
| 16 | + // "msecs/activeSkeleton {:.2f} activeSkeletons/maxActive/total {}/{}/{} processTimeInMainLoop/targetTime {:.2f}/{:.2f}" |
| 17 | + private static readonly Regex activeSkeletonsRegex = new Regex( |
| 18 | + @"msecs/activeSkeleton\s*([\d\.]+)\s*activeSkeletons/maxActive/total\s*(\d+)/(\d+)/(\d+)\s*processTimeInMainLoop/targetTime\s*([\d\.]+)/([\d\.]+)", |
| 19 | + RegexOptions.Compiled | RegexOptions.IgnoreCase); |
| 20 | + |
| 21 | + // Extracts timestamp "[16:45:25.906]" |
| 22 | + private static readonly Regex timestampRegex = new Regex(@"^\[([\d:\.]+)\]", RegexOptions.Compiled); |
| 23 | + |
| 24 | + public async Task<(LogRunSession session, List<LogDataPoint> dataPoints)> ParseLogFileAsync(string filePath, string existingLastTimestamp = null) |
| 25 | + { |
| 26 | + var dataPoints = new List<LogDataPoint>(); |
| 27 | + |
| 28 | + var session = new LogRunSession |
| 29 | + { |
| 30 | + FileName = Path.GetFileName(filePath), |
| 31 | + ImportedAt = DateTime.Now |
| 32 | + }; |
| 33 | + |
| 34 | + bool skipUntilNew = !string.IsNullOrEmpty(existingLastTimestamp); |
| 35 | + |
| 36 | + using var reader = new StreamReader(filePath); |
| 37 | + string? line; |
| 38 | + |
| 39 | + // Read version from first line if possible |
| 40 | + if ((line = await reader.ReadLineAsync()) != null) |
| 41 | + { |
| 42 | + session.Version = ExtractVersion(line); |
| 43 | + session.SessionKey = $"{session.FileName}_{session.Version}"; // Basic key |
| 44 | + } |
| 45 | + |
| 46 | + // Stateful tracking |
| 47 | + int? currentActiveSkeletons = null; |
| 48 | + int? currentMaxActive = null; |
| 49 | + int? currentTotal = null; |
| 50 | + double? currentMsecsPerAct = null; |
| 51 | + double? currentProcTime = null; |
| 52 | + double? currentTargetTime = null; |
| 53 | + |
| 54 | + while ((line = await reader.ReadLineAsync()) != null) |
| 55 | + { |
| 56 | + var tsMatch = timestampRegex.Match(line); |
| 57 | + string timestamp = tsMatch.Success ? tsMatch.Groups[1].Value : string.Empty; |
| 58 | + |
| 59 | + if (skipUntilNew && tsMatch.Success) |
| 60 | + { |
| 61 | + if (timestamp == existingLastTimestamp) |
| 62 | + { |
| 63 | + skipUntilNew = false; // We found the last known timestamp, resume parsing after this |
| 64 | + } |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if (skipUntilNew) continue; |
| 69 | + |
| 70 | + var skelMatch = activeSkeletonsRegex.Match(line); |
| 71 | + if (skelMatch.Success) |
| 72 | + { |
| 73 | + currentMsecsPerAct = double.Parse(skelMatch.Groups[1].Value); |
| 74 | + currentActiveSkeletons = int.Parse(skelMatch.Groups[2].Value); |
| 75 | + currentMaxActive = int.Parse(skelMatch.Groups[3].Value); |
| 76 | + currentTotal = int.Parse(skelMatch.Groups[4].Value); |
| 77 | + currentProcTime = double.Parse(skelMatch.Groups[5].Value); |
| 78 | + currentTargetTime = double.Parse(skelMatch.Groups[6].Value); |
| 79 | + |
| 80 | + dataPoints.Add(new LogDataPoint |
| 81 | + { |
| 82 | + Timestamp = timestamp, |
| 83 | + MsecsPerActiveSkeleton = currentMsecsPerAct, |
| 84 | + ActiveSkeletons = currentActiveSkeletons, |
| 85 | + MaxActiveSkeletons = currentMaxActive, |
| 86 | + TotalSkeletons = currentTotal, |
| 87 | + ProcessTimeInMainLoop = currentProcTime, |
| 88 | + TargetTime = currentTargetTime |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + var costMatch = smpCostRegex.Match(line); |
| 93 | + if (costMatch.Success) |
| 94 | + { |
| 95 | + // Cost match means we emit a datapoint with the cost, and optionally the latest skeleton data if we have it |
| 96 | + dataPoints.Add(new LogDataPoint |
| 97 | + { |
| 98 | + Timestamp = timestamp, |
| 99 | + CostInMainLoop = double.Parse(costMatch.Groups[1].Value), |
| 100 | + CostOutsideMainLoop = double.Parse(costMatch.Groups[2].Value), |
| 101 | + PercentageOutside = double.Parse(costMatch.Groups[3].Value), |
| 102 | + |
| 103 | + // Associate with the most recent skeleton count to allow grouping by skeletons |
| 104 | + ActiveSkeletons = currentActiveSkeletons, |
| 105 | + MaxActiveSkeletons = currentMaxActive, |
| 106 | + TotalSkeletons = currentTotal, |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return (session, dataPoints); |
| 112 | + } |
| 113 | + |
| 114 | + private string ExtractVersion(string firstLine) |
| 115 | + { |
| 116 | + // e.g., "hdtSMP64 200500" or "[16:44:22.378] [2840 ] [I] hdtsmp64 v3-1-9-0" |
| 117 | + if (firstLine.Contains("v3", StringComparison.OrdinalIgnoreCase) || firstLine.Contains("hdtsmp", StringComparison.OrdinalIgnoreCase)) |
| 118 | + { |
| 119 | + var match = Regex.Match(firstLine, @"(v\d+-\d+-\d+-\d+|\d{6})", RegexOptions.IgnoreCase); |
| 120 | + if (match.Success) return match.Value; |
| 121 | + |
| 122 | + var vMatch = Regex.Match(firstLine, @"hdtSMP64\s+(.*)", RegexOptions.IgnoreCase); |
| 123 | + if (vMatch.Success) return vMatch.Groups[1].Value.Trim(); |
| 124 | + } |
| 125 | + return "Unknown"; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments