Skip to content

Commit

Permalink
Better Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
marcussacana committed Jul 5, 2022
1 parent cb369d4 commit e5e826d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 22 deletions.
17 changes: 14 additions & 3 deletions DirectPackageInstaller/DirectPackageInstaller/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static async Task<bool> RunInNewThread(Action Function)
return Failed;
}

internal static Settings Config;
internal static Settings Config = new Settings();

internal static WebClientWithCookies HttpClient = new WebClientWithCookies();
internal static bool IsRunningOnMono => Type.GetType("Mono.Runtime") != null;
Expand Down Expand Up @@ -255,8 +255,19 @@ public enum OS
public static bool UseSDCard = false;
public static string? CacheBaseDirectory => (UseSDCard ? AndroidSDCacheDir : AndroidCacheDir) ?? AndroidCacheDir;

public static string WorkingDirectory => _WorkingDir ??= Environment.GetEnvironmentVariable("CD") ?? Directory.GetCurrentDirectory();

public static string WorkingDirectory
{
get
{
var Result = _WorkingDir ??= Environment.GetEnvironmentVariable("CD") ?? Directory.GetCurrentDirectory();

if (string.IsNullOrWhiteSpace(Result.Trim('/', '\\')))
throw new Exception("FAILED TO GET THE WORKING DIRECTORY PATH");

return Result;
}
}

public static Func<long> GetFreeStorageSpace = () =>
{
if (CurrentPlatform == OS.Windows)
Expand Down
21 changes: 13 additions & 8 deletions DirectPackageInstaller/DirectPackageInstaller/Host/PS4Server.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -23,10 +24,8 @@ public class PS4Server
public Dictionary<string, string> JSONs = new Dictionary<string, string>();

Dictionary<string, int> Instances = new Dictionary<string, int>();

#if DEBUG
static TextWriter LOGWRITER = System.IO.File.CreateText(Path.Combine(App.WorkingDirectory, "DPIServer.log"));
#endif

static TextWriter? LOGWRITER = null;
public int Connections { get; private set; } = 0;

public string LastRequestMode = null;
Expand All @@ -37,6 +36,9 @@ public class PS4Server
public string IP { get => Server.Settings.Hostname; }
public PS4Server(string IP, int Port = 9898)
{
if (App.Config.ShowError)
LOGWRITER = System.IO.File.CreateText(Path.Combine(App.WorkingDirectory, "DPIServer.log"));

Server = new Webserver(new WebserverSettings(IP, Port)
{
IO = new WebserverSettings.IOSettings()
Expand All @@ -50,14 +52,17 @@ public PS4Server(string IP, int Port = 9898)
LOG("Server Address: {0}:{1}", IP, Port);
}

public void LOG(string Message, params object[] Format) {
#if DEBUG
private static void LOG(string Message, params object[] Format)
{
if (!App.Config.ShowError || LOGWRITER == null)
return;

lock (LOGWRITER)
{
LOGWRITER.WriteLine(string.Format(Message, Format));
LOGWRITER.WriteLine(Message, Format);
LOGWRITER.Flush();
}
#endif
}

public void Start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static string? MainExecutable

const string UpdateList = "Update.ini";

public static Version CurrentVersion = new Version("6.1.10");
public static Version CurrentVersion = new Version("6.1.11");

public static Version? LastVersion = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public static async Task<bool> PushRPI(string URL, Settings Config, bool Silent)
catch { }
}

await File.WriteAllTextAsync(Path.Combine(App.WorkingDirectory, "DPI-ERROR.log"), ex.ToString());
await MessageBox.ShowAsync("Failed:\n" + Result == null ? ex.ToString() : Result, "DirectPackageInstaller", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,20 @@ public async Task OnShown(MainWindow? Parent)
{
_ = PS4Finder.StartFinder((PS4IP, PCIP) =>
{
Dispatcher.UIThread.InvokeAsync(() =>
Dispatcher.UIThread.InvokeAsync(async () =>
{
if (string.IsNullOrEmpty(Model.PS4IP))
if (!string.IsNullOrEmpty(Model.PS4IP))
{
Model.PS4IP = PS4IP.ToString();

var NewPCIP = PCIP?.ToString() ?? IPHelper.FindLocalIP(PS4IP.ToString()) ?? "";
if (!string.IsNullOrWhiteSpace(NewPCIP) && NewPCIP != "0.0.0.0")
Model.PCIP = NewPCIP;

RestartServer_OnClick(null, null);
if (Model.PS4IP == PS4IP.ToString() || await MessageBox.ShowAsync($"A new PS4 IP has been found ({PS4IP}), Update the PS4 IP?", "DirectPackageInstaller", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
}
Model.PS4IP = PS4IP.ToString();

var NewPCIP = PCIP?.ToString() ?? IPHelper.FindLocalIP(PS4IP.ToString()) ?? "";
if (!string.IsNullOrWhiteSpace(NewPCIP) && NewPCIP != "0.0.0.0")
Model.PCIP = NewPCIP;

RestartServer_OnClick(null, null);
});
});
}
Expand Down Expand Up @@ -469,6 +471,8 @@ private async void BtnLoadOnClick(object? sender, RoutedEventArgs e)
}
catch (Exception ex)
{
await File.WriteAllTextAsync(Path.Combine(App.WorkingDirectory, "DPI-ERROR.log"), ex.ToString());

if (ex is not AbortException)
await MessageBox.ShowAsync(ex.ToString(), "DirectPackageInstaller - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (App.Config.ShowError)
Expand Down Expand Up @@ -1067,4 +1071,4 @@ protected override Size MeasureCore(Size availableSize)
return base.MeasureCore(availableSize);
}
}
}
}

0 comments on commit e5e826d

Please sign in to comment.