Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_TestForException.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 
13 #include <iostream>
14 
15 //
16 // ToDo: Make these functions thread-safe!
17 //
18 
19 
20 namespace {
21 
22 
23 int throwNumber = 0;
24 
25 
26 bool& loc_enableStackTrace()
27 {
28  static bool static_enableStackTrace =
29 #ifdef HAVE_TEUCHOS_DEFAULT_STACKTRACE
30  true
31 #else
32  false
33 #endif
34  ;
35  return static_enableStackTrace;
36 }
37 
38 
39 } // namespace
40 
41 
43 {
44  ++throwNumber;
45 }
46 
47 
49 {
50  return throwNumber;
51 }
52 
53 
54 void Teuchos::TestForException_break(const std::string &errorMsg, int throwNumber)
55 {
56  (void)throwNumber; // Ignore unused arg
57  // Provide a statement to break on
58  size_t break_on_me;
59  break_on_me = errorMsg.length(); // Use errMsg to avoid compiler warning.
60  if (break_on_me) {} // Avoid warning
61  // Above is just some statement for the debugger to break on. Note: now is
62  // a good time to examine the stack trace and look at the error message in
63  // 'errorMsg' to see what happened. In GDB just type 'where' or you can go
64  // up by typing 'up' and moving up in the stack trace to see where you are
65  // and how you got to this point in the code where you are throwing this
66  // exception! Typing in a 'p errorMsg' will show you what the error message
67  // is. Also, you should consider adding a conditional break-point in this
68  // function based on a specific value of 'throwNumber' if the exception you
69  // want to examine is not the first exception thrown.
70 }
71 
72 
74 {
75  loc_enableStackTrace() = enableStrackTrace;
76 }
77 
78 
80 {
81  return loc_enableStackTrace();
82 }
83 
84 void Teuchos::TestForTermination_terminate(const std::string &msg) {
85  std::ostringstream omsg;
86  if (GlobalMPISession::getNProc() > 1) {
87  omsg << "p="<<GlobalMPISession::getRank()<<": ";
88  }
89  omsg << msg << "\n";
90  std::cerr << omsg.str();
91  std::terminate();
92 }
93 // NOTE: The above usage of ostringstream is so that the output to std::cerr
94 // is done as one string. This should help to avoid jumbled output like is
95 // occurring in tests that grep for this output (see #3163).
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_setEnableStacktrace(bool enableStrackTrace)
Set at runtime if stacktracing functionality is enabled when * exceptions are thrown.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForTermination_terminate(const std::string &msg)
Prints the message to std::cerr and calls std::terminate.
TEUCHOSCORE_LIB_DLL_EXPORT int TestForException_getThrowNumber()
Increment the throw number.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_incrThrowNumber()
Increment the throw number.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_break(const std::string &msg, int throwNumber)
The only purpose for this function is to set a breakpoint.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Standard test and throw macros.
TEUCHOSCORE_LIB_DLL_EXPORT bool TestForException_getEnableStacktrace()
Get at runtime if stacktracing functionality is enabled when exceptions are thrown.