Skip to content

Commit

Permalink
imp - brk|doc - Use CMN, CLN, and CFP in debug ass...
Browse files Browse the repository at this point in the history
...ert

---

Refer to the previous commit, c58f8fd,
for more information.

---

Type: imp
Breaking: True
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Jan 6, 2025
1 parent c58f8fd commit 4cd22c4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
changes!
-->
<NitrocidModAPIVersionMajor>3.0.27</NitrocidModAPIVersionMajor>
<NitrocidModAPIVersionChangeset>31</NitrocidModAPIVersionChangeset>
<NitrocidModAPIVersionChangeset>32</NitrocidModAPIVersionChangeset>

<!-- The above two properties are to be installed to the file version -->
<NitrocidModAPIVersion>$(NitrocidModAPIVersionMajor).$(NitrocidModAPIVersionChangeset)</NitrocidModAPIVersion>
Expand Down
16 changes: 8 additions & 8 deletions private/Nitrocid.Tests/Kernel/Debugging/DebugAssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DebugAssertTests
[TestMethod]
[Description("Misc")]
public void TestAssertNormal() =>
Should.NotThrow(() => DebugCheck.Assert(true));
Should.NotThrow(() => DebugCheck.Assert(true, ""));

/// <summary>
/// Tests assertion...
Expand All @@ -50,7 +50,7 @@ public void TestAssertNormalFailing()
{
DriverHandler.RegisterDriver(DriverTypes.Console, new MyCustomConsoleDriver());
DriverHandler.SetDriver<IConsoleDriver>("MyCustom");
Should.Throw(() => DebugCheck.Assert(false), typeof(KernelException));
Should.Throw(() => DebugCheck.Assert(false, ""), typeof(KernelException));
}

/// <summary>
Expand Down Expand Up @@ -79,7 +79,7 @@ public void TestAssertNormalMessageFailing()
[TestMethod]
[Description("Misc")]
public void TestAssertNotNormal() =>
Should.NotThrow(() => DebugCheck.AssertNot(false));
Should.NotThrow(() => DebugCheck.AssertNot(false, ""));

/// <summary>
/// Tests assertion...
Expand All @@ -90,7 +90,7 @@ public void TestAssertNotNormalFailing()
{
DriverHandler.RegisterDriver(DriverTypes.Console, new MyCustomConsoleDriver());
DriverHandler.SetDriver<IConsoleDriver>("MyCustom");
Should.Throw(() => DebugCheck.AssertNot(true), typeof(KernelException));
Should.Throw(() => DebugCheck.AssertNot(true, ""), typeof(KernelException));
}

/// <summary>
Expand Down Expand Up @@ -119,7 +119,7 @@ public void TestAssertNotNormalMessageFailing()
[TestMethod]
[Description("Misc")]
public void TestAssertNull() =>
Should.NotThrow(() => DebugCheck.AssertNull(Array.Empty<string>()));
Should.NotThrow(() => DebugCheck.AssertNull(Array.Empty<string>(), ""));

/// <summary>
/// Tests assertion...
Expand All @@ -130,7 +130,7 @@ public void TestAssertNullFailing()
{
DriverHandler.RegisterDriver(DriverTypes.Console, new MyCustomConsoleDriver());
DriverHandler.SetDriver<IConsoleDriver>("MyCustom");
Should.Throw(() => DebugCheck.AssertNull<string[]?>(null), typeof(KernelException));
Should.Throw(() => DebugCheck.AssertNull<string[]?>(null, ""), typeof(KernelException));
}

/// <summary>
Expand Down Expand Up @@ -159,7 +159,7 @@ public void TestAssertNullMessageFailing()
[TestMethod]
[Description("Misc")]
public void TestAssertNotNull() =>
Should.NotThrow(() => DebugCheck.AssertNotNull<string[]?>(null));
Should.NotThrow(() => DebugCheck.AssertNotNull<string[]?>(null, ""));

