]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Misc/CustomAssert.cpp
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Misc / CustomAssert.cpp
1 /*\r
2 * Copyright (c) 2008, Power of Two Games LLC\r
3 *               2010, Strahinja Markovic\r
4 *\r
5 * Redistribution and use in source and binary forms, with or without\r
6 * modification, are permitted provided that the following conditions are met:\r
7 *     * Redistributions of source code must retain the above copyright\r
8 *       notice, this list of conditions and the following disclaimer.\r
9 *     * Redistributions in binary form must reproduce the above copyright\r
10 *       notice, this list of conditions and the following disclaimer in the\r
11 *       documentation and/or other materials provided with the distribution.\r
12 *     * Neither the name of Power of Two Games LLC nor the\r
13 *       names of its contributors may be used to endorse or promote products\r
14 *       derived from this software without specific prior written permission.\r
15 *\r
16 * THIS SOFTWARE IS PROVIDED BY POWER OF TWO GAMES LLC ``AS IS'' AND ANY\r
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
19 * DISCLAIMED. IN NO EVENT SHALL POWER OF TWO GAMES LLC BE LIABLE FOR ANY\r
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 */\r
27 \r
28 #include <stdafx.h>\r
29 #include "CustomAssert.h"\r
30 #include <cstdio>\r
31 #include <cstdarg>\r
32 \r
33 namespace assert_ns\r
34 {\r
35 \r
36 namespace\r
37 {\r
38 \r
39 Assert::FailBehavior DefaultHandler(const char* condition, \r
40                                                                         const char* msg, \r
41                                                                         const char* file, \r
42                                                                         const int line)\r
43 {\r
44         std::printf("%s(%d): Assert Failure: ", file, line);\r
45         \r
46         if (condition != NULL)\r
47                 std::printf("'%s' ", condition);\r
48 \r
49         if (msg != NULL)\r
50                 std::printf("%s", msg);\r
51 \r
52         std::printf("\n");\r
53 \r
54         return Assert::Halt;\r
55 }\r
56 \r
57 Assert::Handler& GetAssertHandlerInstance()\r
58 {\r
59         static Assert::Handler s_handler = &DefaultHandler;\r
60         return s_handler;\r
61 }\r
62 \r
63 }\r
64 \r
65 Assert::Handler Assert::GetHandler()\r
66 {\r
67         return GetAssertHandlerInstance();\r
68 }\r
69 \r
70 void Assert::SetHandler(Assert::Handler newHandler)\r
71 {\r
72         GetAssertHandlerInstance() = newHandler;\r
73 }\r
74 \r
75 Assert::FailBehavior Assert::ReportFailure(const char* condition, \r
76                                                                                    const char* file, \r
77                                                                                    const int line, \r
78                                                                                    const char* msg, ...)\r
79 {\r
80         const char* message = NULL;\r
81         if (msg != NULL)\r
82         {\r
83                 char messageBuffer[1024];\r
84                 {\r
85                         va_list args;\r
86                         va_start(args, msg);\r
87 \r
88             #if defined(_MSC_VER)\r
89                         vsnprintf_s(messageBuffer, 1024, 1024, msg, args);\r
90             #else\r
91             vsnprintf(messageBuffer, 1024, msg, args);\r
92             #endif\r
93 \r
94                         va_end(args);\r
95                 }\r
96 \r
97                 message = messageBuffer;\r
98         }\r
99 \r
100         return GetAssertHandlerInstance()(condition, message, file, line);\r
101 }\r
102 \r
103 }\r