forked from Thealexbarney/LibHac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Result methods for debugging (Thealexbarney#90)
- Allow setting a callback function for when Result.Log is called. - Allow setting a function that returns a name for a Result value. - Print Result name in error messages
- Loading branch information
1 parent
172817a
commit cccb811
Showing
6 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using LibHac; | ||
using LibHac.Fs; | ||
|
||
namespace hactoolnet | ||
{ | ||
public static class ResultLogFunctions | ||
{ | ||
private static Dictionary<Result, string> ResultNames { get; } = GetResultNames(); | ||
|
||
public static TextWriter LogWriter { get; set; } | ||
|
||
public static void LogResult(Result result) | ||
{ | ||
if (LogWriter == null) return; | ||
|
||
var st = new StackTrace(2, true); | ||
|
||
if (st.FrameCount > 1) | ||
{ | ||
MethodBase method = st.GetFrame(0).GetMethod(); | ||
|
||
// This result from these functions is usually noise because they | ||
// are frequently used to detect if a file exists | ||
if (result == ResultFs.PathNotFound && | ||
typeof(IFileSystem).IsAssignableFrom(method.DeclaringType) && | ||
method.Name.StartsWith(nameof(IFileSystem.GetEntryType)) || | ||
method.Name.StartsWith(nameof(IAttributeFileSystem.GetFileAttributes))) | ||
{ | ||
// return; | ||
} | ||
|
||
string methodName = $"{method.DeclaringType?.FullName}.{method.Name}"; | ||
|
||
LogWriter.WriteLine($"{result.ToStringWithName()} returned by {methodName}"); | ||
LogWriter.WriteLine(st); | ||
} | ||
} | ||
|
||
public static void LogConvertedResult(Result result, Result originalResult) | ||
{ | ||
if (LogWriter == null) return; | ||
|
||
var st = new StackTrace(2, false); | ||
|
||
if (st.FrameCount > 1) | ||
{ | ||
MethodBase method = st.GetFrame(0).GetMethod(); | ||
|
||
string methodName = $"{method.DeclaringType?.FullName}.{method.Name}"; | ||
|
||
LogWriter.WriteLine($"{originalResult.ToStringWithName()} was converted to {result.ToStringWithName()} by {methodName}"); | ||
} | ||
} | ||
|
||
public static Dictionary<Result, string> GetResultNames() | ||
{ | ||
var dict = new Dictionary<Result, string>(); | ||
|
||
Assembly assembly = typeof(Result).Assembly; | ||
|
||
foreach (Type type in assembly.GetTypes().Where(x => x.Name.Contains("Result"))) | ||
{ | ||
foreach (PropertyInfo property in type.GetProperties() | ||
.Where(x => x.PropertyType == typeof(Result) && x.GetMethod.IsStatic && x.SetMethod == null)) | ||
{ | ||
var value = (Result)property.GetValue(null, null); | ||
string name = $"{type.Name}{property.Name}"; | ||
|
||
dict.Add(value, name); | ||
} | ||
} | ||
|
||
return dict; | ||
} | ||
|
||
public static bool TryGetResultName(Result result, out string name) | ||
{ | ||
return ResultNames.TryGetValue(result, out name); | ||
} | ||
} | ||
} |