|
| 1 | +using UniGetUI.Core.Logging; |
| 2 | +using UniGetUI.Core.SettingsEngine; |
| 3 | +using UniGetUI.PackageEngine; |
| 4 | +using UniGetUI.PackageEngine.Interfaces; |
| 5 | + |
| 6 | +namespace UniGetUI.Interface; |
| 7 | + |
| 8 | +public sealed class AutomationAppLogEntry |
| 9 | +{ |
| 10 | + public string Time { get; set; } = ""; |
| 11 | + public string Severity { get; set; } = ""; |
| 12 | + public string Content { get; set; } = ""; |
| 13 | +} |
| 14 | + |
| 15 | +public sealed class AutomationOperationHistoryEntry |
| 16 | +{ |
| 17 | + public string Content { get; set; } = ""; |
| 18 | +} |
| 19 | + |
| 20 | +public sealed class AutomationManagerLogTask |
| 21 | +{ |
| 22 | + public int Index { get; set; } |
| 23 | + public string[] Lines { get; set; } = []; |
| 24 | +} |
| 25 | + |
| 26 | +public sealed class AutomationManagerLogInfo |
| 27 | +{ |
| 28 | + public string Name { get; set; } = ""; |
| 29 | + public string DisplayName { get; set; } = ""; |
| 30 | + public string Version { get; set; } = ""; |
| 31 | + public AutomationManagerLogTask[] Tasks { get; set; } = []; |
| 32 | +} |
| 33 | + |
| 34 | +public static class AutomationLogsApi |
| 35 | +{ |
| 36 | + public static IReadOnlyList<AutomationAppLogEntry> ListAppLog(int level = 4) |
| 37 | + { |
| 38 | + return Logger.GetLogs() |
| 39 | + .Where(entry => !string.IsNullOrWhiteSpace(entry.Content) && !ShouldSkip(entry.Severity, level)) |
| 40 | + .Select(entry => new AutomationAppLogEntry |
| 41 | + { |
| 42 | + Time = entry.Time.ToString("O"), |
| 43 | + Severity = entry.Severity.ToString().ToLowerInvariant(), |
| 44 | + Content = entry.Content, |
| 45 | + }) |
| 46 | + .ToArray(); |
| 47 | + } |
| 48 | + |
| 49 | + public static IReadOnlyList<AutomationOperationHistoryEntry> ListOperationHistory() |
| 50 | + { |
| 51 | + return Settings.GetValue(Settings.K.OperationHistory) |
| 52 | + .Split('\n') |
| 53 | + .Select(line => line.Replace("\r", "").Replace("\n", "").Trim()) |
| 54 | + .Where(line => !string.IsNullOrWhiteSpace(line)) |
| 55 | + .Select(line => new AutomationOperationHistoryEntry { Content = line }) |
| 56 | + .ToArray(); |
| 57 | + } |
| 58 | + |
| 59 | + public static IReadOnlyList<AutomationManagerLogInfo> ListManagerLogs( |
| 60 | + string? managerName = null, |
| 61 | + bool verbose = false |
| 62 | + ) |
| 63 | + { |
| 64 | + return ResolveManagers(managerName) |
| 65 | + .Select(manager => new AutomationManagerLogInfo |
| 66 | + { |
| 67 | + Name = manager.Name, |
| 68 | + DisplayName = manager.DisplayName, |
| 69 | + Version = manager.Status.Version, |
| 70 | + Tasks = manager.TaskLogger.Operations |
| 71 | + .Select((operation, index) => new AutomationManagerLogTask |
| 72 | + { |
| 73 | + Index = index, |
| 74 | + Lines = operation |
| 75 | + .AsColoredString(verbose) |
| 76 | + .Where(line => !string.IsNullOrWhiteSpace(line)) |
| 77 | + .Select(StripColorCode) |
| 78 | + .Where(line => !string.IsNullOrWhiteSpace(line)) |
| 79 | + .ToArray(), |
| 80 | + }) |
| 81 | + .Where(task => task.Lines.Length > 0) |
| 82 | + .ToArray(), |
| 83 | + }) |
| 84 | + .ToArray(); |
| 85 | + } |
| 86 | + |
| 87 | + private static IReadOnlyList<IPackageManager> ResolveManagers(string? managerName) |
| 88 | + { |
| 89 | + var managers = PEInterface.Managers |
| 90 | + .Where(manager => |
| 91 | + string.IsNullOrWhiteSpace(managerName) |
| 92 | + || manager.Name.Equals(managerName, StringComparison.OrdinalIgnoreCase) |
| 93 | + || manager.DisplayName.Equals(managerName, StringComparison.OrdinalIgnoreCase) |
| 94 | + ) |
| 95 | + .OrderBy(manager => manager.DisplayName, StringComparer.OrdinalIgnoreCase) |
| 96 | + .ToArray(); |
| 97 | + |
| 98 | + if (managers.Length == 0) |
| 99 | + { |
| 100 | + throw new InvalidOperationException( |
| 101 | + string.IsNullOrWhiteSpace(managerName) |
| 102 | + ? "No package managers are available." |
| 103 | + : $"No package manager matching \"{managerName}\" was found." |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + return managers; |
| 108 | + } |
| 109 | + |
| 110 | + private static bool ShouldSkip(LogEntry.SeverityLevel severity, int level) => |
| 111 | + level switch |
| 112 | + { |
| 113 | + <= 1 => severity != LogEntry.SeverityLevel.Error, |
| 114 | + 2 => severity is LogEntry.SeverityLevel.Debug |
| 115 | + or LogEntry.SeverityLevel.Info |
| 116 | + or LogEntry.SeverityLevel.Success, |
| 117 | + 3 => severity is LogEntry.SeverityLevel.Debug or LogEntry.SeverityLevel.Info, |
| 118 | + 4 => severity == LogEntry.SeverityLevel.Debug, |
| 119 | + _ => false, |
| 120 | + }; |
| 121 | + |
| 122 | + private static string StripColorCode(string line) |
| 123 | + { |
| 124 | + return line.Length > 1 && char.IsDigit(line[0]) ? line[1..] : line; |
| 125 | + } |
| 126 | +} |
0 commit comments