Skip to content

Commit

Permalink
Logs and exceptions as classes
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Dec 4, 2019
1 parent dfd5d72 commit f67a235
Show file tree
Hide file tree
Showing 33 changed files with 400 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Compression.BSA.Test/BSATests.cs
Expand Up @@ -31,7 +31,7 @@ public class BSATests
public static void Setup(TestContext testContext)
{
Queue = new WorkQueue();
Utils.LogMessages.Subscribe(f => testContext.WriteLine(f));
Utils.LogMessages.Subscribe(f => testContext.WriteLine(f.ShortDescription));
if (!Directory.Exists(_stagingFolder))
Directory.CreateDirectory(_stagingFolder);

Expand Down
18 changes: 10 additions & 8 deletions Wabbajack.Common/FileExtractor.cs
Expand Up @@ -10,6 +10,8 @@
using ICSharpCode.SharpZipLib.GZip;
using Newtonsoft.Json;
using OMODFramework;
using Wabbajack.Common.StatusFeed;
using Wabbajack.Common.StatusFeed.Errors;

namespace Wabbajack.Common
{
Expand Down Expand Up @@ -46,11 +48,11 @@ public static void ExtractAll(WorkQueue queue, string source, string dest)
else if (source.EndsWith(".exe"))
ExtractAllWithInno(source, dest);
else
ExtractAllWith7Zip(source, dest);
ExtractAllWith7Zip(queue, source, dest);
}
catch (Exception ex)
{
Utils.Log($"Error while extracting {source}");
queue.Log($"Error while extracting {source}");
throw ex;
}
}
Expand Down Expand Up @@ -152,14 +154,14 @@ private static void ExtractAllWithBSA(WorkQueue queue, string source, string des
}
catch (Exception ex)
{
Utils.Log($"While Extracting {source}");
queue.Log($"While Extracting {source}");
throw ex;
}
}

private static void ExtractAllWith7Zip(string source, string dest)
private static void ExtractAllWith7Zip(WorkQueue queue, string source, string dest)
{
Utils.Log($"Extracting {Path.GetFileName(source)}");
queue.Log(new GenericInfo($"Extracting {Path.GetFileName(source)}", $"The contents of {source} are being extracted to {dest} using 7zip.exe"));

var info = new ProcessStartInfo
{
Expand Down Expand Up @@ -204,11 +206,11 @@ private static void ExtractAllWith7Zip(string source, string dest)
}

p.WaitForExit();
if (p.ExitCode != 0)
if (p.ExitCode == 0)
{
Utils.Log(p.StandardOutput.ReadToEnd());
Utils.Log($"Extraction error extracting {source}");
return;
}
queue.Log(new _7zipReturnError(p.ExitCode, source, dest, p.StandardOutput.ReadToEnd()));
}

