MFEM  v3.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
error.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_ERROR
13 #define MFEM_ERROR
14 
15 #include "../config/config.hpp"
16 #include <iomanip>
17 #include <sstream>
18 
19 namespace mfem
20 {
21 
22 void mfem_error(const char *msg = NULL);
23 
24 void mfem_warning(const char *msg = NULL);
25 
26 }
27 
28 #ifndef _MFEM_FUNC_NAME
29 #ifndef _WIN32
30 // This is nice because it shows the class and method name
31 #define _MFEM_FUNC_NAME __PRETTY_FUNCTION__
32 // This one is C99 standard.
33 //#define _MFEM_FUNC_NAME __func__
34 #else
35 // for Visual Studio C++
36 #define _MFEM_FUNC_NAME __FUNCSIG__
37 #endif
38 #endif
39 
40 // Common error message and abort macro
41 #define _MFEM_MESSAGE(msg, warn) \
42  { \
43  std::ostringstream mfemMsgStream; \
44  mfemMsgStream << std::setprecision(16); \
45  mfemMsgStream << std::setiosflags(std::ios_base::scientific); \
46  mfemMsgStream << msg << '\n'; \
47  mfemMsgStream << " ... at line " << __LINE__; \
48  mfemMsgStream << " in " << _MFEM_FUNC_NAME << " of file "; \
49  mfemMsgStream << __FILE__ << "."; \
50  mfemMsgStream << std::ends; \
51  if (!(warn)) \
52  mfem::mfem_error(mfemMsgStream.str().c_str()); \
53  else \
54  mfem::mfem_warning(mfemMsgStream.str().c_str()); \
55  }
56 
57 // Outputs lots of useful information and aborts.
58 // For all of these functions, "msg" is pushed to an ostream, so you can
59 // write useful (if complicated) error messages instead of writing
60 // out to the screen first, then calling abort. For example:
61 // MFEM_ABORT( "Unknown geometry type: " << type );
62 #define MFEM_ABORT(msg) _MFEM_MESSAGE("MFEM abort: " << msg, 0)
63 
64 // Does a check, and then outputs lots of useful information if the test fails
65 #define MFEM_VERIFY(x, msg) \
66  if (!(x)) \
67  { \
68  _MFEM_MESSAGE("Verification failed: (" \
69  << #x << ") is false: " << msg, 0); \
70  }
71 
72 // Use this if the only place your variable is used is in ASSERTs
73 // For example, this code snippet:
74 // int err = MPI_Reduce(ldata, maxdata, 5, MPI_INT, MPI_MAX, 0, MyComm);
75 // MFEM_CONTRACT_VAR(err);
76 // MFEM_ASSERT( err == 0, "MPI_Reduce gave an error with length "
77 // << ldata );
78 #define MFEM_CONTRACT_VAR(x) if (0 && &x == &x){}
79 
80 // Now set up some optional checks, but only if the right flags are on
81 #ifdef MFEM_DEBUG
82 
83 #define MFEM_ASSERT(x, msg) \
84  if (!(x)) \
85  { \
86  _MFEM_MESSAGE("Assertion failed: (" \
87  << #x << ") is false: " << msg, 0); \
88  }
89 
90 #else
91 
92 // Get rid of all this code, since we're not checking.
93 #define MFEM_ASSERT(x, msg)
94 
95 #endif
96 
97 // Generate a warning message - always generated, regardless of MFEM_DEBUG.
98 #define MFEM_WARNING(msg) _MFEM_MESSAGE("MFEM Warning: " << msg, 1)
99 
100 #endif
void mfem_warning(const char *msg)
Definition: error.cpp:38
void mfem_error(const char *msg)
Definition: error.cpp:23