Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Package Manager Loggers #2292

Merged
merged 9 commits into from
Jun 9, 2024
36 changes: 36 additions & 0 deletions src/UniGetUI.PackageEngine.Enums/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,40 @@ public enum OperationType
Uninstall,
None
}

public enum LoggableTaskType
{
/// <summary>
/// Installs a required dependency for a Package Manager
/// </summary>
InstallManagerDependency,
/// <summary>
/// Searches for packages with a specific query
/// </summary>
FindPackages,
/// <summary>
/// Lists all the available updates
/// </summary>
ListUpdates,
/// <summary>
/// Lists the installed packages
/// </summary>
ListPackages,
/// <summary>
/// Refreshes the package indexes
/// </summary>
RefreshIndexes,
/// <summary>
/// Lists the available sources for the manager
/// </summary>
ListSources,
/// <summary>
/// Loads the package details for a specific package
/// </summary>
LoadPackageDetails,
/// <summary>
/// Loads the available versions for a specific package
/// </summary>
LoadPackageVersions,
}
}
109 changes: 109 additions & 0 deletions src/UniGetUI.PackageEngine.ManagerLogger/ManagerLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System.Diagnostics;
using UniGetUI.PackageEngine.ManagerClasses.Manager;

namespace UniGetUI.PackageEngine.ManagerLogger
{
public class ManagerLogger
{
public enum OperationType
{
SearchPackages,
CheckUpdates,
ListInstalledPackages,
RefreshIndexes,
ListSources,
GetPackageDetails,
GetPackageVersions,
}

public class OperationLog
{
ManagerLogger Logger;
OperationType Type;

string Executable;
string Arguments;
int ReturnCode;
DateTime StartTime;
DateTime? EndTime;
List<string> StdOut = new();
List<string> StdErr = new();
bool isComplete = false;
bool isOpen = false;

public OperationLog(ManagerLogger logger, OperationType type, string executable, string arguments)
{
Type = type;
Logger = logger;
Executable = executable;
Arguments = arguments;
StartTime = DateTime.Now;
isComplete = false;
isOpen = true;
}

public void AddStdoutLine(string? line)
{
if (!isOpen) throw new Exception("Attempted to write log into an already-closed OperationLog");
if (line != null)
StdOut.Add(line);
}

public void AddStdoutLines(IEnumerable<string> lines)
{
if (!isOpen) throw new Exception("Attempted to write log into an already-closed OperationLog");
foreach (string line in lines)
StdOut.Add(line);
}

public void AddStderrLine(string? line)
{
if (!isOpen) throw new Exception("Attempted to write log into an already-closed OperationLog");
if (line != null)
StdErr.Add(line);
}

public void AddStderrLines(IEnumerable<string> lines)
{
if (!isOpen) throw new Exception("Attempted to write log into an already-closed OperationLog");
foreach (string line in lines)
StdErr.Add(line);
}

public void End()
{
EndTime = DateTime.Now;
isComplete = true;
isOpen = false;
Logger.LoadOperation(this);
}
}

PackageManager Manager;
List<OperationLog> Operations = new();


public ManagerLogger(PackageManager manager)
{
Manager = manager;
}

private void LoadOperation(OperationLog operation)
{
Operations.Add(operation);
}

public OperationLog CreateOperationLog(OperationType type, Process process)
{
if (process.StartInfo == null)
throw new Exception("Process instance did not have a valid StartInfo value");

return new OperationLog(this, type, process.StartInfo.FileName, process.StartInfo.Arguments);
}

public OperationLog CreateNativeOperation(OperationType type, string ExtraArgument = "N/A")
{
return new OperationLog(this, type, $"{Manager.Name} native operation", ExtraArgument);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
<RuntimeIdentifier>win-$(Platform)</RuntimeIdentifier>
<Platforms>x64</Platforms>
<TargetPlatformMinVersion>10.0.19041.0</TargetPlatformMinVersion>
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
<SdkVersion>8.0.204</SdkVersion>
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
<PublishSelfContained>true</PublishSelfContained>
<FileVersion>3.1.0.0</FileVersion>
<InformationalVersion>3.1.0-alpha0</InformationalVersion>
<Product>UniGetUI</Product>
<Authors>Martí Climent and the contributors</Authors>
<PublisherName>Martí Climent</PublisherName>
<ApplicationVersion>3.1.0-alpha0</ApplicationVersion>
<Copyright>2024, Martí Climent</Copyright>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\UniGetUI.Core.Logger\UniGetUI.Core.Logging.csproj" />
<ProjectReference Include="..\UniGetUI.Core.Settings\UniGetUI.Core.Settings.csproj" />
<ProjectReference Include="..\UniGetUI.Core.Tools\UniGetUI.Core.Tools.csproj" />
<ProjectReference Include="..\UniGetUI.PackageEngine.Enums\UniGetUI.PackageEngine.Enums.csproj" />
<ProjectReference Include="..\UniGetUI.PackageEngine.PackageManagerClasses\UniGetUI.PackageEngine.Classes.csproj" />
</ItemGroup>

</Project>
20 changes: 10 additions & 10 deletions src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ protected override async Task<Package[]> GetAvailableUpdates_UnSafe()
StandardOutputEncoding = System.Text.Encoding.UTF8
};

var logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p);
p.Start();

string? line;
string output = "";
List<Package> Packages = new();
while ((line = await p.StandardOutput.ReadLineAsync()) != null)
{
output += line + "\n";
logger.AddToStdOut(line);
if (!line.StartsWith("Chocolatey"))
{
string[] elements = line.Split('|');
Expand All @@ -95,10 +96,9 @@ protected override async Task<Package[]> GetAvailableUpdates_UnSafe()
}
}

output += await p.StandardError.ReadToEndAsync();
LogOperation(p, output);

logger.AddToStdErr(await p.StandardError.ReadToEndAsync());
await p.WaitForExitAsync();
logger.Close(p.ExitCode);

return Packages.ToArray();
}
Expand All @@ -118,13 +118,14 @@ protected override async Task<Package[]> GetInstalledPackages_UnSafe()
StandardOutputEncoding = System.Text.Encoding.UTF8
};