/// <summary>
/// Tests assertion...
Expand All @@ -170,7 +170,7 @@ public void TestAssertNotNullFailing()
{
DriverHandler.RegisterDriver(DriverTypes.Console, new MyCustomConsoleDriver());
DriverHandler.SetDriver<IConsoleDriver>("MyCustom");
Should.Throw(() => DebugCheck.AssertNotNull(Array.Empty<string>()), typeof(KernelException));
Should.Throw(() => DebugCheck.AssertNotNull(Array.Empty<string>(), ""), typeof(KernelException));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion public/Nitrocid.Templates/templates/KSMod/ModName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ModName : IMod

public string Version => "1.0.0";

public Version MinimumSupportedApiVersion => new(3, 0, 27, 31);
public Version MinimumSupportedApiVersion => new(3, 0, 27, 32);

public ModLoadPriority LoadPriority => ModLoadPriority.Optional;

Expand Down
163 changes: 36 additions & 127 deletions public/Nitrocid/Kernel/Debugging/DebugCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
using Nitrocid.Kernel.Debugging.Trace;
using Nitrocid.Kernel.Exceptions;
using Nitrocid.Languages;
using System.IO;
using System.Runtime.CompilerServices;
using Textify.General;

namespace Nitrocid.Kernel.Debugging
Expand All @@ -29,70 +31,21 @@ namespace Nitrocid.Kernel.Debugging
/// </summary>
public static class DebugCheck
{
/// <summary>
/// Asserts and checks to see if the condition is satisfied
/// </summary>
/// <param name="condition">Condition</param>
public static void Assert(bool condition) =>
Assert(condition, "");

/// <summary>
/// Asserts and checks to see if the condition is satisfied
/// </summary>
/// <param name="condition">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
public static void Assert(bool condition, string message)
{
if (!condition)
{
var trace = new DebugStackFrame();
var exc = new KernelException(KernelExceptionType.AssertionFailure, $"condition is false. {message}");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Condition is false!");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Failure at {0} routine in {1}:{2}", vars: [trace.RoutineName, trace.RoutineFileName, trace.RoutineLineNumber]);
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Message: {0}", vars: [message]);
KernelPanic.KernelErrorContinuable(Translate.DoTranslation("Assertion failure.") + $" {message}", exc);
throw exc;
}
}

/// <summary>
/// Asserts and checks to see if the condition is satisfied
/// </summary>
/// <param name="condition">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
/// <param name="memberName">Member name. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberLine">Member line number. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberPath">Member path. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="vars">Variables to format the message with</param>
public static void Assert(bool condition, string message, params object[] vars)
public static void Assert(bool condition, string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int memberLine = 0, [CallerFilePath] string memberPath = "", object?[]? vars = null)
{
if (!condition)
{
message = TextTools.FormatString(message, vars);
Assert(condition, message);
}
}

/// <summary>
/// Asserts and checks to see if the condition is not satisfied
/// </summary>
/// <param name="condition">Condition</param>
public static void AssertNot(bool condition) =>
AssertNot(condition, "");

/// <summary>
/// Asserts and checks to see if the condition is not satisfied
/// </summary>
/// <param name="condition">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
public static void AssertNot(bool condition, string message)
{
if (condition)
{
var trace = new DebugStackFrame();
var exc = new KernelException(KernelExceptionType.AssertionFailure, $"condition is true. {message}");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Condition is true!");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Failure at {0} routine in {1}:{2}", vars: [trace.RoutineName, trace.RoutineFileName, trace.RoutineLineNumber]);
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Message: {0}", vars: [message]);
KernelPanic.KernelErrorContinuable(Translate.DoTranslation("Assertion failure.") + $" {message}", exc);
throw exc;
AssertFailInternal(message, "Condition is false!", memberName, memberLine, memberPath);
}
}

Expand All @@ -101,39 +54,16 @@ public static void AssertNot(bool condition, string message)
/// </summary>
/// <param name="condition">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
/// <param name="memberName">Member name. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberLine">Member line number. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberPath">Member path. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="vars">Variables to format the message with</param>
public static void AssertNot(bool condition, string message, params object[] vars)
public static void AssertNot(bool condition, string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int memberLine = 0, [CallerFilePath] string memberPath = "", object?[]? vars = null)
{
if (!condition)
{
message = TextTools.FormatString(message, vars);
AssertNot(condition, message);
}
}

/// <summary>
/// Asserts and checks to see if the value is null
/// </summary>
/// <param name="value">Condition</param>
public static void AssertNull<T>(T value) =>
AssertNull(value, "");

/// <summary>
/// Asserts and checks to see if the value is null
/// </summary>
/// <param name="value">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
public static void AssertNull<T>(T value, string message)
{
if (value is null)
{
var trace = new DebugStackFrame();
var exc = new KernelException(KernelExceptionType.AssertionFailure, $"value is null. {message}");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Value is null!");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Failure at {0} routine in {1}:{2}", vars: [trace.RoutineName, trace.RoutineFileName, trace.RoutineLineNumber]);
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Message: {0}", vars: [message]);
KernelPanic.KernelErrorContinuable(Translate.DoTranslation("Assertion failure.") + $" {message}", exc);
throw exc;
AssertFailInternal(message, "Condition is true!", memberName, memberLine, memberPath);
}
}

