xref: /oneTBB/src/tbb/assert_impl.h (revision 8dcbd5b1)
1 /*
2     Copyright (c) 2005-2020 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_assert_impl_H
18 #define __TBB_assert_impl_H
19 
20 #include "oneapi/tbb/detail/_config.h"
21 
22 #include <cstdio>
23 #include <cstdlib>
24 #include <cstring>
25 #include <cstdarg>
26 #if _MSC_VER && _DEBUG
27 #include <crtdbg.h>
28 #endif
29 
30 #include <mutex>
31 
32 namespace tbb {
33 namespace detail {
34 namespace r1 {
35 
36 // TODO: consider extension for formatted error description string
37 static void assertion_failure_impl(const char* filename, int line, const char* expression, const char* comment) {
38     std::fprintf(stderr, "Assertion %s failed on line %d of file %s\n", expression, line, filename);
39     if (comment) {
40         std::fprintf(stderr, "Detailed description: %s\n", comment);
41     }
42 #if _MSC_VER && _DEBUG
43     if (1 == _CrtDbgReport(_CRT_ASSERT, filename, line, "tbb_debug.dll", "%s\r\n%s", expression, comment?comment:"")) {
44         _CrtDbgBreak();
45     }
46 #else
47     std::fflush(stderr);
48     std::abort();
49 #endif
50 }
51 
52 void __TBB_EXPORTED_FUNC assertion_failure(const char* filename, int line, const char* expression, const char* comment) {
53     static std::once_flag flag;
54     std::call_once(flag, [&](){ assertion_failure_impl(filename, line, expression, comment); });
55 }
56 
57 //! Report a runtime warning.
58 void runtime_warning( const char* format, ... ) {
59     char str[1024]; std::memset(str, 0, 1024);
60     va_list args; va_start(args, format);
61     vsnprintf( str, 1024-1, format, args);
62     va_end(args);
63     fprintf(stderr, "TBB Warning: %s\n", str);
64 }
65 
66 } // namespace r1
67 } // namespace detail
68 } // namespace tbb
69 
70 #endif // __TBB_assert_impl_H
71 
72