var logger = TaskLogger.CreateNew(LoggableTaskType.ListPackages, p);
p.Start();

string? line;
string output = "";
List<Package> Packages = new();
while ((line = await p.StandardOutput.ReadLineAsync()) != null)
{
output += line + "\n";
logger.AddToStdOut(line);
if (!line.StartsWith("Chocolatey"))
{
string[] elements = line.Split(' ');
Expand All @@ -140,10 +141,9 @@ protected override async Task<Package[]> GetInstalledPackages_UnSafe()
}
}

output += await p.StandardError.ReadToEndAsync();
LogOperation(p, output);

logger.AddToStdErr(await p.StandardError.ReadToEndAsync());
await p.WaitForExitAsync();
logger.Close(p.ExitCode);

return Packages.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ protected override async Task<ManagerSource[]> GetSources_UnSafe()
{
List<ManagerSource> sources = new();

Process process = new();
ProcessStartInfo startInfo = new()
Process p = new();
p.StartInfo = new()
{
FileName = Manager.Status.ExecutablePath,
Arguments = Manager.Properties.ExecutableCallArgs + " source list",
Expand All @@ -48,15 +48,13 @@ protected override async Task<ManagerSource[]> GetSources_UnSafe()
StandardOutputEncoding = System.Text.Encoding.UTF8
};

process.StartInfo = startInfo;
process.Start();
var logger = Manager.TaskLogger.CreateNew(LoggableTaskType.ListSources, p);
p.Start();


string output = "";
string? line;
while ((line = await process.StandardOutput.ReadLineAsync()) != null)
while ((line = await p.StandardOutput.ReadLineAsync()) != null)
{
output += line + "\n";
logger.AddToStdOut(line);
try
{
if (string.IsNullOrEmpty(line))
Expand All @@ -73,14 +71,14 @@ protected override async Task<ManagerSource[]> GetSources_UnSafe()
}
catch (Exception e)
{
Logger.Error(e);
logger.AddToStdErr(e.ToString());
}
}

output += await process.StandardError.ReadToEndAsync();
Manager.LogOperation(process, output);

await process.WaitForExitAsync();
logger.AddToStdErr(await p.StandardError.ReadToEndAsync());
await p.WaitForExitAsync();
logger.Close(p.ExitCode);

return sources.ToArray();
}
}
Expand Down
Loading
Loading