Nektar++
Loading...
Searching...
No Matches
Classes | Public Types | Static Public Member Functions | Static Private Attributes | List of all members
Nektar::ErrorUtil Class Reference

#include <ErrorUtil.hpp>

Classes

class  NekError
 

Public Types

enum  ErrType { efatal , ewarning }
 

Static Public Member Functions

static void SetErrorStream (std::ostream &o)
 
static void SetPrintBacktrace (bool b)
 
static bool HasCustomErrorStream ()
 
static void Error (ErrType type, const char *routine, int lineNumber, const char *msg, unsigned int level, bool DoComm=false)
 
static void Error (ErrType type, const char *routine, int lineNumber, const std::string &msg, unsigned int level)
 
static void Error (ErrType type, const char *routine, int lineNumber, const char *msg)
 

Static Private Attributes

static std::ostream * m_outStream = &std::cerr
 
static bool m_printBacktrace = true
 

Detailed Description

Definition at line 54 of file ErrorUtil.hpp.

Member Enumeration Documentation

◆ ErrType

Enumerator
efatal 
ewarning 

Definition at line 65 of file ErrorUtil.hpp.

Member Function Documentation

◆ Error() [1/3]

static void Nektar::ErrorUtil::Error ( ErrType  type,
const char *  routine,
int  lineNumber,
const char *  msg 
)
inlinestatic

Definition at line 193 of file ErrorUtil.hpp.

195 {
196 Error(type, routine, lineNumber, msg, 0);
197 }
static void Error(ErrType type, const char *routine, int lineNumber, const char *msg, unsigned int level, bool DoComm=false)
Definition ErrorUtil.hpp:86

References Error().

◆ Error() [2/3]

static void Nektar::ErrorUtil::Error ( ErrType  type,
const char *  routine,
int  lineNumber,
const char *  msg,
unsigned int  level,
bool  DoComm = false 
)
inlinestatic

Definition at line 86 of file ErrorUtil.hpp.

90 {
91 // The user of outStream is primarily for the unit tests. The unit
92 // tests often generate errors on purpose to make sure invalid usage is
93 // flagged appropriately. Printing the error messages to cerr made the
94 // unit test output hard to parse.
95
96 std::string baseMsg =
97 "Level " + std::to_string(level) + " assertion violation\n";
98#if defined(NEKTAR_DEBUG) || defined(NEKTAR_FULLDEBUG)
99 baseMsg += "Where : " + std::string(routine) + "[" +
100 std::to_string(lineNumber) + "]\nMessage : ";
101#endif
102 baseMsg += std::string(msg);
103
104 // Default rank is zero. If MPI used and initialised, populate with
105 // the correct rank. Messages are only printed on rank zero.
106 int rank = 0;
107#if defined(NEKTAR_USE_MPI) && !defined(NEKTAR_USE_CWIPI)
108 int flag = 0;
109 if (DoComm)
110 {
111 MPI_Initialized(&flag);
112 if (flag)
113 {
114 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
115 }
116 }
117#endif
118
119 std::string btMessage("");
120#if defined(NEKTAR_FULLDEBUG)
121#ifndef _WIN32
123 {
124 void *btArray[40];
125 int btSize;
126 char **btStrings;
127
128 btSize = backtrace(btArray, 40);
129 btStrings = backtrace_symbols(btArray, btSize);
130
131 for (int i = 0; i < btSize; ++i)
132 {
133 btMessage += std::string(btStrings[i]) + "\n";
134 }
135 free(btStrings);
136 }
137#endif
138#endif
139
140 switch (type)
141 {
142 case efatal:
143 if (!rank)
144 {
146 {
147 (*m_outStream) << btMessage;
148 }
149 (*m_outStream)
150 << std::endl
151 << "\033[31mFatal : \033[0m" << baseMsg << std::endl
152 << std::endl;
153 }
154
155#if defined(NEKTAR_USE_MPI) && !defined(NEKTAR_USE_CWIPI)
156 if (DoComm)
157 {
158 if (flag)
159 {
160 MPI_Barrier(MPI_COMM_WORLD);
161 }
162 }
163#endif
164 throw NekError(baseMsg);
165 break;
166 case ewarning:
167 if (!rank)
168 {
170 {
171 (*m_outStream) << btMessage;
172 }
173 (*m_outStream)
174 << std::endl
175 << "\033[36mWarning : \033[0m" << baseMsg << std::endl
176 << std::endl;
177 }
178 break;
179 default:
180 (*m_outStream) << std::endl
181 << "\033[35mUnknown warning type: \033[0m"
182 << baseMsg << std::endl
183 << std::endl;
184 }
185 }
static bool m_printBacktrace

References efatal, ewarning, and m_printBacktrace.

Referenced by Error(), and Error().

◆ Error() [3/3]

static void Nektar::ErrorUtil::Error ( ErrType  type,
const char *  routine,
int  lineNumber,
const std::string &  msg,
unsigned int  level 
)
inlinestatic

Definition at line 187 of file ErrorUtil.hpp.

189 {
190 Error(type, routine, lineNumber, msg.c_str(), level);
191 }

References Error().

◆ HasCustomErrorStream()

static bool Nektar::ErrorUtil::HasCustomErrorStream ( )
inlinestatic

Definition at line 81 of file ErrorUtil.hpp.

82 {
83 return m_outStream != &std::cerr;
84 }
static std::ostream * m_outStream

References m_outStream.

Referenced by Nektar::UnitTests::RedirectCerrIfNeeded().

◆ SetErrorStream()

static void Nektar::ErrorUtil::SetErrorStream ( std::ostream &  o)
inlinestatic

Definition at line 71 of file ErrorUtil.hpp.

72 {
73 m_outStream = &o;
74 }

References m_outStream.

Referenced by PYBIND11_MODULE(), and Nektar::UnitTests::RedirectCerrIfNeeded().

◆ SetPrintBacktrace()

static void Nektar::ErrorUtil::SetPrintBacktrace ( bool  b)
inlinestatic

Definition at line 76 of file ErrorUtil.hpp.

77 {
79 }

References m_printBacktrace.

Member Data Documentation

◆ m_outStream

std::ostream * Nektar::ErrorUtil::m_outStream = &std::cerr
staticprivate

Definition at line 200 of file ErrorUtil.hpp.

Referenced by HasCustomErrorStream(), and SetErrorStream().

◆ m_printBacktrace

bool Nektar::ErrorUtil::m_printBacktrace = true
staticprivate

Definition at line 201 of file ErrorUtil.hpp.

Referenced by Error(), and SetPrintBacktrace().