MFEM  v3.0
 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.googlecode.com.
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 s; \
44  s << std::setprecision(16); \
45  s << std::setiosflags(std::ios_base::scientific); \
46  s << msg << '\n'; \
47  s << " ... at line " << __LINE__; \
48  s << " in " << _MFEM_FUNC_NAME << " of file " << __FILE__ << "."; \
49  s << std::ends; \
50  if (!(warn)) \
51  mfem::mfem_error(s.str().c_str()); \
52  else \
53  mfem::mfem_warning(s.str().c_str()); \
54  }
55 
56 // Outputs lots of useful information and aborts.
57 // For all of these functions, "msg" is pushed to an ostream, so you can
58 // write useful (if complicated) error messages instead of writing
59 // out to the screen first, then calling abort. For example:
60 // MFEM_ABORT( "Unknown geometry type: " << type );
61 #define MFEM_ABORT(msg) _MFEM_MESSAGE("MFEM abort: " << msg, 0)
62 
63 // Does a check, and then outputs lots of useful information if the test fails
64 #define MFEM_VERIFY(x, msg) \
65  if (!(x)) \
66  { \
67  _MFEM_MESSAGE("Verification failed: (" \
68  << #x << ") is false: " << msg, 0); \
69  }
70 
71 // Use this if the only place your variable is used is in ASSERTs
72 // For example, this code snippet:
73 // int err = MPI_Reduce(ldata, maxdata, 5, MPI_INT, MPI_MAX, 0, MyComm);
74 // MFEM_CONTRACT_VAR(err);
75 // MFEM_ASSERT( err == 0, "MPI_Reduce gave an error with length "
76 // << ldata );
77 #define MFEM_CONTRACT_VAR(x) if (0 && &x == &x){}
78 
79 // Now set up some optional checks, but only if the right flags are on
80 #ifdef MFEM_DEBUG
81 
82 #define MFEM_ASSERT(x, msg) \
83  if (!(x)) \
84  { \
85  _MFEM_MESSAGE("Assertion failed: (" \
86  << #x << ") is false: " << msg, 0); \
87  }
88 
89 #else
90 
91 // Get rid of all this code, since we're not checking.
92 #define MFEM_ASSERT(x, msg)
93 
94 #endif
95 
96 // Generate a warning message - always generated, regardless of MFEM_DEBUG.
97 #define MFEM_WARNING(msg) _MFEM_MESSAGE("MFEM Warning: " << msg, 1)
98 
99 #endif
void mfem_warning(const char *msg)
Definition: error.cpp:38
void mfem_error(const char *msg)
Definition: error.cpp:23