/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions Wabbajack.Common/StatusFeed/AErrorMessage.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
public abstract class AErrorMessage : Exception, IError
{
public DateTime Timestamp { get; } = DateTime.Now;
public abstract string ShortDescription { get; }
public abstract string ExtendedDescription { get; }
}
}
15 changes: 15 additions & 0 deletions Wabbajack.Common/StatusFeed/AStatusMessage.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
public abstract class AStatusMessage : IStatusMessage
{
public DateTime Timestamp { get; } = DateTime.Now;
public abstract string ShortDescription { get; }
public abstract string ExtendedDescription { get; }
}
}
33 changes: 33 additions & 0 deletions Wabbajack.Common/StatusFeed/Errors/7zipReturnError.cs
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed.Errors
{
public class _7zipReturnError : AStatusMessage, IError
{
private string _destination;
private string _filename;
private int _code;
private string _7zip_output;
public override string ShortDescription => $"7Zip returned an error while executing";

public override string ExtendedDescription =>
$@"7Zip.exe should always return 0 when it finishes executing. While extracting {_filename} 7Zip encountered some error and
instead returned {_code} which indicates there was an error. The archive might be corrupt or in a format that 7Zip cannot handle. Please verify the file is valid and that you
haven't run out of disk space in the {_destination} folder.
7Zip Output:
{_7zip_output}";

public _7zipReturnError(int code, string filename, string destination, string output)
{
_code = code;
_filename = filename;
_destination = destination;
_7zip_output = output;
}
}
}
22 changes: 22 additions & 0 deletions Wabbajack.Common/StatusFeed/Errors/FileExtractionError.cs
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed.Errors
{
class FileExtractionError : AStatusMessage, IError
{
private string _filename;
private string _destination;
public override string ShortDescription { get; }
public override string ExtendedDescription { get; }

public FileExtractionError(string filename, string destination)
{
_filename = filename;
_destination = destination;
}
}
}
21 changes: 21 additions & 0 deletions Wabbajack.Common/StatusFeed/Errors/UnconvertedError.cs
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed.Errors
{
public class UnconvertedError : AErrorMessage
{
private string _msg;

public UnconvertedError(string msg)
{
_msg = msg;
}

public override string ShortDescription { get => _msg; }
public override string ExtendedDescription { get; } = "";
}
}
25 changes: 25 additions & 0 deletions Wabbajack.Common/StatusFeed/GenericInfo.cs
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
public class GenericInfo : AStatusMessage, IInfo
{
public override string ShortDescription { get; }
public override string ExtendedDescription { get;}

public GenericInfo(string short_description, string long_description = "")
{
ShortDescription = short_description;
ExtendedDescription = long_description;
}

public override string ToString()
{
return ShortDescription;
}
}
}
12 changes: 12 additions & 0 deletions Wabbajack.Common/StatusFeed/IError.cs
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
public interface IError : IStatusMessage
{
}
}
13 changes: 13 additions & 0 deletions Wabbajack.Common/StatusFeed/IInfo.cs
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
public interface IInfo : IStatusMessage
{

}
}
15 changes: 15 additions & 0 deletions Wabbajack.Common/StatusFeed/IStatusMessage.cs
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
public interface IStatusMessage
{
DateTime Timestamp { get; }
string ShortDescription { get; }
string ExtendedDescription { get; }
}
}
43 changes: 43 additions & 0 deletions Wabbajack.Common/StatusFeed/IUserIntervention.cs
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wabbajack.Common.StatusFeed
{
/// <summary>
/// Defines a message that requires user interaction. The user must perform some action
/// or make a choice.
/// </summary>
public interface IUserIntervention<T> : IStatusMessage
{
/// <summary>
/// The user didn't make a choice, so this action should be aborted
/// </summary>
void Cancel();

/// <summary>
/// The user has provided the required information.
/// </summary>
/// <param name="result"></param>
void Resume(T result);
}

/// <summary>
/// Defines a message that requires user interaction. The user must perform some action
/// or make a choice.
/// </summary>
public interface IUserIntervention : IStatusMessage
{
/// <summary>
/// The user didn't make a choice, so this action should be aborted
/// </summary>
void Cancel();

/// <summary>
/// Resume without any further information
/// </summary>
void Resume();
}
}
1 change: 1 addition & 0 deletions Wabbajack.Common/StatusUpdate.cs
@@ -1,5 +1,6 @@
using System;
using System.Reactive.Subjects;
using Wabbajack.Common.StatusFeed;

namespace Wabbajack.Common
{
Expand Down
32 changes: 25 additions & 7 deletions Wabbajack.Common/Utils.cs
Expand Up @@ -16,6 +16,8 @@
using ICSharpCode.SharpZipLib.BZip2;
using IniParser;
using Newtonsoft.Json;
using ReactiveUI;
using Wabbajack.Common.StatusFeed;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Directory = System.IO.Directory;
Expand Down Expand Up @@ -44,24 +46,39 @@ static Utils()
File.Delete(LogFile);
}

private static readonly Subject<string> LoggerSubj = new Subject<string>();
public static IObservable<string> LogMessages => LoggerSubj;
private static readonly Subject<IStatusMessage> LoggerSubj = new Subject<IStatusMessage>();
public static IObservable<IStatusMessage> LogMessages => LoggerSubj;

private static readonly string[] Suffix = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}; // Longs run out around EB

private static object _lock = new object();

private static DateTime _startTime;


public static void Log(string msg)
{
Log(new GenericInfo(msg));
}

public static T Log<T>(T msg) where T : IStatusMessage
{
lock (_lock)
{
msg = $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}";

File.AppendAllText(LogFile, msg + "\r\n");
}
LoggerSubj.OnNext(msg);
return msg;
}

public static void Error(AErrorMessage err)
{
lock (_lock)
{
File.AppendAllText(LogFile, err.ShortDescription + "\r\n");
}
LoggerSubj.OnNext(err);
throw err;
}

public static void LogToFile(string msg)
Expand Down Expand Up @@ -668,10 +685,11 @@ public static bool TryGetPatch(string foundHash, string fileHash, out byte[] ePa
return false;
}

/*
public static void Warning(string s)
{
Log($"WARNING: {s}");
}
}*/

public static TV GetOrDefault<TK, TV>(this Dictionary<TK, TV> dict, TK key)
{
Expand Down Expand Up @@ -708,11 +726,12 @@ public static T ViaJSON<T>(this T tv)
return tv.ToJSON().FromJSONString<T>();
}

/*
public static void Error(string msg)
{
Log(msg);
throw new Exception(msg);
}
}*/

public static Stream GetEmbeddedResourceStream(string name)
{
Expand All @@ -738,7 +757,6 @@ public static T FromYaml<T>(this string s)
.Build();
return d.Deserialize<T>(new StringReader(s));
}

public static void LogStatus(string s)
{
Status(s);
Expand Down

0 comments on commit f67a235

Please sign in to comment.