Expand All @@ -142,81 +72,60 @@ public static void AssertNull<T>(T value, string message)
/// </summary>
/// <param name="value">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
/// <param name="memberName">Member name. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberLine">Member line number. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberPath">Member path. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="vars">Variables to format the message with</param>
public static void AssertNull<T>(T value, string message, params object[] vars)
public static void AssertNull<T>(T value, string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int memberLine = 0, [CallerFilePath] string memberPath = "", object?[]? vars = null)
{
if (value is null)
{
message = TextTools.FormatString(message, vars);
AssertNull(value, message);
AssertFailInternal(message, "Value is null!", memberName, memberLine, memberPath);
}
}

/// <summary>
/// Asserts and checks to see if the value is not null
/// </summary>
/// <param name="value">Condition</param>
public static void AssertNotNull<T>(T value) =>
AssertNotNull(value, "");

/// <summary>
/// Asserts and checks to see if the value is not null
/// </summary>
/// <param name="value">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
public static void AssertNotNull<T>(T value, string message)
/// <param name="memberName">Member name. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberLine">Member line number. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberPath">Member path. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="vars">Variables to format the message with</param>
public static void AssertNotNull<T>(T value, string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int memberLine = 0, [CallerFilePath] string memberPath = "", object?[]? vars = null)
{
if (value is not null)
{
var trace = new DebugStackFrame();
var exc = new KernelException(KernelExceptionType.AssertionFailure, $"value is not null. {message}");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Value is not null!");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Failure at {0} routine in {1}:{2}", vars: [trace.RoutineName, trace.RoutineFileName, trace.RoutineLineNumber]);
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Message: {0}", vars: [message]);
KernelPanic.KernelErrorContinuable(Translate.DoTranslation("Assertion failure.") + $" {message}", exc);
throw exc;
message = TextTools.FormatString(message, vars);
AssertFailInternal(message, "Value is not null!", memberName, memberLine, memberPath);
}
}

/// <summary>
/// Asserts and checks to see if the value is not null
/// Triggers assertion failure
/// </summary>
/// <param name="value">Condition</param>
/// <param name="message">A message to clarify why the assert failed</param>
/// <param name="memberName">Member name. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberLine">Member line number. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="memberPath">Member path. Do not set unless you know what you're doing. Usually, using <c>vars: [...]</c> directly before the <paramref name="memberName"/> parameter is enough.</param>
/// <param name="vars">Variables to format the message with</param>
public static void AssertNotNull<T>(T value, string message, params object[] vars)
public static void AssertFail(string message, [CallerMemberName] string memberName = "", [CallerLineNumber] int memberLine = 0, [CallerFilePath] string memberPath = "", object?[]? vars = null)
{
if (value is not null)
{
message = TextTools.FormatString(message, vars);
AssertNotNull(value, message);
}
message = TextTools.FormatString(message, vars);
AssertFailInternal(message, "Undetermined failure!", memberName, memberLine, memberPath);
}

/// <summary>
/// Triggers assertion failure
/// </summary>
/// <param name="message">A message to clarify why the assert failed</param>
public static void AssertFail(string message)
private static void AssertFailInternal(string message, string reason, string memberName = "", int memberLine = 0, string memberPath = "")
{
var trace = new DebugStackFrame();
var exc = new KernelException(KernelExceptionType.AssertionFailure, $"undetermined failure. {message}");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Undetermined failure!");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Failure at {0} routine in {1}:{2}", vars: [trace.RoutineName, trace.RoutineFileName, trace.RoutineLineNumber]);
string fileName = Path.GetFileName(memberPath);
var exc = new KernelException(KernelExceptionType.AssertionFailure, $"{reason} {message}");
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! {0}", vars: [reason]);
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Failure at {0} routine in {1}:{2}", vars: [memberName, fileName, memberLine]);
DebugWriter.WriteDebug(DebugLevel.E, "!!! ASSERTION FAILURE !!! Message: {0}", vars: [message]);
KernelPanic.KernelErrorContinuable(Translate.DoTranslation("Assertion failure.") + $" {message}", exc);
throw exc;
}

/// <summary>
/// Triggers assertion failure
/// </summary>
/// <param name="message">A message to clarify why the assert failed</param>
/// <param name="vars">Variables to format the message with</param>
public static void AssertFail(string message, params object[] vars)
{
message = TextTools.FormatString(message, vars);
AssertFail(message);
}
}
}
1 change: 0 additions & 1 deletion public/Nitrocid/Kernel/Debugging/DebugWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace Nitrocid.Kernel.Debugging
/// </summary>
public static class DebugWriter
{
// , [CallerMemberName] string memberName = "", [CallerLineNumber] int memberLine = 0, [CallerFilePath] string memberPath = ""
internal static BaseLogger? debugLogger;
internal static object WriteLock = new();
internal readonly static List<string> debugStackTraces = [];
Expand Down

0 comments on commit 4cd22c4

Please sign in to comment.