Nektar++
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 56 of file ErrorUtil.hpp.

Member Enumeration Documentation

◆ ErrType

Enumerator
efatal 
ewarning 

Definition at line 67 of file ErrorUtil.hpp.

68  {
69  efatal,
70  ewarning
71  };

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 192 of file ErrorUtil.hpp.

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

References Error(), and CellMLToNektar.pycml::msg.

◆ 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 88 of file ErrorUtil.hpp.

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

References efatal, ewarning, CellMLToNektar.pycml::level, m_printBacktrace, and CellMLToNektar.pycml::msg.

Referenced by 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 186 of file ErrorUtil.hpp.

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

References Error(), CellMLToNektar.pycml::level, and CellMLToNektar.pycml::msg.

◆ HasCustomErrorStream()

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

Definition at line 83 of file ErrorUtil.hpp.

84  {
85  return m_outStream != &std::cerr;
86  }
static std::ostream * m_outStream
Definition: ErrorUtil.hpp:199

References m_outStream.

◆ SetErrorStream()

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

Definition at line 73 of file ErrorUtil.hpp.

74  {
75  m_outStream = &o;
76  }

References m_outStream.

Referenced by BOOST_PYTHON_MODULE().

◆ SetPrintBacktrace()

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

Definition at line 78 of file ErrorUtil.hpp.

79  {
80  m_printBacktrace = b;
81  }

References m_printBacktrace.

Member Data Documentation

◆ m_outStream

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

Definition at line 199 of file ErrorUtil.hpp.

Referenced by HasCustomErrorStream(), and SetErrorStream().

◆ m_printBacktrace

bool Nektar::ErrorUtil::m_printBacktrace = true
staticprivate

Definition at line 200 of file ErrorUtil.hpp.

Referenced by Error(), and SetPrintBacktrace().