1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using System.Diagnostics;
9 using System.Linq.Expressions;
10 using System.Runtime.CompilerServices;
11 using Antmicro.Renode.Exceptions;
12 using Antmicro.Renode.Logging;
13 
14 namespace Antmicro.Renode.Debugging
15 {
16     public class DebugHelper
17     {
18         [Conditional("DEBUG")]
Assert(bool condition, string message = R, [CallerMemberName] string memberName = R, [CallerFilePath] string sourceFilePath = R, [CallerLineNumber] int sourceLineNumber = 0)19         public static void Assert(bool condition,
20             string message = "",
21             [CallerMemberName] string memberName = "",
22             [CallerFilePath] string sourceFilePath = "",
23             [CallerLineNumber] int sourceLineNumber = 0)
24         {
25             if(!condition)
26             {
27                 var formattedMessage = $"Assertion in {memberName} ({sourceFilePath}:{sourceLineNumber}) failed. {(string.IsNullOrEmpty(message) ? string.Empty : message)}";
28                 Logger.Log(null, LogLevel.Error, formattedMessage);
29                 throw new AssertionException(formattedMessage);
30             }
31         }
32     }
33 
34     // in fact this should be more `EmulationException` as we cannot guarantee that we can recover from it in any way
35     public class AssertionException : RecoverableException
36     {
AssertionException(string message)37         public AssertionException(string message) : base(message)
38         {
39         }
40     }
41 }
42