1 | // tAssert.cpp  |
2 | //  |
3 | // Tacent asserts and warnings.  |
4 | //  |
5 | // Copyright (c) 2004-2006, 2015, 2017 Tristan Grimmer.  |
6 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
7 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
8 | //  |
9 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
10 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
11 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
12 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
13 | // PERFORMANCE OF THIS SOFTWARE.  |
14 |   |
15 | #include <stdio.h>  |
16 | #include "Foundation/tPlatform.h"  |
17 | #ifdef PLATFORM_WINDOWS  |
18 | #include <signal.h>  |
19 | #include <Windows.h>  |
20 | #endif  |
21 |   |
22 |   |
23 | void tAbort()  |
24 | {  |
25 | // Note the use of stdio printf here to reduce dependencies.  |
26 | printf("%s(%d) Tacent Abort\n" , __FILE__, __LINE__);  |
27 | abort();  |
28 | }  |
29 |   |
30 |   |
31 | void tAssertPrintBreak(const char* expr, const char* fileName, int lineNum, const char* msg)  |
32 | {  |
33 | const int assertMessageSize = 4*1024;  |
34 | char message[assertMessageSize];  |
35 |   |
36 | snprintf  |
37 | (  |
38 | message, assertMessageSize,  |
39 | "Tacent Assert Failed.\n\n"   |
40 | "Expr: [%s]\n"   |
41 | "File: [%s]\n"   |
42 | "Line: [%d]\n"   |
43 | "Msg : [%s]\n\n"   |
44 | #ifdef PLATFORM_WINDOWS  |
45 | "Press 'Abort' to abort the program completely.\n"   |
46 | "Press 'Retry' to start debugging.\n"   |
47 | "Press 'Ignore' to try and move past this assert and continue running.\n"   |
48 | #endif  |
49 | ,  |
50 | expr, fileName, lineNum, msg ? msg : "None"   |
51 | );  |
52 | printf("%s" , message);  |
53 |   |
54 | #ifdef PLATFORM_WINDOWS  |
55 | // In windows we bring up a message box.  |
56 | int retCode = ::MessageBox  |
57 | (  |
58 | 0, message, "Tacent Assert" ,  |
59 | MB_ABORTRETRYIGNORE | MB_ICONHAND | MB_SETFOREGROUND | MB_TASKMODAL  |
60 | );  |
61 |   |
62 | switch (retCode)  |
63 | {  |
64 | case IDABORT:  |
65 | // Exit ungracefully.  |
66 | raise(SIGABRT);  |
67 | _exit(200);  |
68 | break;  |
69 |   |
70 | case IDRETRY:  |
71 | // Attempt break to debugger.  |
72 | __debugbreak();  |
73 | return;  |
74 |   |
75 | case IDIGNORE:  |
76 | return;  |
77 | }  |
78 | #endif  |
79 | }  |
80 | |